Tagged Ruby

There's an increasing interest with the D programming language amongst my readers so I figured I'll post a bunch of short posts about D and see what happens.

Anyway, here's a classic example showing Ruby's capabilities taken from Seven Languages in Seven Weeks:

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
puts Roman.XC
puts Roman.XII

Continue reading