How to handle the Order method in Ruby?

I am new to Ruby. I am familiar with several other languages. My question is how to call methods out of order. For example:

def myfunction myfunction2 end def myfunction2 puts "in 2" end 

How can I call myfunction2 before it is declared? Several languages ​​allow you to declare them at the top or in the .h file. How does ruby ​​handle this?

Should I always follow this:

 def myfunction2 puts "in 2" end def myfunction myfunction2 end 

This basically bothers me when I need to call another method inside def initialize for the class.

+7
source share
4 answers

You cannot call a method until it is defined. However, this does not mean that you cannot define myfunction before myfunction2 ! Ruby has the last binding, so calling myfunction2 in myfunction will not be bound to the actual myfunction2 before you call myfunction . This means that as long as the first call to myfunction is executed after the declaration of myfunction2 , you should be fine.

So this is normal:

 def myfunction myfunction2 end def myfunction2 puts "in 2" end myfunction 

and this is not so:

 def myfunction myfunction2 end myfunction def myfunction2 puts "in 2" end 
+18
source

The only time order in the method has purely procedural code and is usually shortsighted, given the two methods:

 def greet puts "%s, Dave" % random_greeting end # If I try to use `greet` here, it'll raise a NoMethodError def random_greeting ["Hello", "Bonjour", "Hallo"].sample end # I can use `greet` here, because `random_greeting` is now defiend 

This will work fine if you do not want to use greet before random_greeting is defined, as it allows all non-trivial code to wrap behavior in the class:

 class Doorman def greet puts "%s, Dave" % random_greeting end def random_greeting ["Hello", "Bonjour", "Hallo"].sample end end Doorman.new.greet 

Then you can greet one guest using Doorman.new.greet , wrapping the behavior in the class, you can better simulate the application (maybe different objects in your hotel code give different greetings) and also keeps the main namespace cleared.

The main object in Ruby already has 114 methods on it, so it's much better to put your own methods in classes that represent actors or objects in your project model.

In addition to what you said in the class initialization question, this is entirely possible:

 class Doorman def initialize puts "%s, I'm a new Doorman instance" & random_greeting end def greet "%s, Dave" % random_greeting end def random_greeting ["Hello", "Bonjour", "Hallo"].sample end end 

Even if the random_greeting method random_greeting not defined when we write initailize , the whole class is defined before initialize ever called. Again, by wrapping classes, it makes life easier, cleaner, and means that things remain encapsulated .

+7
source

Ruby is interpreted as a language, so it does not depend on the order of functions, for example:

 [1] pry(main)> def myfunction [1] pry(main)* myfunction2 [1] pry(main)* end => nil [2] pry(main)> [3] pry(main)> def myfunction2 [3] pry(main)* puts "in 2" [3] pry(main)* end => nil [4] pry(main)> myfunction in 2 => nil 

Also, if, for example, a function calls a non-existent function, then a runtime exception will be called only if this function is called, i. e :.

 [5] pry(main)> def foo [5] pry(main)* blubry_starego_marycha [5] pry(main)* end => nil [6] pry(main)> def boo [6] pry(main)* "bom bom bom" [6] pry(main)* end => nil [7] pry(main)> boo => "bom bom bom" [8] pry(main)> foo NameError: undefined local variable or method `blubry_starego_marycha' for main:Object from (pry):9:in `foo' 

As you can see, I declared a function foo , which calls the non-existent function blubry_starego_marycha , and Ruby is fine with it, it throws an exception only if I call foo .

+1
source

You can define a method in any order, the order has nothing.

-one
source

All Articles