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 .