[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

local constant

aidy

7/26/2008 2:32:00 PM

Hi,

Could anyone tell me why Ruby does not allow a local constant?

FIREFOX_CLASS = "[CLASS:MozillaUIWindowClass]"

Thank You

Aidy
12 Answers

David A. Black

7/26/2008 2:32:00 PM

0

Hi --

On Sat, 26 Jul 2008, aidy wrote:

> Hi,
>
> Could anyone tell me why Ruby does not allow a local constant?
>
> FIREFOX_CLASS = "[CLASS:MozillaUIWindowClass]"

I'm afraid I don't follow. That statement will execute perfectly well.
What exactly are you trying to do?


David

--
Rails training from David A. Black and Ruby Power and Light:
Intro to Ruby on Rails July 21-24 Edison, NJ
* Advancing With Rails August 18-21 Edison, NJ
* Co-taught by D.A. Black and Erik Kastner
See http://www.r... for details and updates!

aidy

7/26/2008 3:11:00 PM

0

On Jul 26, 3:32 pm, "David A. Black" <dbl...@rubypal.com> wrote:
Hi David

Thanks for getting back

> On Sat, 26 Jul 2008, aidy wrote:
> > Hi,
>
> > Could anyone tell me why Ruby does not allow a local constant?
>
> > FIREFOX_CLASS = "[CLASS:MozillaUIWindowClass]"
>
> I'm afraid I don't follow. That statement will execute perfectly well.
> What exactly are you trying to do?
>

I am a little puzzled

irb(main):001:0> def aidy
irb(main):002:1> FIREFOX_CLASS = "[CLASS:MozillaUIWindowClass]"
irb(main):003:1> end
SyntaxError: compile error
(irb):2: dynamic constant assignment
FIREFOX_CLASS = "[CLASS:MozillaUIWindowClass]"
^

Thanks

Aidy

Phlip

7/26/2008 3:20:00 PM

0

aidy wrote:

> irb(main):001:0> def aidy
> irb(main):002:1> FIREFOX_CLASS = "[CLASS:MozillaUIWindowClass]"
> irb(main):003:1> end
> SyntaxError: compile error
> (irb):2: dynamic constant assignment
> FIREFOX_CLASS = "[CLASS:MozillaUIWindowClass]"

The purpose of a constant is to configure two or more methods with the same
invariable value. Within one method, there's no difference between assigning a
putative local constant, and simply assigning a variable - then not varying it.

Ruby expects to assign constants only once. If you called aidy() twice, the
constant would re-assign, and it therefor would not be constant.

Put these rules together, and Ruby can only enforce its constant concept if you
can't write them locally.

--
Phlip

aidy

7/26/2008 3:58:00 PM

0

On Jul 26, 4:19 pm, Phlip <phlip2...@gmail.com> wrote:

>
> Ruby expects to assign constants only once. If you called aidy() twice, the
> constant would re-assign, and it therefor would not be constant.
>

Excellant explanation.

Aidy


Robert Dober

7/26/2008 4:07:00 PM

0

On Sat, Jul 26, 2008 at 5:53 PM, aidy <aidy.lewis@googlemail.com> wrote:
> On Jul 26, 4:19 pm, Phlip <phlip2...@gmail.com> wrote:
>
>>
>> Ruby expects to assign constants only once. If you called aidy() twice, the
>> constant would re-assign, and it therefor would not be constant.
>>
>
> Excellant explanation.

But not quite correct, constants *can* be reassigned all you get is a warning.
Nevertheless I think it is good semantics to forbid constant
assignments in methods. If for one reason or another
you need such a beast you can still do it of course.

def a x; Object.const_set "A", x end
a 42
p A
a 44 # get a warning
p A

HTH
Robert





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

There's no one thing that's true. It's all true.
--
Ernest Hemingway

Phlip

7/26/2008 4:22:00 PM

0

Robert Dober wrote:

>>> Ruby expects to assign constants only once. If you called aidy() twice, the
>>> constant would re-assign, and it therefor would not be constant.
>>>
>> Excellant explanation.
>
> But not quite correct, constants *can* be reassigned all you get is a warning.

Read my verbiage again.

And... constants are only labels. Unless you .freeze their targets, you can
..replace them, etc. etc...

One almost misses C++, where they fill the language up with risks and loopholes,
all to allow most 'const' things to optimize at compile time without global
knowledge...

--
Phlip

Marc Heiler

7/26/2008 7:55:00 PM

0

> Read my verbiage again.

Not trying to nitpick but I agree with Robert Dober about the
formulation:

>> If you called aidy() twice, the constant would re-assign,
>> and it therefor would not be constant.

> constants *can* be reassigned

Especially since the error will happen no matter if the constant already
existed, or did not yet exist.

Ruby allows one to reassign constants freely, while only raising a
warning.
One can even go further and misuse constants as hash/array storage
places and put stuff in or get it out again easily.

Ruby seems to dislike this only if it is done within a method, by
raising the "dynamic constant assignment" error.
--
Posted via http://www.ruby-....

Phlip

7/27/2008 5:09:00 AM

0

Marc Heiler wrote:

>> Read my verbiage again.
>
> Not trying to nitpick but I agree with Robert Dober about the
> formulation:
>
>>> If you called aidy() twice, the constant would re-assign,
>>> and it therefor would not be constant.

The quote depends on magically suspending the Ruby error message. Hence it does
not say out-of-the-box Ruby cannot reassign.

(We get reassignments, with their warnings, all the time in Rails, partly due to
its Magic Loader facility...)

--
Phlip

David A. Black

7/27/2008 11:06:00 AM

0

Hi --

On Sun, 27 Jul 2008, Phlip wrote:

> Marc Heiler wrote:
>
>>> Read my verbiage again.
>>
>> Not trying to nitpick but I agree with Robert Dober about the formulation:
>>
>>>> If you called aidy() twice, the constant would re-assign, and it therefor
>>>> would not be constant.
>
> The quote depends on magically suspending the Ruby error message. Hence it
> does not say out-of-the-box Ruby cannot reassign.
>
> (We get reassignments, with their warnings, all the time in Rails, partly due
> to its Magic Loader facility...)

The magic loading shouldn't lead to those errors. It only does the
magic loading for unresolved constant references, and once it loads
the necessary file, any further references to the constant won't be
unresolved.

Maybe you've got some rogue "load" commands somewhere.


David

--
Rails training from David A. Black and Ruby Power and Light:
* Advancing With Rails August 18-21 Edison, NJ
* Co-taught by D.A. Black and Erik Kastner
See http://www.r... for details and updates!

Robert Dober

7/27/2008 11:25:00 AM

0

On Sat, Jul 26, 2008 at 6:18 PM, Phlip <phlip2005@gmail.com> wrote:
> Robert Dober wrote:
>
>>>> Ruby expects to assign constants only once. If you called aidy() twice,
>>>> the
>>>> constant would re-assign, and it therefor would not be constant.
>>>>
>>> Excellant explanation.
>>
>> But not quite correct, constants *can* be reassigned all you get is a
>> warning.
>
> Read my verbiage again.
Sorry if I misunderstood, I have misinterpreted your sentence "it
would not be a constant anymore".
Well it is not a constant.
That was what I meant with not quite correct, I should have used "I do
not completely agree" maybe.
R.


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

There's no one thing that's true. It's all true.
--
Ernest Hemingway