Skip to content

Instantly share code, notes, and snippets.

@yangsu
Last active December 11, 2015 20:08
Show Gist options
  • Save yangsu/4652898 to your computer and use it in GitHub Desktop.
Save yangsu/4652898 to your computer and use it in GitHub Desktop.
Ruby method_missing
class Roman
def self.method_missing name, *args
roman = name.to_s
roman.gsub!("IV", "IIII")
roman.gsub!("IX", "VIIII")
roman.gsub!("XL", "XXXX")
roman.gsub!("XC", "LXXXX")
(
roman.count("I") +
roman.count("V") * 5 +
roman.count("X") * 10 +
roman.count("L") * 50 +
roman.count("C") * 100
)
end
end
puts Roman.X # 10
puts Roman.XC # 90
puts Roman.XII # 12
puts Roman.IX # 9
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment