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