55502f40dc8b7c769880b10874abc9d0

I only want the keys :a, :b, :c to be passed into the function foo

params = {:a => "a", :b => "thisisb", :c => "c", :d => "thiswasb", :e => "34" }

opts = {}
opts[:a] = params[:a]
opts[:b] = params[:b]
opts[:c] = params[:c]

foo( opts )

Refactorings

No refactoring yet !

910faf70e5e167ccdd8ea18ce558aa15

Simulacre

May 21, 2010, May 21, 2010 05:06, permalink

No rating. Login to rate!
params = {:a => "a", :b => "thisisb", :c => "c", :d => "thiswasb", :e => "34" }

opts = { :a => nil, :b => nil, :c => nil}

opts.each_key {|k| opts[k] = params[k] }

foo( opts )
Cb9b01bd3f65603f3314b71b868a3703

Sam Warmuth

May 28, 2010, May 28, 2010 04:09, permalink

No rating. Login to rate!

Simpler.

params = {:a => "a", :b => "thisisb", :c => "c", :d => "thiswasb", :e => "34" }
save = [:a, :b, :c]

opts = params.select{|k| save.include?(k)}

foo( opts )
Cb9b01bd3f65603f3314b71b868a3703

Sam Warmuth

May 28, 2010, May 28, 2010 04:15, permalink

No rating. Login to rate!

Or, if you're golfing...

params = {:a => "a", :b => "thisisb", :c => "c", :d => "thiswasb", :e => "34" }

foo(params.select{|k| (:a..:c).include?(k)})

Your refactoring





Format Copy from initial code

or Cancel