How do I define virtual global variable in ruby?

Hello all,
in ruby c source code, there is
http://codesearch.google.com/codesearch/p?hl=en#kOEgDIzD-Ao/trunk/lib/ruby/re.c&q=define_virtual&sa=N&cd=1&ct=rc&l=3369rb_define_virtual_variable
like
rb_define_virtual_variable(“$~”, match_getter,
match_setter);http://codesearch.google.com/codesearch/p?hl=en#kOEgDIzD-Ao/trunk/lib/ruby/re.c&q=define_virtual&sa=N&cd=1&ct=rc&l=3369

3370: rb_define_virtualvariable(“$&”, last_match_getter, 0);
http://codesearch.google.com/codesearch/p?hl=en#kOEgDIzD-Ao/trunk/lib/ruby/re.c&q=define_virtual&sa=N&cd=1&ct=rc&l=3370
3371: rb
define_virtualvariable(“$`”, prematch_getter, 0);
http://codesearch.google.com/codesearch/p?hl=en#kOEgDIzD-Ao/trunk/lib/ruby/re.c&q=define_virtual&sa=N&cd=1&ct=rc&l=3371
3372: rb
define_virtualvariable(“$'”, postmatch_getter, 0);
http://codesearch.google.com/codesearch/p?hl=en#kOEgDIzD-Ao/trunk/lib/ruby/re.c&q=define_virtual&sa=N&cd=1&ct=rc&l=3372
3373: rb
define_virtualvariable(“$+”, last_paren_match_getter,
0);
http://codesearch.google.com/codesearch/p?hl=en#kOEgDIzD-Ao/trunk/lib/ruby/re.c&q=define_virtual&sa=N&cd=1&ct=rc&l=3373
3374:
http://codesearch.google.com/codesearch/p?hl=en#kOEgDIzD-Ao/trunk/lib/ruby/re.c&q=define_virtual&sa=N&cd=1&ct=rc&l=3374
3375: rb
define_virtualvariable(“$=”, ignorecase_getter,
ignorecase_setter);
http://codesearch.google.com/codesearch/p?hl=en#kOEgDIzD-Ao/trunk/lib/ruby/re.c&q=define_virtual&sa=N&cd=1&ct=rc&l=3375
3376: rb
define_virtualvariable(“$KCODE”, kcode_getter,
kcode_setter);
http://codesearch.google.com/codesearch/p?hl=en#kOEgDIzD-Ao/trunk/lib/ruby/re.c&q=define_virtual&sa=N&cd=1&ct=rc&l=3376
3377: rb
define_virtual_variable(“$-K”, kcode_getter,
kcode_setter);
http://codesearch.google.com/codesearch/p?hl=en#kOEgDIzD-Ao/trunk/lib/ruby/re.c&q=define_virtual&sa=N&cd=1&ct=rc&l=3377
3378:
http://codesearch.google.com/codesearch/p?hl=en#kOEgDIzD-Ao/trunk/lib/ruby/re.c&q=define_virtual&sa=N&cd=1&ct=rc&l=3378

so actually this looks like global variable, but when you get/set it,
it will run thru a function,
How do I do it in Ruby code? make a global variable running through my
customized get/set procedure?
Thanks.

On Dec 15, 2010, at 00:07 , femto Zheng wrote:

customized get/set procedure?
Without using a hacky trick like calling rb_define_virtual_variable
through FFI or DL, I don’t think you can.

On Wed, Dec 15, 2010 at 9:07 AM, femto Zheng [email protected] wrote:

Hello all,
in ruby c source code, there is

http://codesearch.google.com/codesearch/p?hl=en#kOEgDIzD-Ao/trunk/lib/ruby/re.c&q=define_virtual&sa=N&cd=1&ct=rc&l=3369rb_define_virtual_variable

like
rb_define_virtual_variable(“$~”, match_getter,

match_setter);http://codesearch.google.com/codesearch/p?hl=en#kOEgDIzD-Ao/trunk/lib/ruby/re.c&q=define_virtual&sa=N&cd=1&ct=rc&l=3369

so actually this looks like global variable, but when you get/set it,
it will run thru a function,

They are not global variables, they are more restricted: these
variables are local to the current stack frame, which also makes them
thread local at the same time. You can see it here:

irb(main):001:0> def a
irb(main):002:1> /x+/ =~ “xxxxxyyyyy”
irb(main):003:1> p $&
irb(main):004:1> b
irb(main):005:1> end
=> nil
irb(main):006:0> def b
irb(main):007:1> p $&
irb(main):008:1> end
=> nil
irb(main):009:0> a
“xxxxx”
nil
=> nil
irb(main):010:0>

How do I do it in Ruby code? make a global variable running through my
customized get/set procedure?

What do you want to achieve? Why do you think you need this? You can
always do

class X
def foo=(val)
printf “Setting foo to %p\n”, val
end

def foo
puts “Getting foo”
123
end
end

irb(main):011:0> $GlobalX = X.new
=> #<X:0x1003cd44>
irb(main):012:0> $GlobalX.foo = “hello”
Setting foo to “hello”
=> “hello”

Apart from that global state is always dangerous and should be avoided
if possible.

Kind regards

robert

In addition to what Robert said, maybe const_missing would be useful to
you.

module Foo
def self.const_missing(name)
Time.now
end
end
=> nil

Foo::Bar
=> Wed Dec 15 15:07:26 +0000 2010

Foo::Bar
=> Wed Dec 15 15:07:28 +0000 2010

Constants scoped to modules are IMO less evil than globals.