[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

metaprogramming surprise?

Artur Merke

10/17/2006 9:52:00 AM

Here comes some simple meta code

>>>>>>>>>>>>>>>>>>>>>>>>>
class D
@@e=5
@@f = 10
@@g = 20
class << self
attr_reader :e
attr_accessor :g
end
def self.f
@@f
end

def show
print "\nshow @@e=",@@e, " @@f=", @@f, " @@g=",@@g
print "\nshow self.class.e=", self.class.e
print "\nshow self.class.f=", self.class.f
print "\nshow self.class.g=", self.class.g
end
end

D.g= 40
print "\nD.e= ",D.e
print "\nD.g= ",D.g
print "\nD.f= ",D.f
D.new.show
<<<<<<<<<<<<<<<<<<<<<<<<<<<

here is the result

D.e= nil
D.g= 40
D.f= 10
show @@e=5 @@f=10 @@g=20
show self.class.e=nil
show self.class.f=10
show self.class.g=40

I'would expect
D.e= 5
D.g= 40
D.f= 10
show @@e=5 @@f=10 @@g=40
show self.class.e=nil
show self.class.f=10
show self.class.g=40

so what's wrong with my understanding, that

class D
@@e=5
class << self
attr_reader :e # <- why this does not refer to @@e
end
end

13 Answers

Artur Merke

10/17/2006 9:57:00 AM

0


Artur Merke wrote:
> Here comes some simple meta code
>
> >>>>>>>>>>>>>>>>>>>>>>>>>
> class D
> @@e=5
> @@f = 10
> @@g = 20
> class << self
> attr_reader :e
> attr_accessor :g
> end
> def self.f
> @@f
> end
>
> def show
> print "\nshow @@e=",@@e, " @@f=", @@f, " @@g=",@@g
> print "\nshow self.class.e=", self.class.e
> print "\nshow self.class.f=", self.class.f
> print "\nshow self.class.g=", self.class.g
> end
> end
>
> D.g= 40
> print "\nD.e= ",D.e
> print "\nD.g= ",D.g
> print "\nD.f= ",D.f
> D.new.show
> <<<<<<<<<<<<<<<<<<<<<<<<<<<
>
> here is the result
>
> D.e= nil
> D.g= 40
> D.f= 10
> show @@e=5 @@f=10 @@g=20
> show self.class.e=nil
> show self.class.f=10
> show self.class.g=40
>
> I'would expect
> D.e= 5
> D.g= 40
> D.f= 10
> show @@e=5 @@f=10 @@g=40
> show self.class.e=nil
> show self.class.f=10
> show self.class.g=40
>
> so what's wrong with my understanding, that
>
> class D
> @@e=5
> class << self
> attr_reader :e # <- why this does not refer to @@e
> end
> end

sorry for the typo, I'would naturally expect
D.e= 5
D.g= 40
D.f= 10
show @@e=5 @@f=10 @@g=40
show self.class.e=5
show self.class.f=10
show self.class.g=40

Jano Svitok

10/17/2006 10:03:00 AM

0

On 10/17/06, Artur Merke <am@artbot.de> wrote:
> Here comes some simple meta code
>
> >>>>>>>>>>>>>>>>>>>>>>>>>
> class D
> @@e=5
> @@f = 10
> @@g = 20
> class << self
> attr_reader :e
> attr_accessor :g
> end
> def self.f
> @@f
> end
>
> def show
> print "\nshow @@e=",@@e, " @@f=", @@f, " @@g=",@@g
> print "\nshow self.class.e=", self.class.e
> print "\nshow self.class.f=", self.class.f
> print "\nshow self.class.g=", self.class.g
> end
> end
>
> D.g= 40
> print "\nD.e= ",D.e
> print "\nD.g= ",D.g
> print "\nD.f= ",D.f
> D.new.show
> <<<<<<<<<<<<<<<<<<<<<<<<<<<
>
> here is the result
>
> D.e= nil
> D.g= 40
> D.f= 10
> show @@e=5 @@f=10 @@g=20
> show self.class.e=nil
> show self.class.f=10
> show self.class.g=40
>
> I'would expect
> D.e= 5
> D.g= 40
> D.f= 10
> show @@e=5 @@f=10 @@g=40
> show self.class.e=nil
> show self.class.f=10
> show self.class.g=40
>
> so what's wrong with my understanding, that
>
> class D
> @@e=5
> class << self
> attr_reader :e # <- why this does not refer to @@e
> end
> end

There's a difference between class variables (@@x) and singleton class
instance variables (self.class.@x). They are not the same thing.

dblack

10/17/2006 10:41:00 AM

0

Artur Merke

10/17/2006 12:57:00 PM

0


Jan Svitok schrieb:

> On 10/17/06, Artur Merke <am@artbot.de> wrote:
> > Here comes some simple meta code
> >
> > >>>>>>>>>>>>>>>>>>>>>>>>>
> > class D
> > @@e=5
> > @@f = 10
> > @@g = 20
> > class << self
> > attr_reader :e
> > attr_accessor :g
> > end
> > def self.f
> > @@f
> > end
> >
> > def show
> > print "\nshow @@e=",@@e, " @@f=", @@f, " @@g=",@@g
> > print "\nshow self.class.e=", self.class.e
> > print "\nshow self.class.f=", self.class.f
> > print "\nshow self.class.g=", self.class.g
> > end
> > end
> >
> > D.g= 40
> > print "\nD.e= ",D.e
> > print "\nD.g= ",D.g
> > print "\nD.f= ",D.f
> > D.new.show
> > <<<<<<<<<<<<<<<<<<<<<<<<<<<
> >
> > here is the result
> >
> > D.e= nil
> > D.g= 40
> > D.f= 10
> > show @@e=5 @@f=10 @@g=20
> > show self.class.e=nil
> > show self.class.f=10
> > show self.class.g=40
> >
> > I'would expect
> > D.e= 5
> > D.g= 40
> > D.f= 10
> > show @@e=5 @@f=10 @@g=40
> > show self.class.e=nil
> > show self.class.f=10
> > show self.class.g=40
> >
> > so what's wrong with my understanding, that
> >
> > class D
> > @@e=5
> > class << self
> > attr_reader :e # <- why this does not refer to @@e
> > end
> > end
>
> There's a difference between class variables (@@x) and singleton class
> instance variables (self.class.@x). They are not the same thing.

OK, but why are they not the same???

I'm missing some kind of analogy, see the following code:

>>>>>>>>>>>>>>>>>>>

class E
def show; print "\noutput @e= ",@e; end
end

class F
def show; print "\noutput @@ff= ",@@ff; end
end

obj= E.new
class << obj
attr_accessor :e
end

class << F
attr_accessor :ff
end


F.ff= "should be @@ff?, but is not"
obj.e= "should be @e, and really is"

obj.show
F.new.show

output @e= should be @e, and really is
test.rb:33:in `show': uninitialized class variable @@ff in F
(NameError)
from test.rb:50

Joel VanderWerf

10/17/2006 1:56:00 PM

0

Artur Merke wrote:
> Jan Svitok schrieb:
...
>> There's a difference between class variables (@@x) and singleton class
>> instance variables (self.class.@x). They are not the same thing.
>
> OK, but why are they not the same???

A class variable is not an instance variable of any instance, not even a
class. It has its own special semantics of sharing that is quite
different from the privacy of an instance variable.

class A
@@y = "A y"
end

class B < A
@@x = "B x"
@@y = "B y"
end

class A
@@x = "A x"
end

class A
p [@@x, @@y] # ==> ["A x", "B y"]
end

class B
p [@@x, @@y] # ==> ["B x", "B y"]
end

Note that the fact that @@x was assigned first in a subclass makes it
distinct in A and B, but the fact that @@y was assigned first in the
superclass makes it shared.

--
vjoel : Joel VanderWerf : path berkeley edu : 510 665 3407

Ara.T.Howard

10/17/2006 2:26:00 PM

0

Artur Merke

10/17/2006 3:05:00 PM

0


Joel VanderWerf schrieb:

> Artur Merke wrote:
> > Jan Svitok schrieb:
> ..
> >> There's a difference between class variables (@@x) and singleton class
> >> instance variables (self.class.@x). They are not the same thing.
> >
> > OK, but why are they not the same???
>
> A class variable is not an instance variable of any instance, not even a
> class. It has its own special semantics of sharing that is quite
> different from the privacy of an instance variable.
>
> class A
> @@y = "A y"
> end
>
> class B < A
> @@x = "B x"
> @@y = "B y"
> end
>
> class A
> @@x = "A x"
> end
>
> class A
> p [@@x, @@y] # ==> ["A x", "B y"]
> end
>
> class B
> p [@@x, @@y] # ==> ["B x", "B y"]
> end
>
> Note that the fact that @@x was assigned first in a subclass makes it
> distinct in A and B, but the fact that @@y was assigned first in the
> superclass makes it shared.
>
> --
> vjoel : Joel VanderWerf : path berkeley edu : 510 665 3407
thanx, now I understand.

I also recovered my analogy

class E
def show; print "\noutput show= ",@e; end
end

class F
def self.show; print "\noutput F.show= ",@f; end
def show
print "\noutput @f= ",@f
end
end

obj= E.new
class << obj
attr_accessor :e
end

class << F
attr_accessor :f
end

F.f= "should be 'F.@f', and really is"
obj.e= "should be @e, and really is"

obj.show
F.show
F.new.show

<<<<<<<<<<<<<<<<<<<

results in (the now expected)

output show= should be @e, and really is
output E.show= should be 'F.@f', and really is
output @f= nil


So one has to distinguish between using a 'class variable' and
'class instance variable':

class A
@@class_variable
@class_instance_variable
def initialize
@instance_variable
end
end

I hope this is the correct naming for this variables

Joel VanderWerf

10/17/2006 4:10:00 PM

0

Artur Merke wrote:
> So one has to distinguish between using a 'class variable' and
> 'class instance variable':
>
> class A
> @@class_variable
> @class_instance_variable
> def initialize
> @instance_variable
> end
> end
>
> I hope this is the correct naming for this variables

That seems correct.

--
vjoel : Joel VanderWerf : path berkeley edu : 510 665 3407

John W. Long

10/17/2006 6:02:00 PM

0

Jan Svitok wrote:
> There's a difference between class variables (@@x) and singleton class
> instance variables (self.class.@x). They are not the same thing.

More on this here:

http://wiseheart.../articles/2006/09/22/class-level-instance-...

--
John Long
http://wiseheart...
http://radi...

Gary Wright

10/19/2006 3:24:00 AM

0


On Oct 17, 2006, at 2:02 PM, John W. Long wrote:

> Jan Svitok wrote:
>> There's a difference between class variables (@@x) and singleton
>> class
>> instance variables (self.class.@x). They are not the same thing.

Class variables seem to trip up everyone learning Ruby, yet
proficient Ruby programmers rarely seem to use them. Are they really
worth the trouble? I've yet to feel the need to use them but maybe
that is just me.

Any chance that class variables will disappear in Ruby 2.0? I'm only
half joking.


Gary Wright