Skip to content

Instantly share code, notes, and snippets.

@arekt
Created September 6, 2012 14:28
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 arekt/3656849 to your computer and use it in GitHub Desktop.
Save arekt/3656849 to your computer and use it in GitHub Desktop.
to_proc
class Filter
def initialize()
@constraints = []
end
def constraint(&block)
@constraints << block
end
def to_proc
lambda { |e| @constraints.all? {|fn| fn.call(e) }}
end
end
if __FILE__ == $0
filter = Filter.new
filter.constraint {|x| x > 10 }
filter.constraint {|x| x.even? }
p (8..34).select(&filter)
p (8..34).select(&filter.to_proc)
puts ['a','b','c'].map(&:upcase)
class Symbol
def to_proc
lambda { |e| puts "You want me to do #{self}. hahaha no way" }
end
end
puts ['a','b','c'].map(&:upcase)
end
~
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment