Class: Hash
Overview
Problem: the Hash::slice method in ActiveSupport works a little differently than in Facets. And because Facets is overrides ActiveSupport version this causes problems (for example in to_json method) In ActiveSupport it returns :nil if there’s no key in Facets it raises error.
Use Case: hash = “slug”=>“item-z8w”, “name”=>“Item”, “created_at”=>“2011-01-10T07:04:01Z”, “dependent”=>“false”, “viewers”=>[“manager”, “user:user1”], “updated_at”=>“2011-01-10T07:04:01Z”, :icon_url=>“null”, “text”=>“null”, “tags”=>[], “owner_name”=>“user1”, “_type”=>“Selector” args = [“id”, “_type”, “slug”, “name”, “text”, “tags”, “dependent”, “owner_name”, “viewers”, “created_at”, “updated_at”] hash.slice *args
Solution: Redefining :slice the same way as in ActiveSupport.
Direct Known Subclasses
Instance Method Summary collapse
Instance Method Details
#slice(*keep_keys) ⇒ Object
15 16 17 18 19 20 21 22 |
# File 'lib/rad/support/hacks_and_fixes.rb', line 15 def slice(*keep_keys) hash = {} keep_keys.each do |key| # hash[key] = fetch(key) hash[key] = self[key] end hash end |