Duncan Beevers
Posts: 68
Nickname: dbeevers
Registered: May, 2008
|
Duncan Beevers is a hack and jester building shiny baubles from bytes and tears.
|
|
|
|
Hash key rewrite
|
Posted: Aug 12, 2008 8:34 PM
|
|
|
This post originated from an RSS feed registered with Ruby Buzz
by Duncan Beevers.
|
Original Post: Hash key rewrite
Feed Title: dweebd
Feed URL: http://www.dweebd.com/feed/
Feed Description: Ruby ejecta from the leading edge
|
Latest Ruby Buzz Posts
Latest Ruby Buzz Posts by Duncan Beevers
Latest Posts From dweebd
|
|
Props to Jeremy Voorhis for this cool and simple Hash maneuver.
class Hash
def rewrite mapping
inject({}) do |rewritten_hash, (original_key, value)|
rewritten_hash[mapping.fetch(original_key, original_key)] = value
rewritten_hash
end
end
end
Example usage:
h = { :human => 'squishy', :robot => 'tinny' }
h.rewrite(:human [...]
Read: Hash key rewrite
|
|