Wednesday, July 11, 2012

Ruby and Memcached tricks

I love Ruby. It's a great language. I love almost everything about it (maybe besides the symbol syntax... but that's just me I guess).

Today I fell in love with the alias method :)
require 'memcached'
cache = Memcached.new "localhost:11211"
cache.set "message", "Hello, world!"
puts cache.get "message"
All is nice up to the get and set methods. Wouldn't it be nice to use the cache["message"] syntax?
class Memcached
  alias :"[]" :get
  alias :"[]=" :set
end
And then:
cache["message"] = "Hello"
puts cache["message"]

And live is good again :)

1 comment:

Matthias Hryniszak said...

Strange... Those aliases are there by default in Memcached::Rails... Why not in Memcached???