Module: EnumerableArgs

Included in:
Interval
Defined in:
lib/mega/enumerable_args.rb

Overview

:title: EnumerableArgs

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 'enumerable_args'

class T
  include EnumerableArgs
  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]

Author(s)

Thomas Sawyer

Constant Summary collapse

VERSION =
'0.9.0'

Instance Method Summary collapse

Instance Method Details

#collect(*args) ⇒ Object Also known as: map

:yield:



59
60
61
62
63
# File 'lib/mega/enumerable_args.rb', line 59

def collect(*args)  # :yield:
  a = []
  each(*args){ |n| a << yield(n) }
  a
end

#detect(*args) ⇒ Object Also known as: find

:yield:



66
67
68
69
# File 'lib/mega/enumerable_args.rb', line 66

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.



90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/mega/enumerable_args.rb', line 90

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



111
112
113
114
115
# File 'lib/mega/enumerable_args.rb', line 111

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?

Returns:

  • (Boolean)


117
118
119
120
# File 'lib/mega/enumerable_args.rb', line 117

def include?(anObj, *args)
  each(*args){ |n| return true if anObj == n }
  false
end

#max(*args) ⇒ Object



123
124
125
# File 'lib/mega/enumerable_args.rb', line 123

def max(*args)
  to_a(*args).max
end

#min(*args) ⇒ Object



127
128
129
# File 'lib/mega/enumerable_args.rb', line 127

def min(*args)
  to_a(*args).min
end

#reject(*args) ⇒ Object



131
132
133
134
135
# File 'lib/mega/enumerable_args.rb', line 131

def reject(*args)
  a = []
  each(*args){ |n| a << n if ! yield(n) }
  a
end

#select(*args) ⇒ Object Also known as: find_all

:yield:



104
105
106
107
108
# File 'lib/mega/enumerable_args.rb', line 104

def select(*args)  # :yield:
  a = []
  each(*args){ |n| a << n if yield(n) }
  a
end

#sort(*args) ⇒ Object



137
138
139
# File 'lib/mega/enumerable_args.rb', line 137

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



79
80
81
82
83
# File 'lib/mega/enumerable_args.rb', line 79

def to_a(*args)
  a = []
  each(*args){ |n| a << n }
  a
end