Begin forwarded message:
On Sep 8, 2007, at 1:59 PM, James Edward G. II wrote:
Begin forwarded message:
I would appreciate any
comments improving my idiomatic ruby.
Some thoughts…
letters = {}
letters.default = 0
That could be:
letters = Hash.new(0)
#put letters in a hash
s = sentence.gsub(/[^A-Z]/,’’)
s.split(’’).each do |letter|
letters[letter] = letters[letter] + 1
end
Instead of gsub() and split(), you could use scan(). You can take
another shortcut here with +=:
sentence.scan(/[A-Z]/) { |l| letters[l] += 1 }
print i.to_s + “.”
We generally favor interpolation in Ruby so we can avoid these
casting calls:
print “#{i}.”
Great solution though. Thanks for sending it in.
James Edward G. II