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

L

Lionel Thiry

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
 
F

Florian Gross

Lionel said:
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.)
 
D

David A. Black

Hi --

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

Anyway, the short answer as far as I know is "no" :)


David
 
Y

Yukihiro Matsumoto

Hi,

In message "Re: [ruby2] will '@@' disapear in ruby2?"

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

Lionel Thiry

David said:
Hi --




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! :)
 
L

Lionel Thiry

Yukihiro said:
Hi,

In message "Re: [ruby2] will '@@' disapear in ruby2?"

|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!
 
C

Csaba Henk


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
 
D

David A. Black

Hi --

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.

You can always use class_eval or instance_eval. I think part of the
point is that they're specific to that scope.
Does anyone know, eg, how to define a class variable in a nameless
class?

Do you mean like:

def class_factory
return Class.new { @@x = 1 }
end

class_factory.class_eval { puts @@x } # 1


David
 
L

Lionel Thiry

Csaba said:
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
 
C

Csaba Henk

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
 
C

Csaba Henk

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
 
D

David A. Black

Hi --

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.

Ugh. Well, as I've said, I'd like @@var to disappear anyway :)


David
 
L

Lionel Thiry

David said:
Hi --

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.


Ugh. Well, as I've said, I'd like @@var to disappear anyway :)


David
I agree!
I just don't understand all this code. (and with ruby 1.9.0, I feel even more
confused with @@)
 

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
473,733
Messages
2,569,440
Members
44,832
Latest member
GlennSmall

Latest Threads

Top