Skip to content

Instantly share code, notes, and snippets.

@apirak
Created March 23, 2012 15:02
Show Gist options
  • Save apirak/2171493 to your computer and use it in GitHub Desktop.
Save apirak/2171493 to your computer and use it in GitHub Desktop.
Meta programming example with currency expert
currency = {usd: 1, baht: 30.73, yen: 83.447, rupee: 50.21}
[Float, Fixnum].each do |number|
currency.each do |key, value|
number.class_eval "def #{key};
return self/#{value};
end"
number.class_eval %{def #{key}_string;
return (self * #{value}).to_s+" #{key.upcase}";
end}
end
end
puts 3.methods.grep(/usd/)
puts (3.usd + 20.baht).usd_string
puts (3.usd + 20.baht).baht_string
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment