2009/5/12 matt neuburg <matt / tidbits.com>:
> 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:

Right!

> class Page
>   
>    
>  
> end
>
> class MainPage < Page
>   
>  
>  
>  
> end
>
> class LoginPage < Page
>   
>  >  
>  
> 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

-- 
remember.guy do |as, often| as.you_can - without end
http://blog.rubybestpractices.com/