I have a question about best practices for Ruby variable-scope using class_eval.
In this code, in the class_eval block, the local variables first and second are undefined.
def parent_method(opts={})
first = opts[:percent]
second = (10 * first).to_i
SecondClass.class_eval do
def second_method; return {:a => first, :b => second}; end;
end
end
This appears to be a scoping issue, because the only way I’ve found to get this to work is to make first and second class-level variables:
def parent_method(opts={})
@@first = opts[:percent]
@@second = (10 * @@first).to_i
SecondClass.class_eval do
def second_method; return {:a => @@first, :b => @@second}; end;
end
end
- What is the best practice for this scenario? I realize globals are a poor choice for this scenario, because of the wide scope that results, however, are class-level variables similarly frowned upon?
- Aside from dealing with variable scope, are there any other ways to address the accessibility of these variables from within
class_eval?
The problem is not because of
class_eval, but because ofdef. When you define a method that way, it introduces a new scope. Here is the best way to fix it: