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

Returns:

  • (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

#complete_to(length, member = nil) ⇒ Object



482
483
484
# File 'lib/hash-utils/array.rb', line 482

def complete_to(length, member = nil)
    self.dup.complete_to!(length, member)
end

#complete_to!(length, member = nil) ⇒ Object



502
503
504
505
506
507
508
509
510
511
512
513
# File 'lib/hash-utils/array.rb', line 502

def complete_to!(length, member = nil)
    delta = length - self.length
    
    if delta > 0
        fill = [member] * delta 
        self.push(*fill) 
    elsif delta == 0
        self
    else
        self.shrink_to!(length)
    end
end

#cut(length) ⇒ Object



425
426
427
# File 'lib/hash-utils/array.rb', line 425

def cut(length)
    self.dup.cut! length
end

#cut!(length) ⇒ Object



400
401
402
403
404
405
406
407
408
409
# File 'lib/hash-utils/array.rb', line 400

def cut!(length)
    delta = self.length - length
    
    if delta >= 0
        self.slice! delta..(self.length)
        self
    else
        self.clear
    end
end

#drop!(length) ⇒ Object



572
573
574
575
576
577
578
579
580
581
# File 'lib/hash-utils/array.rb', line 572

def drop!(length)
    if length < 0
        raise ArgumentError::new('attempt to drop negative size')
    elsif length == 0
        self
    else
        self.slice! 0..(length - 1)
        self
    end
end

#eighthObject



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

def eighth
    self[7]
end

#expand_by(*objects) ⇒ Object



360
361
362
# File 'lib/hash-utils/array.rb', line 360

def expand_by(*objects)
    self.dup.expand_by!(*objects)
end

#expand_by!(*objects) ⇒ Object



378
379
380
381
382
383
384
# File 'lib/hash-utils/array.rb', line 378

def expand_by!(*objects)
    if self.empty?
        self
    else
        self.map! { |i| i = [i]; i += objects }.flatten! 1
    end
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

#interlace(*objects) ⇒ Object



342
343
344
# File 'lib/hash-utils/array.rb', line 342

def interlace(*objects)
    self.dup.interlace! *objects
end

#interlace!(*objects) ⇒ Object



324
325
326
# File 'lib/hash-utils/array.rb', line 324

def interlace!(*objects)
    self.expand_by!(*objects).cut! objects.length
end

#merge!(*arrays) ⇒ Object



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

def merge!(*arrays)
    arrays.flatten! 1
    self.push(*arrays)
    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

#shrink_to(length) ⇒ Object



528
529
530
# File 'lib/hash-utils/array.rb', line 528

def shrink_to(length)
    self.dup.shrink_to! length
end

#shrink_to!(length) ⇒ Object



546
547
548
549
550
551
552
553
554
555
556
# File 'lib/hash-utils/array.rb', line 546

def shrink_to!(length)
    delta = self.length - length
    
    if length < 0
        raise ::ArgumentError::new("negative argument")
    elsif delta > 0
        self.cut! self.length - length
    else
        self
    end
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

#valuesObject



596
597
598
# File 'lib/hash-utils/array.rb', line 596

def values
    self
end

#zip!(*members) ⇒ Object



444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
# File 'lib/hash-utils/array.rb', line 444

def zip!(*members)
    self.map! { |i| [i] }
    lengths = [ ]
    
    # creates zip by hand
    members.each_index do |i|
        item = members[i]
        lengths << item.length
        
        item.each_index do |j|
            self[j][i + 1] = item[j]
        end
    end
    
    # padds to fixed length according to longest
    if not lengths.empty?
        padding = lengths.max + 1
        self.each { |i| i.complete_to!(padding) }
    end
    
    return self
end