My attempt in Ruby.