Sha256: 34719a59280cfbe8ab8cda1412ac37c14293fc3bd1f20877e4fad96d96c27100

Contents?: true

Size: 696 Bytes

Versions: 24

Compression:

Stored size: 696 Bytes

Contents

# encoding: utf-8

# Array
class Array
  # exchange arrays elements
  #
  # === Example
  #
  #   [*1..6].exchange!(1, 5) # => [1, 6, 3, 4, 5, 2]
  #   [*1..6].exchange!(1, -1) # => [1, 6, 3, 4, 5, 2]
  #   [*1..6].exchange!(1, 6) # => [*1..6]
  #   [].exchange!(1, 2) # => []
  #
  def exchange!(one_index, other_index)
    return self unless one_index.respond_to? :to_i
    return self unless other_index.respond_to? :to_i
    one_index = one_index.to_i
    other_index = other_index.to_i
    return self if one_index.abs >= size
    return self if other_index.abs >= size
    tmp_one = self[one_index]
    self[one_index] = self[other_index]
    self[other_index] = tmp_one
    self
  end
end

Version data entries

24 entries across 24 versions & 1 rubygems

Version Path
tbpgr_utils-0.0.151 lib/open_classes/array/exchange.rb
tbpgr_utils-0.0.150 lib/open_classes/array/exchange.rb
tbpgr_utils-0.0.149 lib/open_classes/array/exchange.rb
tbpgr_utils-0.0.148 lib/open_classes/array/exchange.rb
tbpgr_utils-0.0.147 lib/open_classes/array/exchange.rb
tbpgr_utils-0.0.146 lib/open_classes/array/exchange.rb
tbpgr_utils-0.0.145 lib/open_classes/array/exchange.rb
tbpgr_utils-0.0.144 lib/open_classes/array/exchange.rb
tbpgr_utils-0.0.143 lib/open_classes/array/exchange.rb
tbpgr_utils-0.0.142 lib/open_classes/array/exchange.rb
tbpgr_utils-0.0.141 lib/open_classes/array/exchange.rb
tbpgr_utils-0.0.140 lib/open_classes/array/exchange.rb
tbpgr_utils-0.0.139 lib/open_classes/array/exchange.rb
tbpgr_utils-0.0.138 lib/open_classes/array/exchange.rb
tbpgr_utils-0.0.137 lib/open_classes/array/exchange.rb
tbpgr_utils-0.0.136 lib/open_classes/array/exchange.rb
tbpgr_utils-0.0.135 lib/open_classes/array/exchange.rb
tbpgr_utils-0.0.134 lib/open_classes/array/exchange.rb
tbpgr_utils-0.0.133 lib/open_classes/array/exchange.rb
tbpgr_utils-0.0.132 lib/open_classes/array/exchange.rb