Hi,
I always wonder what the difference is, and what is good practice to define a class (or module) constant:
class X;MYCONT=1;end
class X;@@MYCONT=1;end
Thanks
Hi,
I always wonder what the difference is, and what is good practice to define a class (or module) constant:
class X;MYCONT=1;end
class X;@@MYCONT=1;end
Thanks
Hi,
I would suggest to define the constant this way:
So, you could invoke it like that:
Hi,
Thanks for having given an answer.
The style guide is useful.
my take would be:
1- for constants, use:
class X
A=1
end
2- not use:
class X
@@A=1
end
I found it interesting in that it allows to limit the visibility of the const to the class and children.
nevertheless; it has the problem that class variables in parent are modified when assigning in child class. So the style guide says to not use class variables (and constants) @@
example:
class X;@@A=1;def a;@@A;end;end
class Y<X;@@A=2;end
X.new.a
=> 2
Y.new.a
=> 2
not even a warning or const redef. not what I wanted…
3- to modify constant visibility, use:
class X
A=1
B="2"
private_constant :A,:B
end
This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.
Sponsor our Newsletter | Privacy Policy | Terms of Service | Remote Ruby Jobs