[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

Does singleton variables have any meaning ?

Arno J.

8/16/2007 4:06:00 PM

Hello,

I would like to have a confirmation about the fact that singleton
variables just don't have any meaning, or an explation about how to use
them.
An example to be sure that we are talking about the same thing :

class A

@class_instance_variable

class << self #or class << A

@singleton_variable # <- Can this be used/called elsewhere ?

def some_method
@class_instance_variable
end

end

end

Thanks
--
Posted via http://www.ruby-....

16 Answers

Alex Young

8/16/2007 4:26:00 PM

0

Arno J. wrote:
> Hello,
>
> I would like to have a confirmation about the fact that singleton
> variables just don't have any meaning, or an explation about how to use
> them.
> An example to be sure that we are talking about the same thing :
>
> class A
>
> @class_instance_variable
>
> class << self #or class << A
>
> @singleton_variable # <- Can this be used/called elsewhere ?
>
> def some_method
> @class_instance_variable
> end
>
> end
>
> end
>
> Thanks
class A
class << self
@singleton_variable
attr_accessor :singleton_variable # <----- N.B.
end
end
irb(main):035:0> A.singleton_variable = :foo
=> :foo
irb(main):037:0> A.singleton_variable
=> :foo

That's how I use them...

--
Alex

Phrogz

8/16/2007 6:56:00 PM

0

On Aug 16, 10:25 am, Alex Young <a...@blackkettle.org> wrote:
> Arno J. wrote:
> > class A
> > @class_instance_variable
> > class << self #or class << A
> > @singleton_variable # <- Can this be used/called elsewhere ?
> > def some_method
> > @class_instance_variable
> > end
> > end
> > end


> class A
> class << self
> @singleton_variable
> attr_accessor :singleton_variable # <----- N.B.
> end
> end
> irb(main):035:0> A.singleton_variable = :foo
> => :foo
> irb(main):037:0> A.singleton_variable
> => :foo

Your accessor method there is referencing a new class-level instance
variable, that is in a different scope than your (no-op)
@singleton_variable line there.

irb(main):001:0> class A
irb(main):002:1> @foo = 1
irb(main):003:1> class << self
irb(main):004:2> @foo = 2
irb(main):005:2> attr_accessor :foo
irb(main):006:2> end
irb(main):007:1> end
=> nil
irb(main):008:0> A.foo
=> 1

Alex Young

8/16/2007 7:25:00 PM

0

Phrogz wrote:
> On Aug 16, 10:25 am, Alex Young <a...@blackkettle.org> wrote:
>> Arno J. wrote:
>>> class A
>>> @class_instance_variable
>>> class << self #or class << A
>>> @singleton_variable # <- Can this be used/called elsewhere ?
>>> def some_method
>>> @class_instance_variable
>>> end
>>> end
>>> end
>
>
>> class A
>> class << self
>> @singleton_variable
>> attr_accessor :singleton_variable # <----- N.B.
>> end
>> end
>> irb(main):035:0> A.singleton_variable = :foo
>> => :foo
>> irb(main):037:0> A.singleton_variable
>> => :foo
>
> Your accessor method there is referencing a new class-level instance
> variable, that is in a different scope than your (no-op)
> @singleton_variable line there.
>
> irb(main):001:0> class A
> irb(main):002:1> @foo = 1
> irb(main):003:1> class << self
> irb(main):004:2> @foo = 2
> irb(main):005:2> attr_accessor :foo
> irb(main):006:2> end
> irb(main):007:1> end
> => nil
> irb(main):008:0> A.foo
> => 1
>
>
You're absolutely right. Got myself confused in a maze of twisty
passages. Sorry for the noise :-)

--
Alex

David A. Black

8/16/2007 8:06:00 PM

0

Arno J.

8/16/2007 11:50:00 PM

0

>> class << self #or class << A
>>
>> @singleton_variable # <- Can this be used/called elsewhere ?
>
> Like all instance variables, it belongs to whatever object is 'self'
> at the point where the instance variable appears, and multiple
> references to the same instance variable in different contexts where
> self is the same, will be references to the same instance variable.

But here self is #<Class:A>. Here it really the same context/self as for
class instance variables (A).


