Class: Array

Inherits:
Object show all
Defined in:
lib/ruco/core_ext/array.rb,
lib/ruco/core_ext/array.rb,
lib/ruco/core_ext/array.rb

Overview

Direct Known Subclasses

Ruco::Position

Instance Method Summary collapse

Instance Method Details

#<(other) ⇒ Object



18
19
20
# File 'lib/ruco/core_ext/array.rb', line 18

def <(other)
  (self.<=>other) == -1
end

#<=(other) ⇒ Object



22
23
24
# File 'lib/ruco/core_ext/array.rb', line 22

def <=(other)
  self.<(other) or self.==other
end

#>(other) ⇒ Object



26
27
28
# File 'lib/ruco/core_ext/array.rb', line 26

def >(other)
  (self.<=>other) == 1
end

#>=(other) ⇒ Object



30
31
32
# File 'lib/ruco/core_ext/array.rb', line 30

def >=(other)
  self.>(other) or self.==other
end

#between?(a, b) ⇒ Boolean

Returns:

  • (Boolean)


14
15
16
# File 'lib/ruco/core_ext/array.rb', line 14

def between?(a,b)
  self.>= a and self.<= b
end

#map_with_index(&block) ⇒ Object



7
8
9
# File 'lib/ruco/core_ext/array.rb', line 7

def map_with_index(&block)
  dup.map_with_index!(&block)
end

#map_with_index!Object



3
4
5
# File 'lib/ruco/core_ext/array.rb', line 3

def map_with_index!
  each_with_index do |e, idx| self[idx] = yield(e, idx); end
end

#sum(method = nil, &block) ⇒ Object



37
38
39
40
41
42
43
44
45
46
# File 'lib/ruco/core_ext/array.rb', line 37

def sum(method = nil, &block)
  if block_given?
    raise ArgumentError, "You cannot pass a block and a method!" if method
    inject(0) { |sum, i| sum + yield(i) }
  elsif method
    inject(0) { |sum, i| sum + i.send(method) }
  else
    inject(0) { |sum, i| sum + i }
  end
end