Module: Enumerable

Included in:
Color::Palette::AdobeColor, Color::Palette::Gimp, OpenSSL::Buffering, REXML::AttlistDecl, REXML::Elements, REXML::Parent, REXML::SyncEnumerator, Range, Set, StringInput
Defined in:
lib/extensions/set/set.rb,
lib/framework/builtinME.rb,
lib/extensions/rhoxml/rexml/set.rb

Instance Method Summary collapse

Instance Method Details

#all?(&proc) ⇒ Boolean

Returns:

  • (Boolean)


181
182
183
184
185
# File 'lib/framework/builtinME.rb', line 181

def all?(&proc)
    proc = lambda { |obj| obj } unless block_given?
    each { |obj| return false unless proc.call(obj) }
    true
end

#any?(&proc) ⇒ Boolean

Returns:

  • (Boolean)


187
188
189
190
191
# File 'lib/framework/builtinME.rb', line 187

def any?(&proc)
    proc = lambda { |obj| obj } unless block_given?
    each { |obj| return true if proc.call(obj) }
    false
end

#collectObject Also known as: map



193
194
195
196
197
# File 'lib/framework/builtinME.rb', line 193

def collect
    arr = []
    each{|obj| arr << yield(obj)}
    return arr
end

#detect(ifnone = nil) ⇒ Object Also known as: find



127
128
129
130
# File 'lib/framework/builtinME.rb', line 127

def detect(ifnone = nil)
    each { |obj| return obj if yield(obj) }
    ifnone.call if ifnone
end

#each_with_indexObject



134
135
136
137
# File 'lib/framework/builtinME.rb', line 134

def each_with_index 
    i = 0;
    each {|x| yield x, i; i = i + 1}
end

#find_allObject Also known as: select



108
109
110
111
112
113
114
115
116
# File 'lib/framework/builtinME.rb', line 108

def find_all
    a = []
    each {|x|
        if yield x
            a.push(x)
        end
    }
    return a
end

#group_by(&block) ⇒ Object



215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
# File 'lib/framework/builtinME.rb', line 215

def group_by(&block)
    res = {}
    self.each do |obj|
        group = yield(obj)
        values = res[group]
        if values.nil?
            values = [obj]
            res[group] = values
        else
            values << obj
        end
    end
    
    res
end

#inject(*args) ⇒ Object



166
167
168
169
170
171
172
173
174
175
176
177
178
179
# File 'lib/framework/builtinME.rb', line 166

def inject(*args)
    if args.size == 0 then
        vals = to_a
        memo = vals[0]
        vals[1..vals.size-1].each {|obj| memo = yield(memo, obj)}
        return memo
    elsif args.size == 1 then
        memo = args[0]
        each {|obj| memo = yield(memo, obj)}
        return memo
    else
        nil
    end
end

#max(&proc) ⇒ Object



201
202
203
204
205
206
# File 'lib/framework/builtinME.rb', line 201

def max(&proc)
    proc = lambda { |a, b| a <=> b } unless block_given?
    max = nil
    each {|obj| max = obj if max.nil? || proc.call(max, obj) < 0}
    max
end

#member?(other) ⇒ Boolean Also known as: include?

Returns:

  • (Boolean)


159
160
161
162
# File 'lib/framework/builtinME.rb', line 159

def member?(other)
    each{|obj| return true if obj == other }
    return false
end

#min(&proc) ⇒ Object



208
209
210
211
212
213
# File 'lib/framework/builtinME.rb', line 208

def min(&proc)
    proc = lambda { |a, b| a <=> b } unless block_given?
    min = nil
    each {|obj| min = obj if min.nil? || proc.call(min, obj) > 0}
    min
end

#sort(&proc) ⇒ Object



147
148
149
150
151
152
153
154
155
156
157
# File 'lib/framework/builtinME.rb', line 147

def sort(&proc)
    #proc = lambda{ |a,b| a<=>b } unless block_given?
    arr = to_a
    arr.sort{|x,y| 
        if block_given?  then
            proc.call(x,y)
        else
        x<=>y
        end
    }
end

#sort_by(&block) ⇒ Object



120
121
122
123
124
125
# File 'lib/framework/builtinME.rb', line 120

def sort_by &block
    array_of_tuples = []
    each {|x| array_of_tuples.push([x, yield(x)])}
    array_of_tuples = array_of_tuples.sort {|x, y| x[1] <=> y[1]}
    return array_of_tuples.collect {|x| x[0]}
end

#to_aObject Also known as: entries



139
140
141
142
143
# File 'lib/framework/builtinME.rb', line 139

def to_a
    arr = []
    each{|obj| arr <<obj}
    return arr
end

#to_set(klass = Set, *args, &block) ⇒ Object

Makes a set from the enumerable object with given arguments. Needs to require “set” to use this method.



622
623
624
# File 'lib/extensions/set/set.rb', line 622

def to_set(klass = Set, *args, &block)
  klass.new(self, *args, &block)
end