Ruby的一些疑问
1、网点看到一断程序,
class Filter def initialize @constraints = [] end def constraint(&block) @constraints << block end def to_proc lambda { |e| @constraints.all? { |fn| fn.call(e) } } endendfilter = Filter.newfilter.constraint { |x| x > 10 }filter.constraint { |x| x.even? }filter.constraint { |x| x % 3 == 0 }p (8..24).select(&filter) #=> [12,18,24]