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 !
Simulacre
May 21, 2010, May 21, 2010 05:06, permalink
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 )
Sam Warmuth
May 28, 2010, May 28, 2010 04:09, permalink
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 )
Sam Warmuth
May 28, 2010, May 28, 2010 04:15, permalink
Or, if you're golfing...
params = {:a => "a", :b => "thisisb", :c => "c", :d => "thiswasb", :e => "34" }
foo(params.select{|k| (:a..:c).include?(k)})
I only want the keys :a, :b, :c to be passed into the function foo