Abinoam,
thank you for get the benchmarks.
I guess the different of spped from generating objects.
They generate minimal objects which Object#tap and Enumerable#each_with_obj=
ect.
---------------------------------------------------------------------------=
----
Ralph,
I like #1 for comprehensibility meaning

# looks "construct Hash instance from inner subscript"
Hash[]

# looks "collect the own values"
a.group_by{|n|n}

# counting
map{|k, v|[k, v.size]}]

# if Hash has own map
https://gist.github.com/1626034
---------------------------------------------------------------------------=
----
I like #2 for comprehensibility, clear name-space, and speed meanings

# comprehensibility
# looks like "list comprehension"
tap{|h|a.each{|n|h[n] +=3D 1}}

# clear name-space
no make variables for out of block

# speed meanings
look Abinoam's benchmarks

# if Enumerable has method for this case
https://gist.github.com/1625981

2012/1/17 Ralph Shnelvar <ralphs / dos32.com>:
> Kenichi,
>
> Monday, January 16, 2012, 9:21:51 AM, you wrote:
>
>
>
> KK> 2012/1/17 Ralph Shnelvar <ralphs / dos32.com>:
>>> a =3D [4,5,6,4,5,6,6,7]
>>>
>>> result =3D Hash.new(0)
>>> a.each { |x| =A0result[x] +=3D 1 }
>>>
>>> p result
>>>
>>>
>>>
>>> The result I am getting
>>> =A0 {4=3D>2, 5=3D>2, 6=3D>3, 7=3D>1}
>>> is what I want.
>>>
>>> Is there a better way; perhaps using uniq?
>>>
>>>
>
>
> KK> I like this
>
> KK> a =3D [4,5,6,4,5,6,6,7]
>
> KK> # 1
> KK> p Hash[a.group_by{|n|n}.map{|k, v|[k, v.size]}]
>
> KK> # 2
> KK> p Hash.new(0).tap{|h|a.each{|n|h[n] +=3D 1}}
>
>
> I like #2. =A0I can understand it. =A0I'm still having trouble wrapping m=
y head around #1.
>
>
> Having said that, is your #2 better than mine in any dimension (comprehen=
sibility and/or speed of execution?
>
>
>

--=20
Kenichi Kamiya