Class: Array

Inherits:
Object show all
Defined in:
lib/more_ruby/array.rb

Instance Method Summary collapse

Instance Method Details

#all_instance_of?(klass) ⇒ Boolean

Returns true if all elements of self are instance_of?(klass)

Returns:

  • (Boolean)


94
95
96
# File 'lib/more_ruby/array.rb', line 94

def all_instance_of?(klass)
    all? {|item| item.instance_of?(klass)}
end

#all_kind_of?(klass) ⇒ Boolean

Returns true if all elements of self are kind_of?(klass)

Returns:

  • (Boolean)


89
90
91
# File 'lib/more_ruby/array.rb', line 89

def all_kind_of?(klass)
    all? {|item| item.kind_of?(klass)}
end

#delete_randomObject

Deletes at a random index from the array, and returns the deleted item



17
18
19
20
21
22
# File 'lib/more_ruby/array.rb', line 17

def delete_random
    index = rand(size)
    item = self[index]
    delete_at(index)
    item
end

#include_any?(*items) ⇒ Boolean

Does the array contain any of the supplied items?

Returns:

  • (Boolean)


4
5
6
7
8
9
# File 'lib/more_ruby/array.rb', line 4

def include_any?(*items)
    items.each do |i|
     return true if self.include? i
    end
    false
end

#insert_flat(index, o) ⇒ Object

Will insert each member of the supplied array into self, such that self does not contain a new sub-array Insertion behaviour is more like += than << , e.g. [1, 2, 3].insert_flat(0, [4, 5]) => [4, 5, 1, 2, 3] Does not flatten self, so any preexisting subarrays are preserved



60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/more_ruby/array.rb', line 60

def insert_flat(index, o)
    case o
    when Array
        # A -ve insertion index inserts items AFTER the item at that index, which causes problems when inserting multiple times
        # Use o.each when -ve ; o.reverse_each when +ve or zero
        if index < 0
            o.each do |item|
                insert(index, item)
            end
        else
            o.reverse_each do |item|
                insert(index, item)
            end
        end
    else
        insert(index, o)
    end
end

#meanObject Also known as: av

Returns the mean / average of the Numeric array. Returns a float. All entries must be Numeric.



42
43
44
45
# File 'lib/more_ruby/array.rb', line 42

def mean
    each {|item| raise TypeError, "Cannot determine the mean of an array that contains non-Numeric objects." unless item.kind_of?(Numeric)}
    inject{ |sum, item| sum + item }.to_f / size
end

#peachObject

A prettier way of printing an array Want to p each row, but have each p appear on a new line in STDOUT



100
101
102
# File 'lib/more_ruby/array.rb', line 100

def peach
    self.each { |z| p z }
end

#randomObject

Return a random item from the array (by index)



12
13
14
# File 'lib/more_ruby/array.rb', line 12

def random
    self[rand(size)]
end

#random_indexObject



24
25
26
# File 'lib/more_ruby/array.rb', line 24

def random_index
    rand(size)
end

#random_insert(element) ⇒ Object

Insert the supplied element to a random position within the array



29
30
31
# File 'lib/more_ruby/array.rb', line 29

def random_insert(element)
    insert(rand(size), element)
end

#random_move(times = 1) ⇒ Object

Move a random element to a random position in the array



34
35
36
37
38
39
# File 'lib/more_ruby/array.rb', line 34

def random_move(times = 1)
    times.times do
        element = delete_random
        random_insert(element)
    end
end

#stringify_all_values_deepObject

Method to convert all values to strings, for all layers within the array



105
106
107
108
109
110
111
112
113
114
115
116
117
# File 'lib/more_ruby/array.rb', line 105

def stringify_all_values_deep
    for i in 0 ... size do
        v = self[i]
        case v
        when Array, Hash
            v = v.stringify_all_values_deep
        else
            v = v.to_s
        end
        self[i] = v
    end
    self
end

#sumObject

Returns the summation of the contents of the array Ignores nils; raises if the array contains a non-Numeric non-nil

Raises:

  • (TypeError)


50
51
52
53
54
55
# File 'lib/more_ruby/array.rb', line 50

def sum
    raise TypeError, "Array contained non-numeric non-nil elements; cannot sum contents." unless compact.all_kind_of? Numeric
    compact.inject(0) do |sum, item| 
        sum += item 
    end
end

#wrap_fetch(index) ⇒ Object Also known as: modulo_fetch

A simple method to fetch the item at the index. If index > size, then it ‘wraps’ around and looks again E.g. [0, 1, 2].wrap_fetch(4) returns 1 Operation is effectively fetch(index % size)



82
83
84
85
# File 'lib/more_ruby/array.rb', line 82

def wrap_fetch(index)
    return nil if empty?
    fetch(index % size)
end