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 →


Create a free website or blog at

Up ↑