In Ruby the last expression in a method is returned.
Why is this not the case for the initialize method?
It is.
class Foo
def initialize()
5
end
end
foo = Foo.send :allocate
foo.send :initialize
#=> 5
So you see that initialize does indeed return 5 here. Foo.new however
will not
return 5. This is quite simply due to the fact that new explicitly
returns
the newly created object, i.e. the call to initialize is not the last
thing
in new, so it’s not the return value of new.
If you want to expand your Browser class with some custom methods and
redirect all other methods to Watir::IE, add
method_missing(methodname, *args) to it, e.g.
class Browser
attr_reader :ie
def initialize @ie ||= Watir::IE.new
end
def method_missing(methodname, *args) @ie.send(methodname, *args)
end
end
ie = Browser.new
Because you are not really calling initialize, you are calling new,
which is a different method. So you need to apply your reasoning to #new.
new is a wrapper around initialize that returns an instance of the
class Browser, no matter what initialize returned.