Understanding instance variables

Hi,

what happens here?

    class C
    end

    c = C.new

    class <<c ; @x = 'X' ; end

    puts C.instance_variables, C.instance_eval( '@x')
    puts c.instance_variables, c.instance_eval( '@x')
    puts @x

Output:

    nil
    nil
    nil

Thanks in advance

Bertram

···

--
Bertram Scharpf
Stuttgart, Deutschland/Germany
http://www.bertram-scharpf.de

    class <<c ; @x = 'X' ; end

You are working with the singleton class (see what is the value of self
when you make the assignement to @x), use the singleton class

    puts c.instance_variables, c.instance_eval( '@x')
    puts @x

   puts class <<c; self end.instance_variables,
          class <<c; self end.instance_eval( '@x')

Guy Decoux

Hi,

> class <<c ; @x = 'X' ; end

You are working with the singleton class (see what is the value of self
when you make the assignement to @x), use the singleton class

> puts c.instance_variables, c.instance_eval( '@x')
> puts @x

   puts class <<c; self end.instance_variables,
          class <<c; self end.instance_eval( '@x')

That means all three of

    c's Singleton < C < Object

can have their own instance variables.

c=class C ; self ; end.new
s=class << C ; self ; end
[c,s,C,Object].zip( %w-I S C O-).each { |x,y| x.instance_eval '@x='+y.inspect }
[c,s,C,Object].collect { |x| x.instance_eval '@x' }

=> ["I", "S", "C", "O"]

Or is there something I missed?

Bertram

···

Am Donnerstag, 24. Mär 2005, 03:30:28 +0900 schrieb ts:

--
Bertram Scharpf
Stuttgart, Deutschland/Germany
http://www.bertram-scharpf.de

Hi --

Hi,

> class <<c ; @x = 'X' ; end

You are working with the singleton class (see what is the value of self
when you make the assignement to @x), use the singleton class

> puts c.instance_variables, c.instance_eval( '@x')
> puts @x

   puts class <<c; self end.instance_variables,
          class <<c; self end.instance_eval( '@x')

That means all three of

   c's Singleton < C < Object

can have their own instance variables.

c=class C ; self ; end.new
s=class << C ; self ; end

Did you mean class << c ? (You had mentioned c's singleton class.)
But either way...

[c,s,C,Object].zip( %w-I S C O-).each { |x,y| x.instance_eval '@x='+y.inspect }
[c,s,C,Object].collect { |x| x.instance_eval '@x' }

=> ["I", "S", "C", "O"]

Or is there something I missed?

... that's correct: any object can have its own instance variables.

David

···

On Fri, 25 Mar 2005, Bertram Scharpf wrote:

Am Donnerstag, 24. Mär 2005, 03:30:28 +0900 schrieb ts:

--
David A. Black
dblack@wobblini.net

Hi,

>
> c's Singleton < C < Object
>
>can have their own instance variables.
>
>>>c=class C ; self ; end.new
>>>s=class << C ; self ; end

Did you mean class << c ? (You had mentioned c's singleton class.)
But either way...

Eh, what's the difference?

>>>[c,s,C,Object].zip( %w-I S C O-).each { |x,y| x.instance_eval '@x='+y.inspect }
>>>[c,s,C,Object].collect { |x| x.instance_eval '@x' }
>=> ["I", "S", "C", "O"]
>
>Or is there something I missed?

... that's correct: any object can have its own instance variables.

Ah, thanks!

Bertram

···

Am Freitag, 25. Mär 2005, 09:30:01 +0900 schrieb David A. Black:

On Fri, 25 Mar 2005, Bertram Scharpf wrote:

--
Bertram Scharpf
Stuttgart, Deutschland/Germany
http://www.bertram-scharpf.de

svg% ruby -e 'class A; end; p class << A; self end; p class << A.new; self end'
#<Class:A>
#<Class:#<A:0x40099f34>>
svg%

Guy Decoux

···

Am Freitag, 25. Mär 2005, 09:30:01 +0900 schrieb David A. Black:

Did you mean class << c ? (You had mentioned c's singleton class.)
But either way...

Eh, what's the difference?

Hi --

···

On Fri, 25 Mar 2005, Bertram Scharpf wrote:

Hi,

Am Freitag, 25. Mär 2005, 09:30:01 +0900 schrieb David A. Black:

On Fri, 25 Mar 2005, Bertram Scharpf wrote:

  c's Singleton < C < Object

can have their own instance variables.

c=class C ; self ; end.new
s=class << C ; self ; end

Did you mean class << c ? (You had mentioned c's singleton class.)
But either way...

Eh, what's the difference?

As examples of the "every object can have i.variables" principle, no
difference. But c's singleton and C's singleton are completely
different objects.

David

--
David A. Black
dblack@wobblini.net

Hi,

···

Am Freitag, 25. Mär 2005, 19:55:42 +0900 schrieb ts:

> Am Freitag, 25. Mär 2005, 09:30:01 +0900 schrieb David A. Black:
>> Did you mean class << c ? (You had mentioned c's singleton class.)
>> But either way...

> Eh, what's the difference?

svg% ruby -e 'class A; end; p class << A; self end; p class << A.new; self end'

Sure, the c in "class <<c" was meant to be lowercase, not
"class <<C". Sorry!

Thank you both.

Bertram

--
Bertram Scharpf
Stuttgart, Deutschland/Germany
http://www.bertram-scharpf.de