Created
May 16, 2012 14:04
-
-
Save rmw/2710573 to your computer and use it in GitHub Desktop.
Extend Ruby Hash with a method to return all keys as symbols
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Hash | |
def with_sym_keys | |
self.inject({}) { |memo, (k,v)| memo[k.to_sym] = v; memo } | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'spec_helper' | |
describe Hash do | |
describe "#with_sym_keys" do | |
it "should replace string keys with symbols" do | |
hash = { "a" => 1, "b" => 1 } | |
hash.with_sym_keys.should == { :a => 1, :b => 1 } | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
oooh. yes, much more readable. Thanks!