Skip to content

Instantly share code, notes, and snippets.

@phppirate
Forked from eladmeidar/casey_redis.rb
Created October 29, 2013 21:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save phppirate/7223080 to your computer and use it in GitHub Desktop.
Save phppirate/7223080 to your computer and use it in GitHub Desktop.
# config/initializers/redis.rb
require 'rubygems'
require 'redis'
REDIS = Redis.new()
class User
attr_accessor :name, :id
def initialize(name, id)
@name = name
@id = id
end
def like_recipe(recipe)
REDIS.sadd "user:#{self.id}:liked_recipes", recipe.id
end
def has_in_common_with_user?(another_user)
REDIS.sinter("user:#{self.id}:liked_recipes", "user:#{another_user.id}:liked_recipes").size > 0
end
def liked_recipes
REDIS.smembers "user:#{self.id}:liked_recipes"
end
end
class Recipe
attr_accessor :name, :id, :stuff_in_in
def initialize(name, id, stuff_in_it = [])
@name = name
@id = id
@stuff_in_it = []
end
end
r1 = Recipe.new("Spaghetti", 1, ["tomato", "onion", "meat"])
r2 = Recipe.new("Pizza", 2, ["tomato", "onion", "flour"])
u1 = User.new("elad", 1)
u2 = User.new("casey", 2)
u1.like_recipe(r1)
u1.like_recipe(r2)
u2.like_recipe(r2)
puts "user #{u1.name} likes: #{u1.liked_recipes}"
puts "user #{u2.name} likes: #{u2.liked_recipes}"
puts "#{u1.name} has recipes in common with #{u2.name}: #{u1.has_in_common_with_user?(u2)}"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment