Ruby Inject

Weird behavior of inject() method in Ruby: fruits = %w{apples oranges bananas} str = "" fruits.inject { |str, x| str << x } p str p fruits Produces: "applesorangesbananas"   # Correct ["applesorangesbananas", "oranges", "bananas"]   # Wrong Why did it modify the original array? The inject should only be working on str. To produce the... Continue Reading →

Advertisements

Blog at WordPress.com.

Up ↑