[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

Prevent ruby constant variables from changing?

George Wang

5/22/2008 2:28:00 AM

Hi,

I understand that ruby constant variables are declared by using
uppercase letter. However, the constant variable can still be
reassigned even though a warning is issued.

Is there a way I can fix the value that is initially assigned to the
constant variable?

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

34 Answers

Dave Bass

5/22/2008 11:01:00 AM

0

George Wang wrote:
> However, the constant variable can still be
> reassigned even though a warning is issued.

Am I the only person who thinks that constants should be unchangeable?

Being new to Ruby, it seems weird to me that constants aren't actually
constant. I'm sure there must be some arcane reason for it, but surely
most people want their constants to remain constant! That's how it works
in most languages.

Freezing would seem like a good idea but I haven't actually tried it.
Yet.
--
Posted via http://www.ruby-....

Florian Gilcher

5/22/2008 11:39:00 AM

0

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1


On May 22, 2008, at 1:01 PM, Dave Bass wrote:

> George Wang wrote:
>> However, the constant variable can still be
>> reassigned even though a warning is issued.
>
> Am I the only person who thinks that constants should be unchangeable?
>
> Being new to Ruby, it seems weird to me that constants aren't actually
> constant. I'm sure there must be some arcane reason for it, but surely
> most people want their constants to remain constant! That's how it
> works
> in most languages.
>
> Freezing would seem like a good idea but I haven't actually tried it.
> Yet.
> --
> Posted via http://www.ruby-....
>

If you noticed, class names are constants. As you can change classes
at runtime, constants must be changable.
Actually overwriting Constants emits a warning. A you should program
warnings-free, that should be no problem :).

Regards,
Florian Gilcher
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.8 (Darwin)

iEYEARECAAYFAkg1WvgACgkQJA/zY0IIRZYmkQCeJvyw4XwGA2+sYWN8RdYt+F/7
1pkAn3pRTl129HoSddfetPJhyji3gBg/
=8BGR
-----END PGP SIGNATURE-----

Robert Klemme

5/22/2008 11:46:00 AM

0

On 22.05.2008 13:01, Dave Bass wrote:
> George Wang wrote:
>> However, the constant variable can still be
>> reassigned even though a warning is issued.
>
> Am I the only person who thinks that constants should be unchangeable?

Probably not. However, there are more spots like this in Ruby (e.g.
private methods). It is a general feature of the language to not be too
restrictive about such things. For me this was never an issue. YMMV
though. OTOH this makes some things (mostly related to meta
programming) pretty easy that are hard to impossible in other languages.
For example, if you want to write code in Java that modifies arbitrary
private instance variables, you can do it but it's much more involved
than using #instance_variable_set. It's probably good that it is not
made too easy in Java - and equally good that it's the way it is in Ruby.

> Being new to Ruby, it seems weird to me that constants aren't actually
> constant. I'm sure there must be some arcane reason for it, but surely
> most people want their constants to remain constant! That's how it works
> in most languages.
>
> Freezing would seem like a good idea but I haven't actually tried it.
> Yet.

Freezing does something else: freezing prohibits assigning to and
creation of instance variables. This helps if you want to use an
instance of an otherwise mutable class as constant.

I agree with Pena that the safest approach is probably the one he
demonstrated.

Kind regards

robert

Robert Klemme

5/22/2008 11:59:00 AM

0

On 22.05.2008 13:38, Florian Gilcher wrote:
> If you noticed, class names are constants. As you can change classes
> at runtime, constants must be changable.

No, not necessarily: you can change a class without reassignment to the
constant.

> Actually overwriting Constants emits a warning. A you should program
> warnings-free, that should be no problem :).

At least it's a good guiding rule to follow.

Kind regards

robert

Robert Dober

5/22/2008 12:36:00 PM

0

On Thu, May 22, 2008 at 1:38 PM, Florian Gilcher <flo@andersground.net> wrote:
> -----BEGIN PGP SIGNED MESSAGE-----
> Hash: SHA1
>
>
> On May 22, 2008, at 1:01 PM, Dave Bass wrote:
>
>> George Wang wrote:
>>>
>>> However, the constant variable can still be
>>> reassigned even though a warning is issued.
>>
>> Am I the only person who thinks that constants should be unchangeable?
>>
>> Being new to Ruby, it seems weird to me that constants aren't actually
>> constant. I'm sure there must be some arcane reason for it, but surely
>> most people want their constants to remain constant! That's how it works
>> in most languages.
>>
>> Freezing would seem like a good idea but I haven't actually tried it.
>> Yet.
>> --
>> Posted via http://www.ruby-....
>>
>
> If you noticed, class names are constants.
Not necessarily.
c = Class::new { def ... }
>As you can change classes at
> runtime, constants must be changable.
Not at all, you can always change the object a constant points to,
unless it is frozen.
Robert
--
http://ruby-smalltalk.blo...

