I've recently been learning more Ruby by doing problems on Code Abbey. The very first problems involve summing integers that are passed in via standard input. My original solution turned out to be as follows:
numbers = [] numberStrings = gets.chomp.split numberStrings.each {|number| numbers << number.to_i} sum = 0 numbers.each {|number| sum = sum + number} print "#{sum.to_s}\n"
And this is fine, in that it works. But were I to code this today it would simply read like this:
puts readline.chomp.split.map(&:to_i).reduce(:+).to_s
Of course it's the exact same thing conceptually, but Ruby-wise, it's a vast improvement.
So, have a look at some of your own code my friends. You may be pleasantly surprised at the progress you've made.
No comments:
Post a Comment