Class: Array

Inherits:
Object
  • Object
show all
Defined in:
lib/rest_connection/patches.rb

Overview

Array Patches

Instance Method Summary collapse

Instance Method Details

#*(second) ⇒ Object



115
116
117
118
119
120
121
122
123
124
125
126
127
# File 'lib/rest_connection/patches.rb', line 115

def *(second)
  if second.is_a?(Integer)
    ret = []
    second.times { |i| ret += dup }
    return ret
  elsif second.is_a?(Array)
    ret = []
    each { |x| second.each { |y| ret << [x,y].flatten } }
    return ret
  else
    old_multiply(second)
  end
end

#**(second) ⇒ Object



129
130
131
132
133
134
135
136
137
138
139
140
141
# File 'lib/rest_connection/patches.rb', line 129

def **(second)
  if second.is_a?(Integer)
    ret = dup
    (second - 1).times {
      temp = []
      ret.each { |x| each { |y| temp << [x,y].flatten } }
      ret = temp
    }
    return ret
  else
    raise TypeError.new("can't convert #{second.class} into Integer")
  end
end

#deep_merge(second) ⇒ Object



79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/rest_connection/patches.rb', line 79

def deep_merge(second)
  target = dup
  return target unless second
  unless Array === second
    raise TypeError.new("can't convert #{second.class} into #{self.class}")
  end
  second.each_index do |k|
    if second[k].is_a? Array and self[k].is_a? Array
      target[k] = target[k].deep_merge(second[k])
      next
    elsif second[k].is_a? Hash and self[k].is_a? Hash
      target[k] = target[k].deep_merge(second[k])
      next
    end
    target << second[k] unless target.include?(second[k])
  end
  target
end

#deep_merge!(second) ⇒ Object



98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
# File 'lib/rest_connection/patches.rb', line 98

def deep_merge!(second)
  return nil unless second
  unless Array === second
    raise TypeError.new("can't convert #{second.class} into #{self.class}")
  end
  second.each_index do |k|
    if self[k].is_a?(Array) and second[k].is_a?(Array)
      self[k].deep_merge!(second[k])
    elsif self[k].is_a?(Hash) and second[k].is_a?(Hash)
      self[k].deep_merge!(second[k])
    else
      self << second[k] unless self.include?(second[k])
    end
  end
end

#old_multiplyObject



114
# File 'lib/rest_connection/patches.rb', line 114

alias :old_multiply :*