---
Whereof one cannot speak, thereof one must be silent.
Ludwig Wittgenstein

Florian Gilcher

5/22/2008 1:12:00 PM

0

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1


On May 22, 2008, at 2:36 PM, Robert Dober wrote:

>>
>> If you noticed, class names are constants.
> Not necessarily.
> c = Class::new { def ... }


So how does the creation of an anonymous class hurt the argument that
class names are constants?
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.8 (Darwin)

iEYEARECAAYFAkg1cSMACgkQJA/zY0IIRZaVYACfYKNZSIRdk1IUeEVSgCAMhto6
vP8An25z7xkB0HzOwszj8U6zlocudryQ
=oNYV
-----END PGP SIGNATURE-----

ara.t.howard

5/22/2008 3:35:00 PM

0


On May 22, 2008, at 7:12 AM, Florian Gilcher wrote:

> So how does the creation of an anonymous class hurt the argument
> that class names are constants?


it's not not anonymous, it's held in a variable, just as normal
classes are

c = Class.new

class C; end

C = Class.new

it's subtle, but there is no such thing as a 'class name', rather
there are classes that a held in variables, often these variables are
constants, but not always, and it is true that ruby give you the
illusion that they are names via a handy Class#name method, but note
that this is really just inspect and not specific to classes:

cfp:~ > ruby -e 'class C;end; module M;end; p C.name; p C; p
M.name; p M'
"C"
C
"M"
M


the name is not special, just a method:

cfp:~ > ruby -e' class C;end; Constant = C; p Constant.name; def
Constant.name; "Constant"; end; p Constant.name '
"C"
"Constant"

regards.

a @ http://codeforp...
--
we can deny everything, except that we have the possibility of being
better. simply reflect on that.
h.h. the 14th dalai lama




ara.t.howard

5/22/2008 3:38:00 PM

0


On May 22, 2008, at 1:49 AM, Pe=F1a, Botp wrote:

> i too had this problem, and the closest i can get is to wrap the =20
> constants in a module and then freeze the module.

i'm sure you're aware but:

cfp:~ > cat a.rb
(

module Namespace
X =3D 4
Y =3D 2
end

).freeze

Object.send :remove_const, :Namespace

Namespace =3D Module.new
Namespace::X =3D 2
Namespace::Y =3D 4

puts Namespace::Y, Namespace::X

cfp:~ > ruby a.rb
4
2



it's hard to get around the dynamic aspects of ruby (thankfully) ;-)

a @ http://codeforp...
--
we can deny everything, except that we have the possibility of being =20
better. simply reflect on that.
h.h. the 14th dalai lama




Robert Dober

5/22/2008 3:40:00 PM

0

On Thu, May 22, 2008 at 3:12 PM, Florian Gilcher <flo@andersground.net> wrote:
> -----BEGIN PGP SIGNED MESSAGE-----
> Hash: SHA1
>
>
> On May 22, 2008, at 2:36 PM, Robert Dober wrote:
>
>>>
>>> If you noticed, class names are constants.
>>
>> Not necessarily.
>> c = Class::new { def ... }
>
>
> So how does the creation of an anonymous class hurt the argument that class
> names are constants?
I will try to answer your question with another one.
What is the name of the class above? Well it is "", would you say this
is a constant?
This however is not what was most missleading. It does not matter if a
constant is pointing to a class or not, class names are not constants
at all, there is just this little quirck in Ruby that
X = Class::new
sets X.name to "X" by some magic.
This allows to reopen the class with
class X
instead of writing
c.module_eval

c.module_eval creates faster code though...

Cheers
Robert

> -----BEGIN PGP SIGNATURE-----
> Version: GnuPG v1.4.8 (Darwin)
>
> iEYEARECAAYFAkg1cSMACgkQJA/zY0IIRZaVYACfYKNZSIRdk1IUeEVSgCAMhto6
> vP8An25z7xkB0HzOwszj8U6zlocudryQ
> =oNYV
> -----END PGP SIGNATURE-----
>
>



--
http://ruby-smalltalk.blo...

---
Whereof one cannot speak, thereof one must be silent.
Ludwig Wittgenstein

Robert Dober

5/22/2008 3:46:00 PM

0

On Thu, May 22, 2008 at 5:34 PM, ara.t.howard <ara.t.howard@gmail.com> wrote:

> it's subtle, but there is no such thing as a 'class name', rather there are
> classes that a held in variables, often these variables are constants, but
> not always, and it is true that ruby give you the illusion that they are
> names via a handy Class#name method, but note that this is really just
> inspect and not specific to classes:
Hmm Ara, are you sure?
Is it not the interpreter that "quirks", as I have put it, the name of
a class when it is assigned to a constant, either via
class Klass
but also via
Klass = Class::new
while it quirks it to "" if we do
c = Class::new

I think it is a mess, after reading your metakoan Rubyquiz I should
never ever have used constants to point to classes anymore, I am a
weakling ;)
Cheers
Robert