I know that each_with_objectdoes not work with immutable objects like integer.
for instance (1..3).each_with_object(0) {|i,sum| sum += i} #=> 0
We can fix this by injection, and the amount will be 6. Fine!
Take another example
("a".."c").each_with_object("") {|i,str| str += i} # => ""
This will not work, because when we do str+=i, we create a new object
Instead we need to do
("a".."c").each_with_object("") {|i,str| str << i} # => "abc"
Why does this happen, when I do the injectfollowing two ways, I get strange results
("a".."c").inject("") {|i,str| str += i}
("a".."c").inject("") {|i,str| str << i}
Why is the first print abcin reverse order? And why the second mistake?
, .each_with_object , inject (, integer),
.inject , .each_with_object? - ?