Synonyms

Time to play with synonyms. Let me know which versions you like better.

result = 0
0.upto(arr.length) do |i|
  result += arr[i]
end

or

arr.inject{|x,y| x + y}

How about this one?

0.upto(arr.length) do |i|
  arr[i] = 10+arr[i]
end

or

arr.map! {|x| 10 + x}

Higher-order procedures are h0t!

3 Comments »

  1. jmontesi said,

    February 28, 2006 @ 12:17 pm

    The last one. Short and sweet. Good job.

  2. Mike said,

    March 13, 2006 @ 1:17 pm

    Have you redefined collect in your install, was that an intended inject call, or something entirely different?

  3. Administrator said,

    March 16, 2006 @ 6:36 pm

    Good call, Mike. I must have been playing fast and loose w/ my keyboard. It’s now fixed to be an inject.

RSS feed for comments on this post · TrackBack URI

Leave a Comment