Skip to content

Instantly share code, notes, and snippets.

@bill-transue
Created October 26, 2012 16:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bill-transue/3959906 to your computer and use it in GitHub Desktop.
Save bill-transue/3959906 to your computer and use it in GitHub Desktop.
Is this dumb?
def add_rotator_to(hash)
hash.define_singleton_method :rotator do
inject({}) do |rotated, key_value|
if key_value.first =~ /_rotate/
rotated.merge key_value.first[0..-8].to_sym => self[key_value.first].rotate!.first
else
rotated.merge key_value.first => key_value.last
end
end
end
end
>> a = { :foo => "bar", :baz_rotate => [ "que", "quz" ] }
=> {:foo=>"bar", :baz_rotate=>["que", "quz"]}
>> add_rotator_to a
=> #<Proc:0x007fd91fcbc640@/Users/bill/Projects/strands/spec/factories.rb:32 (lambda)>
>> a.rotator
=> {:foo=>"bar", :baz=>"quz"}
>> a
=> {:foo=>"bar", :baz_rotate=>["quz", "que"]}
>> a.rotator
=> {:foo=>"bar", :baz=>"que"}
>> a
=> {:foo=>"bar", :baz_rotate=>["que", "quz"]}
>>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment