31e2b397e15442a2d3ba42db7bd4584e

Here's the long way of writing what I want:
subscriptions[:free] = SubscriptionType.make(:free)
subscriptions[:lite] = SubscriptionType.make(:lite)
subscriptions[:brisk] = SubscriptionType.make(:brisk)
subscriptions[:jammin] = SubscriptionType.make(: jammin)

Please see how I currently do this below, but I'd rather not initialize an empty hash first. Any suggestions? Thanks!

@subscriptions  = {}
[:free, :lite, :brisk, :jammin].each { |sub_type| subscriptions[sub_type] = SubscriptionType.make(sub_type) }

Refactorings

No refactoring yet !

880cbab435f00197613c9cc2065b4f5a

danielharan

August 3, 2009, August 03, 2009 20:32, permalink

No rating. Login to rate!

Use inject({}). Technically you're still initializing an empty hash, but it's more idiomatic.

31e2b397e15442a2d3ba42db7bd4584e

chipcastle.myopenid.com

August 4, 2009, August 04, 2009 01:42, permalink

No rating. Login to rate!

Of course! inject is definitely the way to go. Here's what I ended up with:

@subscriptions = [:free, :lite, :brisk, :jammin].inject({}) {|result, ele| result[ele] = SubscriptionType.make(ele); result }
880cbab435f00197613c9cc2065b4f5a

danielharan

August 4, 2009, August 04, 2009 03:55, permalink

No rating. Login to rate!

Hmm... that's a bit long. I prefer splitting it up for readability

@subscriptions = [:free, :lite, :brisk, :jammin].inject({}) do |result, ele|
  result[ele] = SubscriptionType.make(ele)
  result
end
31e2b397e15442a2d3ba42db7bd4584e

Chip Castle

August 4, 2009, August 04, 2009 13:56, permalink

No rating. Login to rate!

I agree. Thanks for the suggestions!

B94d3947daf98d3d6c0dd5b50215e4af

Vidar Hokstad

August 5, 2009, August 05, 2009 20:01, permalink

No rating. Login to rate!

Here's an alternative using Hash[]

@subscriptions = Hash[*[:free, :lite, :brisk, :jammin].collect {|ele| [ele, SubscriptionType.make(ele)] }.flatten]
F30b04ed4d0b3fc4bc791a28815f34ca

ReinH

September 9, 2009, September 09, 2009 18:35, permalink

No rating. Login to rate!

One option is to make a factory Hash. More a design alternative than a strict refactoring.

@subscriptions = Hash.new {|h,k| h[k] = SubscriptionType.make(k)}
4cff8f078a502344a716cd62641ea081

Pharmd189

November 3, 2009, November 03, 2009 16:14, permalink

No rating. Login to rate!

Very nice site!

Very nice site!
376b85c3100967d100d0a64076a4de05

OvXjWl

November 18, 2009, November 18, 2009 22:00, permalink

No rating. Login to rate!

Hi! UZWEDf

Hi! UZWEDf
975cd989cc04976864d822751c559eee

sNjhqbc

November 19, 2009, November 19, 2009 19:36, permalink

No rating. Login to rate!

Hi! ZnEHzrwV

Hi! ZnEHzrwV
1955678407fb79869b3da8179b83d786

IAEohm

December 22, 2009, December 22, 2009 13:58, permalink

No rating. Login to rate!

Hi! LPvbIn

Hi! LPvbIn
64d8b9a14f491d246b0396d048e1ab8e

cytotec

March 18, 2011, March 18, 2011 13:32, permalink

No rating. Login to rate!

Hello!

Hello!

Your refactoring





Format Copy from initial code

or Cancel