Module: EnumerableWithArgs
- Included in:
- Interval
- Defined in:
- lib/carat/enumerable-args.rb
Overview
EnumerableWithArgs
This is a simple reimplementation of the core Enumerable module to allow the methods to take and pass-on arbitrary arguments to the underlying #each call.
Synopsis
require 'carat/enumerable-args'
class T
include Enumerable
def initialize(arr)
@arr = arr
end
def each(n)
arr.each{ |e| yield(e+n) }
end
end
t = T.new([1,2,3])
t.collect(4)
#=> [5,6,7]
Note
The #sort method still needs to be done.
Legal
Copyright (c) 2004 Thomas Sawyer, Pancakes License
History
$Id: enumerable.rb,v 1.0 2004/11/30 transami Exp $
Instance Method Summary collapse
-
#collect(*args) ⇒ Object
(also: #map)
:yield:.
-
#detect(*args) ⇒ Object
(also: #find)
:yield:.
-
#each_slice(*args, &yld) ⇒ Object
(also: #each_by)
An additional method not part of standard Enumerable.
- #grep(pattern, *args) ⇒ Object
- #include?(anObj, *args) ⇒ Boolean (also: #member?)
- #max(*args) ⇒ Object
- #min(*args) ⇒ Object
- #reject(*args) ⇒ Object
-
#select(*args) ⇒ Object
(also: #find_all)
:yield:.
- #sort(*args) ⇒ Object
-
#to_a(*args) ⇒ Object
(also: #entries)
problematic for multiple arity on block def each_with_index(*args) i=0 each(*args){ |n,i| yield(n); i+=1 } self end.
Instance Method Details
#collect(*args) ⇒ Object Also known as: map
:yield:
43 44 45 46 47 |
# File 'lib/carat/enumerable-args.rb', line 43 def collect(*args) # :yield: a = [] each(*args){ |n| a << yield(n) } a end |
#detect(*args) ⇒ Object Also known as: find
:yield:
50 51 52 53 |
# File 'lib/carat/enumerable-args.rb', line 50 def detect(*args) # :yield: each(*args){ |n| return n if yield(n) } nil end |
#each_slice(*args, &yld) ⇒ Object Also known as: each_by
An additional method not part of standard Enumerable. The regular version of this method can be found in Facets, but it is a bit more advanced then this one. At some point they need to be put into sync.
74 75 76 77 78 79 80 81 82 83 84 85 |
# File 'lib/carat/enumerable-args.rb', line 74 def each_slice(*args, &yld) a = []; s = [] ar = yld.arity.abs each(*args){ |n| s << n if s.length >= ar yld.call(*s) s = [] end } a end |
#grep(pattern, *args) ⇒ Object
95 96 97 98 99 |
# File 'lib/carat/enumerable-args.rb', line 95 def grep(pattern, *args) a = [] each(*args){ |n| a << (block_given? ? yield(n) : n) if pattern === n } a end |
#include?(anObj, *args) ⇒ Boolean Also known as: member?
101 102 103 104 |
# File 'lib/carat/enumerable-args.rb', line 101 def include?(anObj, *args) each(*args){ |n| return true if anObj == n } false end |
#max(*args) ⇒ Object
107 108 109 |
# File 'lib/carat/enumerable-args.rb', line 107 def max(*args) to_a(*args).max end |
#min(*args) ⇒ Object
111 112 113 |
# File 'lib/carat/enumerable-args.rb', line 111 def min(*args) to_a(*args).min end |
#reject(*args) ⇒ Object
115 116 117 118 119 |
# File 'lib/carat/enumerable-args.rb', line 115 def reject(*args) a = [] each(*args){ |n| a << n if ! yield(n) } a end |
#select(*args) ⇒ Object Also known as: find_all
:yield:
88 89 90 91 92 |
# File 'lib/carat/enumerable-args.rb', line 88 def select(*args) # :yield: a = [] each(*args){ |n| a << n if yield(n) } a end |
#sort(*args) ⇒ Object
121 122 123 |
# File 'lib/carat/enumerable-args.rb', line 121 def sort(*args) # TODO end |
#to_a(*args) ⇒ Object Also known as: entries
problematic for multiple arity on block def each_with_index(*args)
i=0
each(*args){ |n,i| yield(n); i+=1 }
self
end
63 64 65 66 67 |
# File 'lib/carat/enumerable-args.rb', line 63 def to_a(*args) a = [] each(*args){ |n| a << n } a end |