I am new to Ruby. I am familiar with several other languages. My question is about calling methods out of order. For example:
def myfunction
myfunction2
end
def myfunction2
puts "in 2"
end
How could I call myfunction2 before it is declared? Several languages let you declare it at the top or in a .h file. How does ruby handle it?
Do I always need to follow this:
def myfunction2
puts "in 2"
end
def myfunction
myfunction2
end
Mainly this bugs me when I need to call another method inside of def initialize for a class.
You can not call a method before you define it. However, that does not mean you can’t define
myfunctionbeforemyfunction2! Ruby has late binding, so the call tomyfunction2inmyfunctionwill not be associated with the actualmyfunction2before you callmyfunction. That means that as long as the first call tomyfunctionis done aftermyfunction2is declared, you should be fine.So, this is ok:
and this is not: