Class: PayPal::SDK::Core::API::DataTypes::ArrayWithBlock

Inherits:
Array
  • Object
show all
Defined in:
lib/paypal-sdk/core/api/data_types/array_with_block.rb

Overview

Create Array with block.

Example

ary = ArrayWithBlock.new{|val| val.to_i }
ary.push("123") # [ 123 ]
ary.merge!(["1", "3"])  # [ 123, 1, 3 ]

Instance Method Summary collapse

Constructor Details

#initialize(&block) ⇒ ArrayWithBlock

Returns a new instance of ArrayWithBlock.



10
11
12
13
# File 'lib/paypal-sdk/core/api/data_types/array_with_block.rb', line 10

def initialize(&block)
  @block   = block
  super()
end

Instance Method Details

#[](key) ⇒ Object



15
16
17
# File 'lib/paypal-sdk/core/api/data_types/array_with_block.rb', line 15

def [](key)
  super(key) || send(:"[]=", key, nil)
end

#[]=(key, value) ⇒ Object



19
20
21
# File 'lib/paypal-sdk/core/api/data_types/array_with_block.rb', line 19

def []=(key, value)
  super(key, @block ? @block.call(value) : value)
end

#merge!(array) ⇒ Object



27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/paypal-sdk/core/api/data_types/array_with_block.rb', line 27

def merge!(array)
  if array.is_a? Array
    array.each do |object|
      push(object)
    end
  elsif array.is_a? Hash and array.keys.first.to_s =~ /^\d+$/
    array.each do |key, object|
      self[key.to_i] = object
    end
  else
    push(array)
  end
  self
end

#push(value) ⇒ Object



23
24
25
# File 'lib/paypal-sdk/core/api/data_types/array_with_block.rb', line 23

def push(value)
  super(@block ? @block.call(value) : value)
end