[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

[ruby2] will '@@' disapear in ruby2?

Lionel Thiry

3/12/2005 2:43:00 PM

Hello there!

I remember I have read some post of Matz saying '@@' will disapear in ruby2.
Have I read well or am I in some sort of confusion once again? Will '@@' really
disapear in ruby2 or not?

Thanks in advance,
Lionel Thiry
12 Answers

Florian Gross

3/12/2005 6:19:00 PM

0

Lionel Thiry wrote:

> I remember I have read some post of Matz saying '@@' will disapear in
> ruby2. Have I read well or am I in some sort of confusion once again?
> Will '@@' really disapear in ruby2 or not?

AFAIK it will only change in that it will have a better defined role.
(Definition time scope based sharing between instances and the class
itself, AFAIK.)

dblack

3/15/2005 3:12:00 PM

0

Yukihiro Matsumoto

3/15/2005 3:15:00 PM

0

Hi,

In message "Re: [ruby2] will '@@' disapear in ruby2?"
on Tue, 15 Mar 2005 22:55:48 +0900, Lionel Thiry <lthiryidontwantspam@skynetnospam.be> writes:

|I remember I have read some post of Matz saying '@@' will disapear in ruby2.

Did I? I remember I admitted it's ugly.

|Have I read well or am I in some sort of confusion once again? Will '@@' really
|disapear in ruby2 or not?

I have no plan to remove it right now.

matz.


Lionel Thiry

3/15/2005 3:40:00 PM

0

David A. Black wrote:
> Hi --
>
> On Tue, 15 Mar 2005, Lionel Thiry wrote:
>
>> Hello there!
>>
>> I remember I have read some post of Matz saying '@@' will disapear in
>> ruby2. Have I read well or am I in some sort of confusion once again?
>> Will '@@' really disapear in ruby2 or not?
>
>
> I think you may have read my post saying that if I could change one
> thing about Ruby, it would be to get rid of @@var :-)
Maybe, I can't remember who post it. But I remember Matz saying he made a big
mistake and regret it. Or was it someone who tried to act as him?

> Matz is going
> to change it, so that it's more truly class-specific, though I would
> still rather just ask classes to have their own instance variables and
> not have this other thing that's *almost* like that, but not quite.
> I've seen a lot of confusion arising from the @/@@ similarity, and
> I've never thought it's worth it.
I'm currently explaining the differences to a friend. As he mainly comes from
java and C++ background, it is a big pain.

>
> Anyway, the short answer as far as I know is "no" :-)
Too bad! :)

--
Lionel Thiry

Lionel Thiry

3/15/2005 3:44:00 PM

0

Yukihiro Matsumoto wrote:
> Hi,
>
> In message "Re: [ruby2] will '@@' disapear in ruby2?"
> on Tue, 15 Mar 2005 22:55:48 +0900, Lionel Thiry <lthiryidontwantspam@skynetnospam.be> writes:
>
> |I remember I have read some post of Matz saying '@@' will disapear in ruby2.
>
> Did I? I remember I admitted it's ugly.
Maybe I'm in confusion but... I remember you said you made a mistake with '@@',
in response to a post saying '@@' violates some fundamental OO principles, or
something like that, and should had never existed.

>
> |Have I read well or am I in some sort of confusion once again? Will '@@' really
> |disapear in ruby2 or not?
>
> I have no plan to remove it right now.
>
> matz.
>
>

Thanks a lot for the answer!

--
Lionel Thiry

Csaba Henk

3/16/2005 8:45:00 AM

0

On 2005-03-15, Lionel Thiry <lthiryidontwantspam@skynetnospam.be> wrote:
>> Anyway, the short answer as far as I know is "no" :-)
> Too bad! :)

I do like having them. In fact, they are globals, wrapped in the
namespace of a class/module. There are situations when you need such a
beast, and it's better than an explicit global... and better than using a
container as a constant (just because constans don't like being
reassigned), that's really ugly.

My problem is that you can't really access them from the outside, unless
accessor method are set up. By "from the outside" I mean that from code
which is not in the scope of a "class/module Foo ... end" thingy. Does
anyone know, eg, how to define a class variable in a nameless class?

Csaba

dblack

3/16/2005 11:34:00 AM

0

Lionel Thiry

3/16/2005 3:14:00 PM

0

Csaba Henk wrote:
> On 2005-03-15, Lionel Thiry <lthiryidontwantspam@skynetnospam.be> wrote:
>
>>>Anyway, the short answer as far as I know is "no" :-)
>>
>>Too bad! :)
>
>
> I do like having them. In fact, they are globals, wrapped in the
> namespace of a class/module. There are situations when you need such a
> beast, and it's better than an explicit global... and better than using a
> container as a constant (just because constans don't like being
> reassigned), that's really ugly.
There's still class instance variable.

class MyClass
@a = "class instance value"

attr_reader :a
def initialize(value)
@a = value
end

def a_of_the_class
self.class.a
end

class << self
attr_reader :a
end
end

my_instance = MyClass.new("instance value")
puts my_instance.a # output: instance value
puts my_instance.a_of_the_class # output: class instance value

other_instance = MyClass.new("other instance value")
puts other_instance.a # output: other instance value
puts other_instance.a_of_the_class # output: class instance value

I prefer that way of doing things over the @@ way.

>
> My problem is that you can't really access them from the outside, unless
> accessor method are set up. By "from the outside" I mean that from code
> which is not in the scope of a "class/module Foo ... end" thingy. Does
> anyone know, eg, how to define a class variable in a nameless class?
With class instance variable, this is easier, I suppose, as you can write:
class << self
attr_reader :a
end

--
Lionel Thiry

Csaba Henk

3/17/2005 6:55:00 AM

0

On 2005-03-16, David A. Black <dblack@wobblini.net> wrote:
> Do you mean like:
>
> def class_factory
> return Class.new { @@x = 1 }
> end
>
> class_factory.class_eval { puts @@x } # 1

let's go on... replace your last line with:

(c = class_factory).class_eval { puts @@x }

and then:

c.class_variables # => []
@@x # => 1
Object.class_variables # => ["@@x"]

What you suggest defines a class variable of Object, not of the nameless
class.

Csaba

Csaba Henk

3/17/2005 7:32:00 AM

0

On 2005-03-16, Lionel Thiry <lthiryidontwantspam@skynetnospam.be> wrote:
> There's still class instance variable.
>
> class MyClass
> @a = "class instance value"

(snip)

> I prefer that way of doing things over the @@ way.

Yes, you are right, you can in fact go that way. @@ is almost nothing
more than syntax sugar. But syntax sugar is an important component of
the rubyness of ruby...

When I write, "almost", I refer to the following slight difference:
within any subclass of the class defining @@x, it can be accessed
directly, without defining accessor methods and without a reference to
the scope where @@x was introduced. I can't give an example though where
this would have real importance... where you would use the subclass in a
way such that you don't really know in which class has the variable been
defined.

Anyway, if you look into (properly written) ruby code, and you see
@@foo, it sticks out, and you can guess its somewhat specific role in
that class design, and easily identify the way and the reason of its
usage. The same regards to constants, and it's a good thing IMHO.

> With class instance variable, this is easier, I suppose, as you can write:
> class << self
> attr_reader :a
> end

Yes, I know. ATM it's a reason for preferring class instance variables
over class variables. But, what I mean doesn't contradict this. I
just said, it would be good if we had this easy accessors to class
variables as well.

Csaba