On 24 Jul 2010, at 17:06, Will Jessop wrote:
> Must. Resist. Golf.
>
> Oh well:
>
> properties = [:a, :b, :c]
> attributes = properties.inject({}) {|hash, e| hash.merge!({e => hash.size+1})
> }
> p.attributes
>
> => {:a=>1, :b=>2, :c=>3}
Oh that's just too easy to golf. Counting the hash size means you no longer
need to access the array within the block.
[:a, :b, :c].inject({}) {|hash, e| hash.merge!({e => hash.size+1}) } # =>
{:c=>3, :b=>2, :a=>1}
C
---
Caius Durling
[email protected]
+44 (0) 7960 268 100
http://caius.name/
--
You received this message because you are subscribed to the Google Groups
"NWRUG" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to
[email protected].
For more options, visit this group at
http://groups.google.com/group/nwrug-members?hl=en.