Can any one tell me why the following:
[\'a\', \'b\'].inject({}) {|m,e| m[e] = e }
throws the error:
IndexError: string no
Rather than using inject, you should look into Enumerable#each_with_object.
Where inject requires you to return the object being accumulated into, each_with_object does it automatically.
From the docs:
Iterates the given block for each element with an arbitrary object given, and returns the initially given object.
If no block is given, returns an enumerator.
e.g.:
evens = (1..10).each_with_object([]) {|i, a| a << i*2 }
#=> [2, 4, 6, 8, 10, 12, 14, 16, 18, 20]
So, closer to your question:
[1] pry(main)> %w[a b].each_with_object({}) { |e,m| m[e] = e }
=> {"a"=>"a", "b"=>"b"}
Notice that inject and each_with_object reverse the order of the yielded parameters.