I am trying to use class_eval to create a method metaprogrammatically if the method that's called starts with "plus". However, I'm having trouble putting together the actual syntax of the class_eval
class Adder
def initialize(my_num)
@my_num = my_num
end
def my_num
@my_num
end
end
def method_missing(meth, *args)
my_meth = meth.to_s
#puts my_meth[0, 4]
if my_meth[0, 4] == "plus" then #/plus\d/ then
num = my_meth.slice(/\d+/).to_i
original_num = self.my_num.to_i
my_sum = original_num + num
class_eval{ eval{"def #{meth}; @my_int = #{my_sum} return @my_int end\n"}}
end
else
super
end
y = Adder.new(12)
puts y.plus10
When the plus10 (or whatever number) is called, the newly created method should add that number to the integer that's being called on, and produce the new sum.
Thanks
Try this:
UPDATE
and this is slightly improved version: