You can get closer:
bar, bar2 = h.values_at :foo, :foo2
Or, I assume that we could extend the Hash to extract into instance variables:
class Hash def extract o each { |k, v| o.instance_variable_set '@' + k.to_s, v } end end h.extract self p [@foo, @foo2]
source share