On 24 Jul 2010, at 16:10, Ashley Moran wrote:
> This snippet:
>
> properties = [:a, :b, :c]
>
> counter = (1..properties.length).each
> attributes = properties.inject({}) { |attribs, property|
> attribs[property] = counter.next
> attribs
> }
>
> puts attributes
>
> produces this output:
>
> {:a=>1, :b=>2, :c=>3}
>
> But I'm sure you can do much better in Ruby 1.9. Anyone know how and wanna
> show me? :)
That's a bit long-winded, even in 1.8.7.
properties = [:a, :b, :c]
attributes = properties.inject({}) {|hash, e| hash[e] = properties.index(e)
+ 1; hash }
p attributes
__END__
{:b=>2, :c=>3, :a=>1}
Obviously it outputs an unordered hash, given it's 1.8.7.
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.