Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Ruby Module with Class and Instance methods extended
module Persistence
def self.included(klass)
klass.extend(ClassMethods)
end
module ClassMethods
def all
puts 'all'
end
def find(id)
puts "looking for entity with id=#{id}"
end
end
def save
puts 'saving'
end
def update
puts 'updating entity'
end
end
class User
include Persistence
end
u = User.new
# instance methods
u.save #=> saving
u.update #=> updating entity
# class methods
User.all #=> all
User.find(1) #=> looking for entity with id=1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.