Understanding instance variables

B

Bertram Scharpf

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
 
T

ts

B> 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

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

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



Guy Decoux
 
B

Bertram Scharpf

Hi,

Am Donnerstag, 24. Mär 2005, 03:30:28 +0900 schrieb ts:
B> 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

B> puts c.instance_variables, c.instance_eval( '@x')
B> 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
 
D

David A. Black

--927295978-454731979-1111710578=:24592
Content-Type: MULTIPART/MIXED; BOUNDARY="927295978-454731979-1111710578=:24592"

This message is in MIME format. The first part should be readable text,
while the remaining parts are likely unreadable without MIME-aware tools.

--927295978-454731979-1111710578=:24592
Content-Type: TEXT/PLAIN; charset=X-UNKNOWN; format=flowed
Content-Transfer-Encoding: QUOTED-PRINTABLE

Hi --

Hi,

Am Donnerstag, 24. M=E4r 2005, 03:30:28 +0900 schrieb ts:

That means all three of

c's Singleton < C < Object

can have their own instance variables.

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=3D'+y= =2Einspect }
[c,s,C,Object].collect { |x| x.instance_eval '@x' }
=3D> ["I", "S", "C", "O"]


Or is there something I missed?

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


David

--=20
David A. Black
(e-mail address removed)
--927295978-454731979-1111710578=:24592--
--927295978-454731979-1111710578=:24592--
 
B

Bertram Scharpf

Hi,

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?
[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
 
T

ts

B> Am Freitag, 25. Mär 2005, 09:30:01 +0900 schrieb David A. Black:
B> Eh, what's the difference?

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
 
D

David A. Black

--927295978-997283620-1111750917=:23330
Content-Type: MULTIPART/MIXED; BOUNDARY="927295978-997283620-1111750917=:23330"

This message is in MIME format. The first part should be readable text,
while the remaining parts are likely unreadable without MIME-aware tools.

--927295978-997283620-1111750917=:23330
Content-Type: TEXT/PLAIN; charset=X-UNKNOWN; format=flowed
Content-Transfer-Encoding: QUOTED-PRINTABLE

Hi --

Hi,

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

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

--=20
David A. Black
(e-mail address removed)
--927295978-997283620-1111750917=:23330--
--927295978-997283620-1111750917=:23330--
 
B

Bertram Scharpf

Hi,

Am Freitag, 25. Mär 2005, 19:55:42 +0900 schrieb ts:
B> Am Freitag, 25. Mär 2005, 09:30:01 +0900 schrieb David A. Black:

B> 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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,169
Messages
2,570,920
Members
47,462
Latest member
ChanaLipsc

Latest Threads

Top