[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

inheritance in ruby

salamond

5/12/2009 10:47:00 AM

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?

4 Answers

Rick DeNatale

5/12/2009 11:31:00 AM

0

On Tue, May 12, 2009 at 6:47 AM, jarodzz <jarodzz@gmail.com> wrote:
> Hi, all.
> I'm really new to Ruby. Got this error:
>
> class Page
> =A0 =A0@url=3D""
>
> =A0 =A0def initialize
> =A0 =A0 =A0 =A0puts "going to "+@url
> =A0 =A0end
> end
>
> class MainPage < Page
> =A0 =A0@url =3D "Main"
> end
>
> class LoginPage < Page
> =A0 =A0@url =3D "Login"
> end
>
> every time a page is created, I want it to go to its own url.
>
> m =3D MainPage.new()
> # should print going to Main
> l =3D 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


--=20
Rick DeNatale

Blog: http://talklikeaduck.denh...
Twitter: http://twitter.com/Ri...
WWR: http://www.workingwithrails.com/person/9021-ric...
LinkedIn: http://www.linkedin.com/in/ri...

matt

5/12/2009 1:37:00 PM

0

jarodzz <jarodzz@gmail.com> wrote:

> class Page
> @url=""
>
> def initialize
> puts "going to "+@url
> end
> end
>
> class MainPage < Page
> @url = "Main"
> 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.

--
matt neuburg, phd = matt@tidbits.com, http://www.tidbits...
Leopard - http://www.takecontrolbooks.com/leopard-custom...
AppleScript - http://www.amazon.com/gp/product/...
Read TidBITS! It's free and smart. http://www.t...

Robert Klemme

5/12/2009 1:47:00 PM

0

2009/5/12 matt neuburg <matt@tidbits.com>:
> jarodzz <jarodzz@gmail.com> wrote:
>
>> class Page
>> =A0 =A0 @url=3D""
>>
>> =A0 =A0 def initialize
>> =A0 =A0 =A0 =A0 puts "going to "+@url
>> =A0 =A0 end
>> end
>>
>> class MainPage < Page
>> =A0 =A0 @url =3D "Main"
>> end
>>
>> class LoginPage < Page
>> =A0 =A0 @url =3D "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:

Right!

> class Page
> =A0 =A0def initialize
> =A0 =A0 =A0 =A0puts "going to "+@url
> =A0 =A0end
> end
>
> class MainPage < Page
> =A0 =A0def initialize
> =A0 =A0 =A0 =A0@url =3D "Main"
> =A0 =A0 =A0 =A0super
> =A0 =A0end
> end
>
> class LoginPage < Page
> =A0 =A0def initialize
> =A0 =A0 =A0 =A0@url =3D "Login"
> =A0 =A0 =A0 =A0super
> =A0 =A0end
> 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

--=20
remember.guy do |as, often| as.you_can - without end
http://blog.rubybestprac...

salamond

5/13/2009 5:00:00 AM

0

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 Klemme
<shortcutter@googlemail.com> wrote:
> 2009/5/12 matt neuburg <matt@tidbits.com>:
>> jarodzz <jarodzz@gmail.com> wrote:
>>
>>> class Page
>>> =A0 =A0 @url=3D""
>>>
>>> =A0 =A0 def initialize
>>> =A0 =A0 =A0 =A0 puts "going to "+@url
>>> =A0 =A0 end
>>> end
>>>
>>> class MainPage < Page
>>> =A0 =A0 @url =3D "Main"
>>> end
>>>
>>> class LoginPage < Page
>>> =A0 =A0 @url =3D "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:
>
> Right!
>
>> class Page
>> =A0 =A0def initialize
>> =A0 =A0 =A0 =A0puts "going to "+@url
>> =A0 =A0end
>> end
>>
>> class MainPage < Page
>> =A0 =A0def initialize
>> =A0 =A0 =A0 =A0@url =3D "Main"
>> =A0 =A0 =A0 =A0super
>> =A0 =A0end
>> end
>>
>> class LoginPage < Page
>> =A0 =A0def initialize
>> =A0 =A0 =A0 =A0@url =3D "Login"
>> =A0 =A0 =A0 =A0super
>> =A0 =A0end
>> end
>
> Keep in mind that super class initialization should be invoked
> /before/ sub class initialization. =A0That'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. =A0This is important if you have sub
> class methods which are invoked from within initialize as well as from
> client code. =A0If 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. =A0Rick's solution does not have this property and is much
> cleaner in this regard.
>
> Kind regards
>
> robert
>
> --
> remember.guy do |as, often| as.you_can - without end
> http://blog.rubybestprac...
>
>