-
-
Save technicalpickles/89491 to your computer and use it in GitHub Desktop.
Revisions
-
technicalpickles revised this gist
Apr 2, 2009 . 1 changed file with 53 additions and 14 deletions.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -6,29 +6,38 @@ def initialize(original) 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 def method_missing(sym, *args, &block) @original.select {|v| v.send(sym, *args, &block) } end def respond_to?(sym, include_private = false) @original.all? {|v| v.respond_to?(sym, include_private) } end end module Enumerable def >(value) select { |v| v > value } end def <(value) select { |v| v < value } end @@ -37,14 +46,44 @@ def filter FilterableEnumerable.new(self) end end require 'test/unit' class TestFilterableEnumerable < Test::Unit::TestCase def setup @array = (0..10).to_a end def test_gte assert_equal [5, 6, 7, 8, 9, 10], @array.filter >= 5 end def test_gt assert_equal [6, 7, 8, 9, 10], @array.filter > 5 end def test_lte assert_equal [0, 1, 2, 3, 4], @array.filter < 5 end def test_le assert_equal [0, 1, 2, 3, 4, 5], @array.filter <= 5 end def test_eq assert_equal [5], @array.filter == 5 end def test_neq assert_equal [0, 1, 2, 3, 4, 6, 7, 8, 10], @array.filter != 5 end def test_method_missing assert_equal [0], @array.filter.zero? end def test_respond_to assert @array.filter.respond_to?(:zero?) end end -
jqr revised this gist
Apr 2, 2009 . 1 changed file with 3 additions and 0 deletions.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -45,3 +45,6 @@ def filter 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 -
jqr revised this gist
Apr 2, 2009 . 1 changed file with 10 additions and 1 deletion.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -7,10 +7,18 @@ 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 @@ -28,11 +36,12 @@ def <(value) 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] -
jqr revised this gist
Apr 2, 2009 . 1 changed file with 26 additions and 2 deletions.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -1,4 +1,21 @@ 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 end module Enumerable def >(value) select { |v| v > value } @@ -7,8 +24,15 @@ def >(value) def <(value) select { |v| v < value } end def filter FilterableEnumerable.new(self) end end a = (1..10).to_a a.filter > 5 # => [6, 7, 8, 9, 10] a.filter < 5 # => [1, 2, 3, 4] a.filter == 5 # => [5] -
jqr created this gist
Apr 2, 2009 .There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,14 @@ # Yes this is a terrible idea, but we're just getting started... module Enumerable def >(value) select { |v| v > value } end def <(value) select { |v| v < value } end end a = (1..10).to_a a > 5 # => [6, 7, 8, 9, 10]