Class: Array
- Inherits:
-
Object
- Object
- Array
- Defined in:
- lib/array_op_custom.rb
Instance Method Summary collapse
- #*(other) ⇒ Object
- #+(other) ⇒ Object
- #-(other) ⇒ Object
- #==(other) ⇒ Object
- #sum ⇒ Object
- #to_h ⇒ Object
- #to_i ⇒ Object
- #to_s ⇒ Object
Instance Method Details
#*(other) ⇒ Object
20 21 22 23 24 25 26 27 28 29 30 |
# File 'lib/array_op_custom.rb', line 20 def *(other) if self.length >= other.length self.each_index do |i| self[i] = other[i] ? self[i] * other[i] : self[i] end elsif other.length > self.length other.each_index do |i| other[i] = self[i] ? self[i] * other[i] : other[i] end end end |
#+(other) ⇒ Object
9 10 11 12 13 14 15 16 17 18 19 |
# File 'lib/array_op_custom.rb', line 9 def +(other) if self.length >= other.length self.each_index do |i| self[i] = other[i] ? self[i] + other[i] : self[i] end elsif other.length > self.length other.each_index do |i| other[i] = self[i] ? self[i] + other[i] : other[i] end end end |
#-(other) ⇒ Object
31 32 33 34 35 36 37 38 39 40 41 |
# File 'lib/array_op_custom.rb', line 31 def -(other) if self.length >= other.length self.each_index do |i| self[i] = other[i] ? (self[i] - other[i]).abs : self[i] end elsif other.length > self.length other.each_index do |i| other[i] = self[i] ? (self[i] - other[i]).abs : other[i] end end end |
#==(other) ⇒ Object
42 43 44 |
# File 'lib/array_op_custom.rb', line 42 def ==(other) return self.dup.push(other) end |
#sum ⇒ Object
58 59 60 |
# File 'lib/array_op_custom.rb', line 58 def sum self.reduce{|a, b| a + b} end |
#to_h ⇒ Object
51 52 53 54 55 56 57 |
# File 'lib/array_op_custom.rb', line 51 def to_h h = {} self.each_with_index do |a, b| h[b.to_s.to_sym] = a end return h end |
#to_i ⇒ Object
45 46 47 |
# File 'lib/array_op_custom.rb', line 45 def to_i self.map{|i| i.to_i} end |
#to_s ⇒ Object
48 49 50 |
# File 'lib/array_op_custom.rb', line 48 def to_s self.map{|i| i.to_s} end |