Created
April 20, 2010 09:18
-
-
Save timmatheson/372226 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
| # Adds a convienence method to the Hash class | |
| # Allows you to check if a hash has a valid key and value. | |
| class Hash | |
| def assert_valid_key?(key) | |
| self.symbolize_keys.has_key?(key.to_sym) && !self[key].blank? | |
| end | |
| end | |
| # Test it | |
| require 'spec_helper' | |
| describe Hash do | |
| it "should have a valid key" do | |
| {:valid_key => "YES"}.assert_valid_key?(:valid_key).should == true | |
| end | |
| it "should not have a valid key" do | |
| {}.assert_valid_key?(:bs_key).should == false | |
| end | |
| end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment