Class: Array

Inherits:
Object show all
Defined in:
lib/hash-utils/array.rb

Overview

Array extension.

Instance Method Summary collapse

Instance Method Details

#array?Boolean



263
264
265
# File 'lib/hash-utils/array.rb', line 263

def array?
    true
end

#avgObject Also known as: average



246
247
248
# File 'lib/hash-utils/array.rb', line 246

def avg
    self.sum.to_f / self.length
end

#clean(value = nil) ⇒ Object



278
279
280
# File 'lib/hash-utils/array.rb', line 278

def clean(value = nil)
    self.reject { |v| v === value }
end

#clean!(value = nil) ⇒ Object



293
294
295
# File 'lib/hash-utils/array.rb', line 293

def clean!(value = nil)
    self.reject! { |v| v === value }
end

#eighthObject



194
195
196
# File 'lib/hash-utils/array.rb', line 194

def eighth
    self[7]
end

#fifthObject



155
156
157
# File 'lib/hash-utils/array.rb', line 155

def fifth
    self[4]
end

#fourthObject



142
143
144
# File 'lib/hash-utils/array.rb', line 142

def fourth
    self[3]
end

#merge!(*arrays) ⇒ Object



101
102
103
104
105
# File 'lib/hash-utils/array.rb', line 101

def merge!(*arrays)
    arrays.flatten! 1
    arrays.each { |i| self << i }
    self
end

#remove!(&block) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/hash-utils/array.rb', line 25

def remove!(&block)
    result = [ ]
    self.reject! do |v|
        if block.call(v)
            result << v
            true
        else
            false
        end
    end
    
    return result
end

#secondObject



116
117
118
# File 'lib/hash-utils/array.rb', line 116

def second
    self[1]
end

#seventhObject



181
182
183
# File 'lib/hash-utils/array.rb', line 181

def seventh
    self[6]
end

#sixthObject



168
169
170
# File 'lib/hash-utils/array.rb', line 168

def sixth
    self[5]
end

#sumObject



215
216
217
# File 'lib/hash-utils/array.rb', line 215

def sum
    self.inject(:+)
end

#thirdObject



129
130
131
# File 'lib/hash-utils/array.rb', line 129

def third
    self[2]
end

#to_h(mode = nil) ⇒ Object



75
76
77
78
79
80
81
# File 'lib/hash-utils/array.rb', line 75

def to_h(mode = nil)
    if mode == :flat
        Hash[*self]
    else
        Hash[self]
    end
end

#to_setObject



306
307
308
# File 'lib/hash-utils/array.rb', line 306

def to_set
    Set::new(self)
end