Class: Array
- Inherits:
-
Object
- Object
- Array
- Defined in:
- lib/array_op_custom.rb
Instance Method Summary collapse
- #*(other) ⇒ Object
- #+(other) ⇒ Object
- #-(other) ⇒ Object
- #sum ⇒ Object
- #to_h ⇒ Object
- #to_i ⇒ Object
- #to_s ⇒ Object
Instance Method Details
#*(other) ⇒ Object
14 15 16 17 18 |
# File 'lib/array_op_custom.rb', line 14 def *(other) self.each_index do |i| self[i] = other[i] ? self[i] * other[i] : self[i] end end |
#+(other) ⇒ Object
9 10 11 12 13 |
# File 'lib/array_op_custom.rb', line 9 def +(other) self.each_index do |i| self[i] = other[i] ? self[i] + other[i] : self[i] end end |
#-(other) ⇒ Object
19 20 21 22 23 |
# File 'lib/array_op_custom.rb', line 19 def -(other) self.each_index do |i| self[i] = other[i] ? (self[i] - other[i]).abs : self[i] end end |
#sum ⇒ Object
37 38 39 |
# File 'lib/array_op_custom.rb', line 37 def sum self.reduce{|a, b| a + b} end |
#to_h ⇒ Object
30 31 32 33 34 35 36 |
# File 'lib/array_op_custom.rb', line 30 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
24 25 26 |
# File 'lib/array_op_custom.rb', line 24 def to_i self.map{|i| i.to_i} end |
#to_s ⇒ Object
27 28 29 |
# File 'lib/array_op_custom.rb', line 27 def to_s self.map{|i| i.to_s} end |