# Why does the initialize method not return the last expression?

**URL:** <https://www.ruby-forum.com/t/why-does-the-initialize-method-not-return-the-last-expression/145493>\
**Category:** Ruby\
**Created:** [August 10, 2008, 8:10pm UTC](https://www.ruby-forum.com/t/why-does-the-initialize-method-not-return-the-last-expression/145493 "2008-08-10T20:10:30Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![aidy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/eada6e/32.png) [@aidy](https://www.ruby-forum.com/u/aidy)\
**Post date:** [August 10, 2008, 8:10pm UTC](https://www.ruby-forum.com/t/why-does-the-initialize-method-not-return-the-last-expression/145493/1 "2008-08-10T20:10:30Z")

</div>

Hi,

In Ruby the last expression in a method is returned.

Why is this not the case for the initialize method?

class Browser

def initialize  
ie ||= Watir::IE.new  
end

end

ie = Browser.new

#goto is a url navigation method in Watir  
#but I can’t do this

ie.goto(“[www.gmail.com](http://www.gmail.com)”)

Thanks

Aidy

---

<div class="post-metadata">

**Author:** ![aidy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/eada6e/32.png) [@aidy](https://www.ruby-forum.com/u/aidy)\
**Post date:** [August 10, 2008, 8:28pm UTC](https://www.ruby-forum.com/t/why-does-the-initialize-method-not-return-the-last-expression/145493/2 "2008-08-10T20:28:38Z")

</div>

aidy wrote:

> 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.

HTH,  
Sebastian

---

<div class="post-metadata">

**Author:** ![aidy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/eada6e/32.png) [@aidy](https://www.ruby-forum.com/u/aidy)\
**Post date:** [August 10, 2008, 8:37pm UTC](https://www.ruby-forum.com/t/why-does-the-initialize-method-not-return-the-last-expression/145493/3 "2008-08-10T20:37:24Z")

</div>

Like the others expressed, you can’t return a value with initialize.  
You can add an instance variable which will represent your Watir  
object.

class Browser  
attr\_reader :ie  
def initialize  
@ie ||= Watir::IE.new  
end

end

and then:

browser = Browser.new  
browser.ie.goto(“[www.gmail.com](http://www.gmail.com)”)

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

Regards, Thomas

---

<div class="post-metadata">

**Author:** ![aidy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/eada6e/32.png) [@aidy](https://www.ruby-forum.com/u/aidy)\
**Post date:** [August 10, 2008, 8:29pm UTC](https://www.ruby-forum.com/t/why-does-the-initialize-method-not-return-the-last-expression/145493/4 "2008-08-10T20:29:13Z")

</div>

On Sun, Aug 10, 2008 at 8:07 PM, aidy [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> 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.

---

<div class="post-metadata">

**Author:** ![aidy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/eada6e/32.png) [@aidy](https://www.ruby-forum.com/u/aidy)\
**Post date:** [August 10, 2008, 9:10pm UTC](https://www.ruby-forum.com/t/why-does-the-initialize-method-not-return-the-last-expression/145493/5 "2008-08-10T21:10:12Z")

</div>

On Sun, Aug 10, 2008 at 2:07 PM, aidy [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> end
> 
> end
> 
> ie = Browser.new
> 
> #goto is a url navigation method in Watir  
> #but I can’t do this
> 
> ie.goto(“[www.gmail.com](http://www.gmail.com)”)

You have a few options. Depending on what you’re really trying to do.

Here is the simplest.

Browser = Watir::IE  
ie = Browser.new  
ie.goto(“[www.gmail.com](http://www.gmail.com)”)

Michael G.
