In Ruby, what is the difference between putting code in an initialize() method rather than directly in the class body? Both appear to be executed when calling
initialize()
Try to create two instances of MyClass
a = MyClass.new b = MyClass.new
to see the difference:
Hello World World
Hello
World
Code in the class body execute only once - when ruby loads the file. initialize() executes every time you create a new instance of your class.