Accessing superclass variables

How do I access an outer variable from within a nested class?

Is it possible to directly access them?

···

--
venlig hilsen / best regards
Nicolaj Søndberg-Madsen

I am not sure what you wanted to know from your question.

Are you asking
How a variable of super class can be accessed from its sub-class?

Thanks
Mohammad

···

On Wed, 2004-09-01 at 11:40, Nicolaj Soendberg Madsen wrote:

How do I access an outer variable from within a nested class?

Is it possible to directly access them?

jib:~ > cat a.rb
   class Outer
     CONST = 42
     p CONST
     class Inner
       CONST = 42.0
       p CONST
       p Outer::CONST
       p ::Outer::CONST
     end
     p Inner::CONST
   end

   p Outer::CONST
   p Outer::Inner::CONST
   p ::Outer::CONST
   p ::Outer::Inner::CONST

   jib:~ > ruby a.rb
   42
   42.0
   42
   42.0
   42
   42.0
   42
   42.0

initial '::' means 'start at the very top level. not needed here - but good to
know.

cheers.

-a

···

On Wed, 1 Sep 2004, Nicolaj Soendberg Madsen wrote:

How do I access an outer variable from within a nested class?

Is it possible to directly access them?

--

EMAIL :: Ara [dot] T [dot] Howard [at] noaa [dot] gov
PHONE :: 303.497.6469
A flower falls, even though we love it;
and a weed grows, even though we do not love it. --Dogen

===============================================================================

Hi --

···

On Thu, 2 Sep 2004 Ara.T.Howard@noaa.gov wrote:

On Wed, 1 Sep 2004, Nicolaj Soendberg Madsen wrote:

> How do I access an outer variable from within a nested class?
>
> Is it possible to directly access them?

   jib:~ > cat a.rb
   class Outer
     CONST = 42
     p CONST
     class Inner
       CONST = 42.0

[...]

Those are (Ruby's somewhat variable :slight_smile: constants, though, not
variables.

David

--
David A. Black
dblack@wobblini.net

Exactly!
That was my question. I would still appreciate any effort to explain me
how it works :slight_smile:

···

On Thu, 2 Sep 2004, Mohammad Khan wrote:

I am not sure what you wanted to know from your question.

Are you asking
How a variable of super class can be accessed from its sub-class?

Thanks
Mohammad

On Wed, 2004-09-01 at 11:40, Nicolaj Soendberg Madsen wrote:
> How do I access an outer variable from within a nested class?
>
> Is it possible to directly access them?

--
venlig hilsen / best regards
Nicolaj Søndberg-Madsen

"Nicolaj Soendberg Madsen" <nicolaj@cs.aau.dk> schrieb im Newsbeitrag
news:Pine.GSO.4.58.0409020937480.13526@fire2.cs.aau.dk...
Exactly!
That was my question. I would still appreciate any effort to explain me
how it works :slight_smile:

Note though that the concept of nested class is something completely
different from sub class! The general advice is to use accessor methods
although you can access them directly. Consider this:

class Super
  attr_accessor :foo

  class Nested
    def recommended(super_obj)
      super_obj.foo
    end

    def direct(super_obj)
      super_obj.instance_variable_get "@foo"
    end
  end
end

class Sub < Super
  def direct; @foo; end
  def recommended; foo; end
end

?> sub = Sub.new
=> #<Sub:0x102d4de8>

sub.foo = "bar"

=> "bar"

sub.direct

=> "bar"

sub.recommended

=> "bar"

nest = Super::Nested.new

=> #<Super::Nested:0x102cf220>

nest.direct(sub)

=> "bar"

nest.recommended(sub)

=> "bar"

···

On Thu, 2 Sep 2004, Mohammad Khan wrote:

I am not sure what you wanted to know from your question.

Are you asking
How a variable of super class can be accessed from its sub-class?

Thanks
Mohammad

On Wed, 2004-09-01 at 11:40, Nicolaj Soendberg Madsen wrote:
> How do I access an outer variable from within a nested class?
>
> Is it possible to directly access them?

Regards

    robert