Class: Array
Instance Method Summary collapse
- #mod(index) ⇒ Object
- #rand ⇒ Object
- #rotate(amount) ⇒ Object
- #to_hash ⇒ Object (also: #to_h, #to_hsh)
- #total ⇒ Object
Instance Method Details
#mod(index) ⇒ Object
106 107 108 |
# File 'lib/libaaron.rb', line 106 def mod(index) self[index] = yield self[index] end |
#rand ⇒ Object
109 110 111 |
# File 'lib/libaaron.rb', line 109 def rand self[Kernel.rand(self.length)] end |
#rotate(amount) ⇒ Object
87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 |
# File 'lib/libaaron.rb', line 87 def rotate(amount) = "Unexpected type! Array.rotate requires an integer!" raise unless amount.is_a? Integer out_arr = self.dup if amount > 0 amount.times do out_arr.push(out_arr.shift) end elsif amount < 0 amount.abs.times do out_arr.unshift(out_arr.pop) end elsif amount == 0 out_arr else raise end return out_arr end |
#to_hash ⇒ Object Also known as: to_h, to_hsh
77 78 79 80 81 82 83 84 |
# File 'lib/libaaron.rb', line 77 def to_hash out_hash = Hash.new for pair in self raise "Malformed array" unless pair.is_a?(Array) && (pair.length == 2) out_hash[pair[0]] = pair[1] end return out_hash end |
#total ⇒ Object
74 75 76 |
# File 'lib/libaaron.rb', line 74 def total self.inject {|sum,num| sum+num} end |