What i bascially want is to extend the Numeric class so that it has one extra Attribute (currencie), which is set when of the undefined methods are invoked [yen(s), euro(s), etc.]
So, here is the class definition:
class Numeric
@@currencies = {'yen' => 0.013, 'euro' => 1.292, 'rupee' => 0.019, 'dollar' => 1}
attr_accessor :currencie
def method_missing(method_id)
singular_currency = method_id.to_s.gsub( /s$/, '')
if @@currencies.has_key?(singular_currency)
self.currencie = singular_currency
self * @@currencies[singular_currency]
puts "method finished"
else
super
end
end
def in(convert_to)
end
end
Now, when i run the code
a = 5.rupees
puts "currencie is -> " + a.currencie
i’ve got:
method finished
/path_to_file/hw2.1.rb:33:in `<main>': undefined method `currencie' for nil:NilClass (NoMethodError)
Also the attribute currencie seems to be unset.
What am i doing wrong ?
In your case
method_missingshould return object i.e.self. Just addselftomethod_missingand it will work.EDIT: Fixed as
injektsaid