[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

Understanding instance variables

Bertram Scharpf

3/23/2005 6:20:00 PM

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


7 Answers

ts

3/23/2005 6:30:00 PM

0

>>>>> "B" == Bertram Scharpf <lists@bertram-scharpf.de> writes:

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



Bertram Scharpf

3/25/2005 12:19:00 AM

0

Hi,

Am Donnerstag, 24. Mär 2005, 03:30:28 +0900 schrieb ts:
> >>>>> "B" == Bertram Scharpf <lists@bertram-scharpf.de> writes:
>
> 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


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


dblack

3/25/2005 12:30:00 AM

0

Bertram Scharpf

3/25/2005 10:49:00 AM

0

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?

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


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


ts

3/25/2005 10:56:00 AM

0

>>>>> "B" == Bertram Scharpf <lists@bertram-scharpf.de> writes:

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

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


dblack

3/25/2005 11:42:00 AM

0

Bertram Scharpf

3/25/2005 11:52:00 AM

0

Hi,

Am Freitag, 25. Mär 2005, 19:55:42 +0900 schrieb ts:
> >>>>> "B" == Bertram Scharpf <lists@bertram-scharpf.de> writes:
>
> B> 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...
>
> 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


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