How does enum#with_object differ from enum#each_with_object?

允我心安 提交于 2019-12-12 15:08:50

问题


The documentation says:

enum#each_with_object :-

Iterates the given block for each element with an arbitrary object, obj, and returns obj

enum#with_object:-

Iterates the given block for each element with an arbitrary object, obj, and returns obj

But when I tried the below on the both constructs, one gave me the output as expected but the others didn't. So I suspect there is a difference between those two constructs.

Using each_with_object

%w(foo bar).each_with_object({}) { |str, hsh| hsh[str] = str.upcase }
=> {"foo"=>"FOO", "bar"=>"BAR"}

success here!

Using with_object

%w(foo bar).with_object({}) { |str, hsh| hsh[str] = str.upcase }
 => NoMethodError: undefined method `with_object' for ["foo", "bar"]:Array
    from (irb):1
    from C:/Ruby193/bin/irb:12:in `<main>'

failed here!

So what is the difference between these two methods?


回答1:


each returns an Enumerator object.

%w(foo bar).each.class
=> Enumerator

So, for the first case, the array'll be converted to Enumerator first, then works on the with_object.

If you want the second case works, you have to convert the array to Enumerator. You can use .to_enum, .each, or .map to convert the array.

%w(foo bar).map.with_object({}) { |str, hsh| hsh[str] = str.upcase }
=> {"foo"=>"FOO", "bar"=>"BAR"}

More details: Enumerator




回答2:


with object only works on enumerators, which means that you have to chain it on to something that returns one. eg.

%w(foo bar).each.with_object({}) { |str, h| h[str] = str.upcase }

%w(foo bar).detect.with_object(obj) { ... }

So, you can call with_object on anything that returns an enumerator if you don't give it a block (such as map, reduce, detect, find_all...). This includes anything that mixes in Enumerable.

each_with_object is essentially an alias for each.with_object.



来源:https://stackoverflow.com/questions/14671881/how-does-enumwith-object-differ-from-enumeach-with-object

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!