Created
March 23, 2012 15:02
-
-
Save apirak/2171493 to your computer and use it in GitHub Desktop.
Meta programming example with currency expert
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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