Skip to content

Instantly share code, notes, and snippets.

@blairanderson
Created November 13, 2018 21:00
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 blairanderson/6c87b500412fe28e8772a3ea80044534 to your computer and use it in GitHub Desktop.
Save blairanderson/6c87b500412fe28e8772a3ea80044534 to your computer and use it in GitHub Desktop.
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