Solution in Ruby