R
Robert Klemme
Eric said:because you can get a handle on that rebound Proc. You might
want to pass it around or whatever.
I see. Although I don't have a use case for this at hand and in fact
never missed that. But that might be just my personal experience.
When do you think will unbind_locals be useful?
As a replacement for many string evals - which are ugly,
inefficient, and possibly dangerous. Many (most?) times that
you need to eval a string it is because you need to pull in
some local variables to help define the string to be evaled.
Here is the first example of a string eval in the 1.8 library I
found:
for element in %w[ HTML HEAD BODY P PLAINTEXT DT DD
LI OPTION tr th td ]
methods += <<-BEGIN + nO_element_def(element) + <<-END
def #{element.downcase}(attributes = {})
BEGIN
end
END
end
eval(methods)
This could be replaced by:
for element in %w[ HTML HEAD BODY P PLAINTEXT DT DD
LI OPTION tr th td ]
define_method(element.downcase.to_sym ,
proc { |attributes={}|
nO_element_def(element)
}.unbind_locals # replace element with constant
)
end
Much cleaner, huh?
Not really (at least to my eyes). Also, there are some issues:
- I don't know what nO_element_def does exactly, but it will have to be
rewritten to not create a string with ruby code
- Your version might be less efficient.
- There might be subtle differences because "element" is pulled into the
closure
- Also, unbind_locals will remove "element" from the procs bindings
Transferring locals might be pretty easy, but transferring the
meaning of self would be more difficult, I think. At least
without making a new Binding (and then you'd still need a way
to rebind it to the original proc).
Why do you think that self is special? If there is a general mechanism to
transfer state (i.e. bindings) into a binding, any variable can be
rebound. I imagine something like
proc.binding.bindself => whatever, :foo => "bar")
eval("self", proc.binding) # -> whatever
eval("foo", proc.binding) # -> "bar"
Interesting. I assumed that since
class_eval/instance_eval/module_eval all returned the same
"self" that they did the same thing. The only difference I see
is in "def <method> ...". With class_eval, it defines instance
methods and with instance_eval, it defines class methods.
That's kind of strange. Some kind of magic is going on here
other than the changing of self.
Definitely.
I was hoping that instance_eval could be used to define class
methods using #define_method, but #define_method does the same
with both - defines instance methods. Anybody know of an
equivalent to #define_method for making class methods? I
couldn't figure out any way to define a class method from a
proc - just out of curiosity.
Since a class method is just an instance method of the class:
?> Foo.bar
=> "bar"
Thanks for the interesting exchange!
Kind regards
robert