Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > ||= [] idiom

Reply
Thread Tools

||= [] idiom

 
 
Leslie Viljoen
Guest
Posts: n/a
 
      02-19-2008
I often make use of this idiom to add something to an array in a hash of arrays:

@categories = {}

pagelist.each do |resource|
@categories[resource.tag] ||= []
@categories[resource.tag] << resource
end


.. is there a better way? Can the two @categories lines
be made into one?


Les

 
Reply With Quote
 
 
 
 
Sebastian Hungerecker
Guest
Posts: n/a
 
      02-19-2008
Leslie Viljoen wrote:
> @categories =3D {}
>
> pagelist.each do |resource|
> =A0 =A0 @categories[resource.tag] ||=3D []
> =A0 =A0 @categories[resource.tag] << resource
> end
>
>
> .. is there a better way?


@categories =3D Hash.new {Array.new}
pagelist.each do |resource|
@categories[resource.tag] << resource
end



=2D-=20
Jabber: http://www.velocityreviews.com/forums/(E-Mail Removed)
ICQ: 205544826

 
Reply With Quote
 
 
 
 
MenTaLguY
Guest
Posts: n/a
 
      02-19-2008
On Wed, 20 Feb 2008 07:11:31 +0900, "Leslie Viljoen" <(E-Mail Removed)> wrote:
> I often make use of this idiom to add something to an array in a hash of
> arrays:
>
> @categories = {}
>
> pagelist.each do |resource|
> @categories[resource.tag] ||= []
> @categories[resource.tag] << resource
> end
>
> .. is there a better way? Can the two @categories lines
> be made into one?


I sometimes do this sort of thing:

( @categories[resource.tag] ||= [] ) << resource

Although this might possibly be more readable:

category = ( @categories[resource.tag] ||= [] )
category << resource

Often it can make sense to factor the ||= portion into a
separate method:

def category_for(tag)
@categories[tag] ||= []
end

# ...

category_for(resource.tag) << resource

-mental


 
Reply With Quote
 
Joel VanderWerf
Guest
Posts: n/a
 
      02-19-2008
Sebastian Hungerecker wrote:
> Leslie Viljoen wrote:
>> @categories = {}
>>
>> pagelist.each do |resource|
>> @categories[resource.tag] ||= []
>> @categories[resource.tag] << resource
>> end
>>
>>
>> .. is there a better way?

>
> @categories = Hash.new {Array.new}
> pagelist.each do |resource|
> @categories[resource.tag] << resource
> end


That one still bites me from time to time...

irb(main):001:0> h = Hash.new {Array.new}
=> {}
irb(main):002:0> h[4]
=> []
irb(main):003:0> h[4] << 5
=> [5]
irb(main):004:0> h
=> {}


Try this...

irb(main):005:0> h = Hash.new {|h,k| h[k]=Array.new}
=> {}
irb(main):006:0> h[4] << 5
=> [5]
irb(main):007:0> h
=> {4=>[5]}

--
vjoel : Joel VanderWerf : path berkeley edu : 510 665 3407

 
Reply With Quote
 
William James
Guest
Posts: n/a
 
      02-19-2008
On Feb 19, 4:11 pm, Leslie Viljoen <(E-Mail Removed)> wrote:
> I often make use of this idiom to add something to an array in a hash of arrays:
>
> @categories = {}
>
> pagelist.each do |resource|
> @categories[resource.tag] ||= []
> @categories[resource.tag] << resource
> end
>
> . is there a better way? Can the two @categories lines
> be made into one?
>
> Les


irb(main):001:0> h = Hash.new { |hash, key| hash[key] =[] }
=> {}
irb(main):002:0> h['foo'] << 33
=> [33]
irb(main):003:0> h['foo'] << 44
=> [33, 44]
irb(main):004:0> h
=> {"foo"=>[33, 44]}
 
Reply With Quote
 
MenTaLguY
Guest
Posts: n/a
 
      02-19-2008
On Wed, 20 Feb 2008 07:15:52 +0900, Sebastian Hungerecker <(E-Mail Removed)> wrote:
>> .. is there a better way?

>
> @categories = Hash.new {Array.new}
> pagelist.each do |resource|
> @categories[resource.tag] << resource
> end


Although it is specific to hashes, when this is applicable I do
think it can be a better approach than the more general approach
I outlined in my other email.

-mental


 
Reply With Quote
 
Jos Backus
Guest
Posts: n/a
 
      02-19-2008
On Wed, Feb 20, 2008 at 07:11:31AM +0900, Leslie Viljoen wrote:
> I often make use of this idiom to add something to an array in a hash of arrays:
>
> @categories = {}
>
> pagelist.each do |resource|
> @categories[resource.tag] ||= []
> @categories[resource.tag] << resource
> end


@categories = {}

pagelist.each do |resource|
(@categories[resource.tag] ||= []) << resource
end

or

@categories = Hash.new {|h, k| h[k] = []}

pagelist.each do |resource|
@categories[resource.tag] << resource
end

--
Jos Backus
jos at catnook.com

 
Reply With Quote
 
Christopher Swasey
Guest
Posts: n/a
 
      02-19-2008
[Note: parts of this message were removed to make it a legal post.]

On 2/19/08, Leslie Viljoen <(E-Mail Removed)> wrote:
>
> I often make use of this idiom to add something to an array in a hash of
> arrays:
>
> @categories = {}
>
> pagelist.each do |resource|
> @categories[resource.tag] ||= []
> @categories[resource.tag] << resource
> end
>
>
> .. is there a better way? Can the two @categories lines
> be made into one?



@categories = {}

pagelist.each do |resource|
(@catetegories[resource.tag] ||= []) << resource
end



Christopher

 
Reply With Quote
 
Joel VanderWerf
Guest
Posts: n/a
 
      02-19-2008
MenTaLguY wrote:
> Often it can make sense to factor the ||= portion into a
> separate method:
>
> def category_for(tag)
> @categories[tag] ||= []
> end
>
> # ...
>
> category_for(resource.tag) << resource


That's something I've used frequently. It's kind of a poor man's
dependency injection.

--
vjoel : Joel VanderWerf : path berkeley edu : 510 665 3407

 
Reply With Quote
 
s.ross
Guest
Posts: n/a
 
      02-19-2008
If you don't mind being really cryptic about it:

@categories = resource.inject({}) {|a, v| (a[v.tag] ||= []) << v; a}

I think this works, but I'm not sure the "next guy" would ever figure
out what it does

On Feb 19, 2008, at 2:11 PM, Leslie Viljoen wrote:

> I often make use of this idiom to add something to an array in a
> hash of arrays:
>
> @categories = {}
>
> pagelist.each do |resource|
> @categories[resource.tag] ||= []
> @categories[resource.tag] << resource
> end
>
>
> .. is there a better way? Can the two @categories lines
> be made into one?
>
>
> Les


If you don't mind being really cryptic about it:

@categories = resource.inject({}) {|a, v| (a[v.tag] ||= []) << v; a}

I think this works, but I'm not sure the "next guy" would ever figure
out what it does


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Which exception idiom do you prefer? stevengarcia@yahoo.com Java 6 02-28-2006 06:39 PM
Simplified DOM idiom for building XML - revisited Steve Jorgensen XML 4 08-28-2005 08:54 PM
Idiom Question - Finding Instances Rhino Java 5 02-18-2004 05:36 AM
Pimpl Idiom Icosahedron C++ 7 11-22-2003 11:34 PM
Re: Extendable envelope/letter idiom Ivan Vecerina C++ 0 06-24-2003 01:32 PM



Advertisments