> I don't find the term "singleton variable" very helpful. All the
> variables in your example are just instance variables. Calling them
> "class instance variables" is sometimes useful, since there are


So, let's take an another example. After every "puts self" I put the
result.

class A
puts "Inside class"
puts self # A -> This is the self that our instance variable would be
linked to, making a "class instance variable"

def meth
puts "Inside method"
puts self # #<A:0x2ebd140>
end

def self.methc
puts "Inside class method"
puts self # A
end

class << self
puts "Inside singleton"
puts self # #<Class:A> -> and this is the self that our instance
variable would be linked to...

def meths
puts "Inside singleton method"
puts self # A
end
end
end

a = A.new
#<A:0x2ebd140>

With a being #<A:0x2ebd140>, the results are :
- A
- #<A:0x2ebd140>
- #<Class:A>

So, when defining what's usually called a "class instance variable", the
self is "A". For what I call a "singleton variable", but that you called
a "class instance variable", the self is "#<Class:A>".
To me, there's a difference, but I don't know what that "#<Class:A>" is.

It's the same thing with a self.method.
Can someone tell me how to use those @my_variable and self.method in the
following class :

class A
class << self
@my_variable
def self.my_method
end
end
# This is where I'd like to use it
end

# Or we can use it from here on a new instance or an the class itself
maybe ?

I'll sleep over it...
--
Posted via http://www.ruby-....

David A. Black

8/17/2007 12:07:00 AM

0

Arno J.

8/17/2007 9:42:00 AM

0

unknown wrote:

> self is #<Class:A> (the singleton class of A), so @singleton_variable
> belongs to #<Class:A>. It has no connection to A.

Ok, that's what I guessed

> #<Class:A> is the singleton class (sometimes called the "metaclass")
> of A. It's a separate object; it has its own instance variables, and
> doesn't share them with A.
>
> As for the terminology; here:
> @var is an instance variable, and self is a class. So we can call it
> an instance variable or a class instance variable. I don't think it's
> a good idea to introduce yet another term (singleton variable) for it;
> it just gets too confusing.

I'm sorry but I still don't get it (maybe I'm a little bit nit_picking
also)

class A
@first_instance_var # self = A

def meth
@second_instance_var # self = #<A:0x2ebd140>
end

class << self
@thir_instance_var # self = #<Class:A>
end

end


All three variables are instance variables (just because of the @, as
far as I know).
BUT, for what I've read on the web / in books
first_... would be called a "class instance variable"
second_... would be called an "instance variable"
third_... would not be called (I never met any), or would be called a
"class instance variable" according to you, although self is not a class
but a singleton class.

I agree that since a singleton class is some sort of class, it can be
called a "class instance variable". But my question are "how do I access
it ?" and "Is it any usefull" (I understand why first and second can be
used, but not third).

> You could get indirect access to it if the object that owns it creates
> wrapper methods for it. But if you put @my_variable where your "This"
> comment is, it will be a completely different @my_variable.

I don't want to put @my_variable here, I want to use the wrapper method
you're talking about, but I don't know how to build it :)

> David

Thank you for your time

Arno
--
Posted via http://www.ruby-....

Robert Dober

8/17/2007 11:03:00 AM

0

I have no actual use case and I believe there will be not many save
for metaprogramming, meta-meta programming maybe ;)

517/18 > cat singvar.rb && ruby singvar.rb
class A
@a = 42
class << self
@a = 222
def class_inst; @a end
def sing_inst; class << self; self end.instance_variable_get("@a") end
end
end
puts A.class_inst
puts A.sing_inst
42
222

Cheers
Robert



--
[...] as simple as possible, but no simpler.
-- Attributed to Albert Einstein

Robert Dober

8/17/2007 11:07:00 AM

0

On 8/17/07, Robert Dober <robert.dober@gmail.com> wrote:
<snip>
> def sing_inst; class << self; self end.instance_variable_get("@a") end
I tend to think too complicated :(

def sing_inst; class << self; @a end end

does the job of course.
<snip>


> 42
> 222
>
R.

David A. Black

8/17/2007 11:29:00 AM

0