Created
September 6, 2012 14:28
-
-
Save arekt/3656849 to your computer and use it in GitHub Desktop.
to_proc
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 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