Warning modifying constant & 'global constant'

  1. if someone attempts to modify a constant, why does ruby choose to
    emit a warning instead of a fatal error?

  2. is there a beast called a ‘global constant’? i tried defining $Foo
    and ruby allows me to rebind $Foo later.

thanks,

···


dave

Hi,

  1. if someone attempts to modify a constant, why does ruby choose to
    emit a warning instead of a fatal error?

If it is a fatal error, something like Ruby embedded editor will meet
serious problems, e.g.

M-x eval-ruby-expression
Eval: Foo = 42
bang!

  1. is there a beast called a ‘global constant’? i tried defining $Foo
    and ruby allows me to rebind $Foo later.

No. $Foo is a plain global variable.

						matz.
···

In message “warning modifying constant & ‘global constant’” on 02/08/26, David Garamond davegaramond@icqmail.com writes:

Yukihiro Matsumoto wrote:

  1. is there a beast called a ‘global constant’? i tried defining $Foo
    and ruby allows me to rebind $Foo later.

No. $Foo is a plain global variable.

thanks matz.

i want to protect some global variables without having to resort to
setting $SAFE to 4. can we use some proxying mechanism, or override an
object method to trap assignment/binding? freeze() only protect the
object value, and does not prevent rebinding.

···


dave

What problems would there be with making it an exception?

I would hope that the embedded editor would already handle exceptions
properly.

Paul

···

On Mon, Aug 26, 2002 at 06:49:55PM +0900, Yukihiro Matsumoto wrote:

  1. if someone attempts to modify a constant, why does ruby choose to
    emit a warning instead of a fatal error?

If it is a fatal error, something like Ruby embedded editor will meet
serious problems, e.g.

M-x eval-ruby-expression
Eval: Foo = 42
bang!

i want to protect some global variables without having to resort to
setting $SAFE to 4. can we use some proxying mechanism, or override an
object method to trap assignment/binding? freeze() only protect the
object value, and does not prevent rebinding.

ruby has internally read only variables, for example :

pigeon% ruby -e '$LOAD_PATH = "aaa"'
-e:1: can't set variable $LOAD_PATH (NameError)
pigeon%

but

pigeon% ruby -e '$LOAD_PATH << "aaa"'
pigeon%

well,

pigeon% ruby -e '$LOAD_PATH.freeze; $LOAD_PATH << "aaa"'
-e:1:in `<<': can't modify frozen array (TypeError)
        from -e:1
pigeon%

but

pigeon% ruby -e '$LOAD_PATH.freeze; $LOAD_PATH[0] << "aaa"'
pigeon%

etc

Guy Decoux

Kernel.trace_var?

chandler@greyhound:~$ irb
irb(main):001:0> trace_var :$global, proc{|v| $global = ‘foo’}
nil
irb(main):002:0> $global = ‘pet’
“pet”
irb(main):003:0> p $global
“foo”
nil

Perhaps make Globals class and make all the variables as instance or class
variables with only attr_reader set or only getter methods?

-michael

···

On Monday 26 August 2002 05:11, David Garamond wrote:

Yukihiro Matsumoto wrote:

  1. is there a beast called a ‘global constant’? i tried defining $Foo
    and ruby allows me to rebind $Foo later.

No. $Foo is a plain global variable.

thanks matz.

i want to protect some global variables without having to resort to
setting $SAFE to 4. can we use some proxying mechanism, or override an
object method to trap assignment/binding? freeze() only protect the
object value, and does not prevent rebinding.

++++++++++++++++++++++++++++++++++++++++++
Michael C. Libby x@ichimunki.com
public key: http://www.ichimunki.com/public_key.txt
web site: http://www.ichimunki.com
++++++++++++++++++++++++++++++++++++++++++

“David Garamond” wrote in

thanks matz.

i want to protect some global variables without having to resort to
setting $SAFE to 4. can we use some proxying mechanism, or override an
object method to trap assignment/binding? freeze() only protect the
object value, and does not prevent rebinding.

Actually freezing a class as a similar effect on its
class, local variables and constants

···

class A
@@var = 3
@var = 2
Var =1
freeze

for example

p @@var # raises a TypeError …
end

/Christoph

Hi,

···

In message “Re: warning modifying constant & ‘global constant’” on 02/08/27, Paul Brannan pbrannan@atdesk.com writes:

M-x eval-ruby-expression
Eval: Foo = 42
bang!

What problems would there be with making it an exception?

for example, eval-current-buffer may cause trouble if it redefines
contants.

						matz.