Quiet new to ruby I can't figure out something. Here's a Sample code
class Big
def self.metaclass; class << self; self; end; end
def self.convertor b
metaclass.instance_eval do
define_method( :convert ) do |val|
return b val
end
end
end
end
class Small < Big
convertor { |v| v.to_i + 1 }
end
puts Small.convert('18')
The aim is to have a lot of subclass to Big and i like to avoid to define in each
def convert(val)
return conversion_specific_to_subclass(val)
end
Doing the former way i just have one line for each subclass. But can't get it to work. What is it i'm doing wrong? Is there a better way to accomplish what i wish?
Thanks in advance
edit: As asked here are the errors this code produce (with ruby 2.1.0)
test2.rb:4:in `convertor': wrong number of arguments (0 for 1) (ArgumentError)
from test2.rb:14:in `<class:Small>'`