Search code examples
rubymethodsargumentsprivateargument-passing

Can I access a method inside another method?


Can I access a method inside another method

 def one(a)
   if a < 10
       two() 
   else
       print "ITs passed in first function ONE"
   end
 end

 def two(b)
   if b < 10
       print "Both function failed"
   else
       print "ITs passed in second function TWO"
   end
 end

 puts one(5)
 puts two(15)

If I run this program I get an error

 test4.rb:9:in `two': wrong number of arguments (0 for 1) (ArgumentError)

Solution

  • The error you got isn't because you can't use the other method. It's because the argument you passed don't match the argument that the method expects.

    Specifically, the method two expects one argument, while you are passing it zero with two().

    Change it to:

     def one(a)
       if a < 10
           two(a)   #<--here
       else
     #...