so there is an implied connection between new() and initialize()?
Historically is there some reason why we didn’t just stick with the
‘def new()’
or did I miss something.
“new” allocates space for the new object and creates it. It then calls
“initialize” to initialize the newly created object. Typically that’s
that only part you want to customize which is why you write an
“initialize” method instead of a “new” method.
new() is actually just a class method of the class object (Obj). You
can create your own method if need be:
class Obj
def self.new
super
…
end
end
Calling ‘super’ is pretty important otherwise your object is not
properly instantiated (I think). You can also wind up with the wrong
kind of object if you’re not careful
irb(main):001:0> class Obj
irb(main):002:1> def self.new
irb(main):003:2> return “Hello!”
irb(main):004:2> end
irb(main):005:1> end
=> nil
irb(main):006:0> o = Obj.new
=> “Hello!”
irb(main):008:0> o.class
=> String