Skip to content

Instantly share code, notes, and snippets.

@timmatheson
Created April 20, 2010 09:18
Show Gist options
  • Save timmatheson/372226 to your computer and use it in GitHub Desktop.
Save timmatheson/372226 to your computer and use it in GitHub Desktop.
# 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