Skip to content
This repository has been archived by the owner on Apr 19, 2023. It is now read-only.

Commit

Permalink
Handy aliases of get to [] and put to []=
Browse files Browse the repository at this point in the history
  • Loading branch information
jcrossley3 committed Mar 1, 2013
1 parent 0a8991a commit 8b4e273
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 0 deletions.
2 changes: 2 additions & 0 deletions gems/cache/lib/cache.rb
Expand Up @@ -141,11 +141,13 @@ def contains_key?( key )
def get(key)
decode(cache.get(encode(key)))
end
alias_method :[], :get

# Write an entry to the cache
def put(key, value, expires = 0)
__put(key, value, expires, :put)
end
alias_method :[]=, :put

def put_if_absent(key, value, expires = 0)
__put(key, value, expires, :put_if_absent)
Expand Down
7 changes: 7 additions & 0 deletions gems/cache/spec/cache_spec.rb
Expand Up @@ -43,6 +43,13 @@
@cache.get('foo').should == 'bar'
end

it "should work with index operators" do
@cache[:a] = 1
@cache[:a].should == 1
@cache[:b] = "two"
@cache[:b].should == "two"
end

it "should accept and return ruby objects" do
heffalump = Snuffleuffagus.new(100, 'snuffle')
@cache.put('heffalump', heffalump).should be_nil
Expand Down

0 comments on commit 8b4e273

Please sign in to comment.