arguments - if one function result is correct it throws error at another function call in ruby -
def one(a,b) if < 10 def two(b) if b < 10 print "both function failed" else print "its passed in sec function 2 \n" end end else print "its passed in first function 1 \n" end end one(11,10) two(10)
the output above programme
its passed in first function 1 test2.rb:18:in `<main>': undefined method `two' main:object (nomethoderror) i know why error occured
test2.rb:18:in `<main>': undefined method `two' main:object (nomethoderror) but if function 1 fails
def one(a,b) if < 10 if < 10 if true have proceed function two
def one(a,b) if < 10 def two(b) if b < 10
don't define methods within other methods. while ruby technically allows it, it's confusing , should avoided.
in example, run one(11, 10). not execute if < 10 ... end branch, meaning def two(b) ... end never evaluated. result, two method not defined, hence nomethoderror.
you should move two method outside of one:
def one(a,b) if < 10 two(b) else print "its passed in first function 1 \n" end end def two(b) if b < 10 print "both function failed" else print "its passed in sec function 2 \n" end end one(11,10) # passed in first function 1 two(10) # passed in sec function 2 ruby arguments argument-passing function
No comments:
Post a Comment