Global variable and dynamic binding: question

CONTEXT:
I am working on some code for dynamic binding in Ruby.
binding (what_to_bind_and_to_what) {
# all code here (nested calls included) sees bindings above
}
# bindings revert here to previous values
# code here sees bindings that existed prior to the binding(…) above.

However, instead of the usual practice of binding a variable:
binding (:var, value) {
# all code here (nested calls included) sees :var as bound to value
}

I want to bind an attribute of an object
binding (object, accessor, value) {
# all code here (nested calls included) sees object.accessor ==
value
}

Reasoning: Ruby is all objects. References to most (all?) non-local
variables should correspond to attribute access on some object, if you treat
things like hash[key] as sugar for hash.get_attr(key).

QUESTION:
Do global variables in Ruby correspond to attributes on some object? Is
there a way to get hold of that object and find or define accessors for
those variables? Most specifically, if that object is referred to as
globals, is there a way to define:
def globals.get_attr (global_symbol)
and
def globals.set_attr (global_symbol, value)

Thanks!

“Its Me” itsme213@hotmail.com schrieb im Newsbeitrag
news:VLzec.19317$jR6.10718@fe2.texas.rr.com

CONTEXT:
I am working on some code for dynamic binding in Ruby.
binding (what_to_bind_and_to_what) {
# all code here (nested calls included) sees bindings above
}
# bindings revert here to previous values
# code here sees bindings that existed prior to the binding(…)
above.

However, instead of the usual practice of binding a variable:
binding (:var, value) {
# all code here (nested calls included) sees :var as bound to
value
}

I want to bind an attribute of an object
binding (object, accessor, value) {
# all code here (nested calls included) sees object.accessor ==
value
}

Reasoning: Ruby is all objects. References to most (all?) non-local
variables should correspond to attribute access on some object, if you
treat
things like hash[key] as sugar for hash.get_attr(key).

QUESTION:
Do global variables in Ruby correspond to attributes on some object? Is
there a way to get hold of that object and find or define accessors for
those variables? Most specifically, if that object is referred to as
globals, is there a way to define:
def globals.get_attr (global_symbol)
and
def globals.set_attr (global_symbol, value)

Thanks!

Hm, there is Kernel.global_variables() but no getter or setter, which you
could override. Do I miss something here?

Regards

robert

Robert Klemme wrote:

“Its Me” itsme213@hotmail.com schrieb im Newsbeitrag

QUESTION:
Do global variables in Ruby correspond to attributes on some object? Is
there a way to get hold of that object and find or define accessors for
those variables? Most specifically, if that object is referred to as
globals, is there a way to define:
def globals.get_attr (global_symbol)
and
def globals.set_attr (global_symbol, value)

Thanks!

Hm, there is Kernel.global_variables() but no getter or setter, which you
could override. Do I miss something here?

Would it help to hack together an object that has attrs corresponding to
global vars?

GLOBALS = Object.new

class << GLOBALS
def method_missing(m, *args, &bl)
case m.to_s
when /^(\w+)=$/
var = $1
if global_variables.include?( “$#{var}” )
instance_eval %{
def self.#{var}=(arg)
$#{var} = arg
end
}
end
self.send m, args[0]

 when /^\w+$/
   if global_variables.include?( "$#{m}" )
     instance_eval %{
       def self.#{m}
         $#{m}
       end
     }
   end
   self.send m

 else
   super
 end

end
end

$test = 3
p GLOBALS.test
GLOBALS.test = 5
p GLOBALS.test

This would require all references to those global vars to change (to
GLOBALS.var, or GLOBALS[var]). I was hoping to avoid such a change, but some
variant of this may be a fall back:

My approach is simple:

def binding (object, accessor, value, &block)
object.with(accessor, value, &block)
end

class Object
def with (attr, val)
begin
temp = self.get_attr(attr)
self.set_attr(attr, val)
yield
ensure
self.set_attr(attr, temp)
end
end
def get_attr(attr); self.send(attr); end
def set_attr(attr, val); self.send(attr.to_s + ‘=’, val); end
end

Hash, Array etc. can override get_attr and set_attr

And then some overrides for global variables …

class << theMissingGlobalObject
def get_attr (global_symbol)
some_getter_function (global_symbol)
end
def set_attr (global_symbol, value)
some_setter_function (global_symbol, value)
end
end

So I’m looking for theMissingGlobalObject. If there is no such thing I’d be
interested to know why; perhaps there is some very good reason, like some
tricky circularity with a global object holding on to global vars?

···

“Joel VanderWerf” vjoel@PATH.Berkeley.EDU wrote

Would it help to hack together an object that has attrs corresponding to
global vars?