Class: Array

Inherits:
Object show all
Defined in:
lib/libaaron.rb

Instance Method Summary collapse

Instance Method Details

#mod(index) ⇒ Object



106
107
108
# File 'lib/libaaron.rb', line 106

def mod(index)
  self[index] = yield self[index]
end

#randObject



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)
  type_error_message = "Unexpected type! Array.rotate requires an integer!"
  raise type_error_message 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 type_error_message
  end
  return out_arr
end

#to_hashObject 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

#totalObject



74
75
76
# File 'lib/libaaron.rb', line 74

def total
  self.inject {|sum,num| sum+num}
end