# Inheritance in ruby

**URL:** <https://www.ruby-forum.com/t/inheritance-in-ruby/167772>\
**Category:** Ruby\
**Created:** [May 12, 2009, 12:47pm UTC](https://www.ruby-forum.com/t/inheritance-in-ruby/167772 "2009-05-12T12:47:31Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![jarodzz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/7cd45c/32.png) [@jarodzz](https://www.ruby-forum.com/u/jarodzz)\
**Post date:** [May 12, 2009, 12:47pm UTC](https://www.ruby-forum.com/t/inheritance-in-ruby/167772/1 "2009-05-12T12:47:31Z")

</div>

Hi, all.  
I’m really new to Ruby. Got this error:

class Page  
@url=""

```
def initialize
    puts "going to "+@url
end

```

end

class MainPage \< Page  
@url = “Main”  
end

class LoginPage \< Page  
@url = “Login”  
end

every time a page is created, I want it to go to its own url.

m = MainPage.new()

# should print going to Main

l = LoginPage.new()

# should print going to Login

But I tried @@, @ and found neither of them works.

Anyone knows what kind of method or variable I could use?

---

<div class="post-metadata">

**Author:** ![jarodzz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/7cd45c/32.png) [@jarodzz](https://www.ruby-forum.com/u/jarodzz)\
**Post date:** [May 12, 2009, 1:32pm UTC](https://www.ruby-forum.com/t/inheritance-in-ruby/167772/2 "2009-05-12T13:32:12Z")

</div>

On Tue, May 12, 2009 at 6:47 AM, jarodzz [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> m = MainPage.new()
> 
> # should print going to Main
> 
> l = LoginPage.new()
> 
> # should print going to Login
> 
> But I tried @@, @ and found neither of them works.
> 
> Anyone knows what kind of method or variable I could use?

It the url is fixed for all instances of each class I wouldn’t use a  
variable at all:

class Page  
def initialize  
puts "going to "+ url  
end  
end

class MainPage \< Page  
def url  
“Main”  
end  
end

class LoginPage \< Page  
def url  
“Login”  
end  
end

–  
Rick DeNatale

Blog: [http://talklikeaduck.denhaven2.com/](http://talklikeaduck.denhaven2.com/)  
Twitter: [http://twitter.com/RickDeNatale](http://twitter.com/RickDeNatale)  
WWR: [http://www.workingwithrails.com/person/9021-rick-denatale](http://www.workingwithrails.com/person/9021-rick-denatale)  
LinkedIn: [http://www.linkedin.com/in/rickdenatale](http://www.linkedin.com/in/rickdenatale)

---

<div class="post-metadata">

**Author:** ![jarodzz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/7cd45c/32.png) [@jarodzz](https://www.ruby-forum.com/u/jarodzz)\
**Post date:** [May 12, 2009, 3:40pm UTC](https://www.ruby-forum.com/t/inheritance-in-ruby/167772/3 "2009-05-12T15:40:51Z")

</div>

jarodzz [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> end
> 
> class LoginPage \< Page  
> @url = “Login”  
> end

The problem is that the @url mentioned in the first line of each class  
definition and the @url mentioned in the initialize method are not the  
same. This is a common beginner mistake. The way to speak of an instance  
variable is from inside an instance method (during code that runs inside  
an instance). So:

class Page  
def initialize  
puts "going to "+@url  
end  
end

class MainPage \< Page  
def initialize  
@url = “Main”  
super  
end  
end

class LoginPage \< Page  
def initialize  
@url = “Login”  
super  
end  
end

m.

---

<div class="post-metadata">

**Author:** ![jarodzz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/7cd45c/32.png) [@jarodzz](https://www.ruby-forum.com/u/jarodzz)\
**Post date:** [May 12, 2009, 3:47pm UTC](https://www.ruby-forum.com/t/inheritance-in-ruby/167772/4 "2009-05-12T15:47:54Z")

</div>

2009/5/12 matt neuburg [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> > class MainPage \< Page  
> > variable is from inside an instance method (during code that runs inside  
> > an instance). So:

Right!

> end  
> end
> 
> class LoginPage \< Page  
> def initialize  
> @url = “Login”  
> super  
> end  
> end

Keep in mind that super class initialization should be invoked  
/before/ sub class initialization. That’s the general pattern  
enforced in other programming languages and there is a good reason to  
do it that way: that way sub class functionality can rely on the super  
class being properly constructed. This is important if you have sub  
class methods which are invoked from within initialize as well as from  
client code. If they cannot rely on the super class state to be  
complete they will either break during initialization or have to be  
made overly complex because they have to react to different  
situations. Rick’s solution does not have this property and is much  
cleaner in this regard.

Kind regards

robert

---

<div class="post-metadata">

**Author:** ![jarodzz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/7cd45c/32.png) [@jarodzz](https://www.ruby-forum.com/u/jarodzz)\
**Post date:** [May 13, 2009, 7:00am UTC](https://www.ruby-forum.com/t/inheritance-in-ruby/167772/5 "2009-05-13T07:00:54Z")

</div>

thanks all.  
It’s now working after  
removing the first line @url,  
and adding each class their own initialize  
following Robert’s suggestion.

On Tue, May 12, 2009 at 9:46 PM, Robert K.
