How do I define virtual global variable in ruby?

F

femto Zheng

[Note: parts of this message were removed to make it a legal post.]

Hello all,
in ruby c source code, there is
<http://codesearch.google.com/codese.../re.c&q=define_virtual&sa=N&cd=1&ct=rc&l=3369>rb_define_virtual_variable
like
rb_*define_virtual*_variable("$~", match_getter,
match_setter);<http://codesearch.google.com/codese.../re.c&q=define_virtual&sa=N&cd=1&ct=rc&l=3369>

3370: rb_*define_virtual*_variable("$&", last_match_getter, 0);
<http://codesearch.google.com/codese.../re.c&q=define_virtual&sa=N&cd=1&ct=rc&l=3370>
3371: rb_*define_virtual*_variable("$`", prematch_getter, 0);
<http://codesearch.google.com/codese.../re.c&q=define_virtual&sa=N&cd=1&ct=rc&l=3371>
3372: rb_*define_virtual*_variable("$'", postmatch_getter, 0);
<http://codesearch.google.com/codese.../re.c&q=define_virtual&sa=N&cd=1&ct=rc&l=3372>
3373: rb_*define_virtual*_variable("$+", last_paren_match_getter,
0); <http://codesearch.google.com/codese.../re.c&q=define_virtual&sa=N&cd=1&ct=rc&l=3373>
3374: <http://codesearch.google.com/codese.../re.c&q=define_virtual&sa=N&cd=1&ct=rc&l=3374>
3375: rb_*define_virtual*_variable("$=", ignorecase_getter,
ignorecase_setter);
<http://codesearch.google.com/codese.../re.c&q=define_virtual&sa=N&cd=1&ct=rc&l=3375>
3376: rb_*define_virtual*_variable("$KCODE", kcode_getter,
kcode_setter); <http://codesearch.google.com/codese.../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/codese.../re.c&q=define_virtual&sa=N&cd=1&ct=rc&l=3377>
3378: <http://codesearch.google.com/codese.../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.
 
R

Ryan Davis

Hello all,
in ruby c source code, there is
[...]
3370: rb_*define_virtual*_variable("$&", last_match_getter, 0);
= <http://codesearch.google.com/codesearch/p?hl=3Den#kOEgDIzD-Ao/trunk/lib/r=
uby/re.c&q=3Ddefine_virtual&sa=3DN&cd=3D1&ct=3Drc&l=3D3370>
[...]
=20
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?

Without using a hacky trick like calling rb_define_virtual_variable =
through FFI or DL, I don't think you can.
 
R

Robert Klemme

Hello all,
in ruby c source code, there is
<http://codesearch.google.com/codese.../re.c&q=define_virtual&sa=N&cd=1&ct=rc&l=3369>rb_define_virtual_variable
like
rb_*define_virtual*_variable("$~", match_getter,
match_setter);<http://codesearch.google.com/codese.../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
 
B

Brian Candler

In addition to what Robert said, maybe const_missing would be useful to
you.
=> Wed Dec 15 15:07:28 +0000 2010

Constants scoped to modules are IMO less evil than globals.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,048
Latest member
verona

Latest Threads

Top