Why the following code issues an error ?
['hello','stack','overflow'].inject{|memo,s|memo+s.length}
TypeError: can't convert Fixnum into String
from (irb):2:in `+'
from (irb):2:in `block in irb_binding'
from (irb):2:in `each'
from (irb):2:in `inject'
from (irb):2
If the initial value is passed, it works OK:
['hello','stack','overflow'].inject(0){|memo,s|memo+s.length}
=> 18
You have the answer in apidock :
That is, without an initial value, you’re trying to do
'hello' + 'stack'.length