Skip to content

Instantly share code, notes, and snippets.

@anthonylewis
Created July 18, 2013 04:17
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anthonylewis/6026685 to your computer and use it in GitHub Desktop.
Save anthonylewis/6026685 to your computer and use it in GitHub Desktop.
An example using class instance variables
class Record
def self.before_save(*methods)
@callbacks ||= []
@callbacks += methods
end
def self.callbacks
@callbacks
end
def save
self.class.callbacks.each do |c|
send c if respond_to? c
end
puts "Saved"
end
end
class User < Record
before_save :saving
def saving
puts "Saving..."
end
end
u = User.new
u.save
@stagrlee
Copy link

from class, this might be more readable for beginners..

send self.c if self.respond_to? c

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment