Ruby inject with initial being a hash

后端 未结 3 571
囚心锁ツ
囚心锁ツ 2020-12-12 15:19

Can any one tell me why the following:

[\'a\', \'b\'].inject({}) {|m,e| m[e] = e }

throws the error:

IndexError: string no         


        
3条回答
  •  无人及你
    2020-12-12 15:26

    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.

提交回复
热议问题