Additional Helper Methods (except, with, only) in spec_helper.rb file in project_folder/spec/
class Hash
# filter key out of a hash
# {:a=>1, :b=> 2, :c=> 3}.except(:a)
# results of hash after call {:b=> 2, :c=> 3 }
def except(*keys)
self.reject { |k,v| keys.include?(k || k.to_sym) }
end
# override some keys with a new value
# {:a=>1, :b=> 2, :c=> 3}.with(:a => 4)
# results of hash after call {:a => 4, :b=> 2, c: => 3 }
def with(overrides = {})
self.merge overrides
end
# return a hash with only the pairs identified by the +keys+
# { :a=>1, :b=>2, :c=>3}.only(:a)
# results of hash after call {:a=>1}
def only(*keys)
self.reject { |k,v| !keys.include?(k || k.to_sym ) }
end
end
What is a proposition?
vor 15 Jahren
Keine Kommentare:
Kommentar veröffentlichen