This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'set' | |
class User | |
def initialize | |
@features = Set.new | |
end | |
def method_missing(meth, *args) | |
if meth.to_s =~ /^can_(.*)\?$/ | |
@features.include? $1.to_s |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module TrackMethods | |
def track(meth) | |
self.class_eval do | |
alias_method "old_#{meth}", meth | |
define_method meth do |*args| | |
puts "Calling #{meth} with #{args.join(', ')}" | |
self.send "old_#{meth}", *args | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Record | |
def self.before_save(*methods) | |
@callbacks ||= [] | |
@callbacks += methods | |
end | |
def self.callbacks | |
@callbacks | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module Memoize | |
def calc(n) | |
@@memo ||= {} | |
@@memo[n] ||= super | |
end | |
end | |
class Fibonacci | |
prepend Memoize |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
for i in 1..25 | |
puts g.next | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'generator' | |
g = Generator.new do |g| | |
n = 2 | |
p = [] | |
while true | |
if p.all? { |f| n % f != 0 } | |
g.yield n | |
p << n |