Skip to content

Instantly share code, notes, and snippets.

@jqr
Created April 2, 2009 20:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save jqr/89410 to your computer and use it in GitHub Desktop.
Save jqr/89410 to your computer and use it in GitHub Desktop.
class FilterableEnumerable
def initialize(original)
@original = original
end
def >(value)
@original.select { |v| v > value }
end
def >=(value)
@original.select { |v| v >= value }
end
def <(value)
@original.select { |v| v < value }
end
def <=(value)
@original.select { |v| v <= value }
end
def ==(value)
@original.select { |v| v == value }
end
end
module Enumerable
def >(value)
select { |v| v > value }
end
def <(value)
select { |v| v < value }
end
def filter
FilterableEnumerable.new(self)
end
end
a = (1..10).to_a
a.filter >= 5 # => [5, 6, 7, 8, 9, 10]
a.filter > 5 # => [6, 7, 8, 9, 10]
a.filter < 5 # => [1, 2, 3, 4]
a.filter <= 5 # => [1, 2, 3, 4, 5]
a.filter == 5 # => [5]
# and the problem case:
a.filter != 5 # => false
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment