Class: Array

Inherits:
Object show all
Defined in:
lib/sixarm_ruby_ramp/array.rb,
lib/sixarm_ruby_ramp/array/join.rb,
lib/sixarm_ruby_ramp/array/shuffle.rb

Instance Method Summary collapse

Instance Method Details

#choiceObject

Implemented in Ruby 1.9

Examples:

[1,2,3,4].choice => 2
[1,2,3,4].choice => 4
[1,2,3,4].choice => 3

Returns:

  • (Object)

    a random item from the array



43
44
45
# File 'lib/sixarm_ruby_ramp/array.rb', line 43

def choice
  self[Kernel.rand(size)]
end

#choices(count) ⇒ Array

Returns a new array filled with count calls to choice.

Examples:

[1,2,3,4].choices(2) => [3,1]
[1,2,3,4].choices(3) => [4,2,3]

Returns:

  • (Array)

    a new array filled with count calls to choice



54
55
56
57
58
# File 'lib/sixarm_ruby_ramp/array.rb', line 54

def choices(count)
  arr = Array.new
  count.times { arr << self.choice }
  return arr
end

#divvyObject

Divvy the array– REMOVED. Change to sixarm_ruby_array_slice gem Array#slice_by_share method.



93
94
95
# File 'lib/sixarm_ruby_ramp/array.rb', line 93

def divvy
  raise "Change to sixarm_ruby_array_slice gem Array#slice_by_share method."
end

#intersectArray

In typical usage, each item is an array.

Examples:

arr=[[1,2,3,4],[3,4,5,6]]
arr.intersect
=> [3,4]

with proc

arr.map(&:foo).intersect
=> foos that are in all of the array items

with nil

[].intersect => []

Returns:

  • (Array)

    the intersection of the array’s items.



141
142
143
# File 'lib/sixarm_ruby_ramp/array.rb', line 141

def intersect
  inject{|inj,item| inj & item.to_a } || []
end

#join(*fixes) ⇒ String

Concatenate the items into a string by join.

Examples:

Join with infix

list=['a','b','c']
list.join("*") => "a*b*c"

Join with prefix and suffix

list=['a','b','c']
list.join("[","]") => "[a][b][c]"

Join with prefix, suffix, and infix

list=['a','b','c']
list.join("*","[","]") => "[a]*[b]*[c]"

Returns:

  • (String)

    concatenated string



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/sixarm_ruby_ramp/array/join.rb', line 24

def join(*fixes)
  if fixes.is_a?(String) then return self.ruby_join(fixes) end
  return case fixes.size
  when 0
    ruby_join
  when 1
    ruby_join(fixes[0].to_s)
  when 2
    join_prefix_suffix(*fixes)
  when 3
    join_prefix_suffix_infix(*fixes)
  else
    raise ArgumentError, "join() takes 0-3 arguments; you gave #{fixes.size}]"
  end
end

#join_prefix_suffix(prefix, suffix) ⇒ String

Concatenate the items by joining using a prefix string and suffix string.

Examples:

list=['a','b','c']
list.join("[","]") => "[a][b][c]"

Returns:

  • (String)

    concatenated string



48
49
50
51
52
# File 'lib/sixarm_ruby_ramp/array/join.rb', line 48

def join_prefix_suffix(prefix, suffix)
  prefix = prefix.to_s
  suffix = suffix.to_s
  return self.map{|item| prefix + item.to_s + suffix}.ruby_join()
end

#join_prefix_suffix_infix(prefix, suffix, infix) ⇒ String

Concatenate the items by joining using a prefix string, suffix string, and infix string.

Examples:

list=['a','b','c']
list.join("*","[","]") => "[a]*[b]*[c]"

Returns:

  • (String)

    concatenated string



62
63
64
65
66
67
# File 'lib/sixarm_ruby_ramp/array/join.rb', line 62

def join_prefix_suffix_infix(prefix, suffix, infix)
  prefix = prefix.to_s
  suffix = suffix.to_s
  infix = infix.to_s
  return self.map{|item| prefix + item.to_s + suffix}.ruby_join(infix)
end

#onto(values) ⇒ Hash

This is identical to calling foo.zip(values).to_h

Examples:

foo=[:a,:b,:c]
goo=[:x,:y,:z]
foo.onto(goo) => {:a=>:x, :b=>:y, :c=>:z}

Returns:

  • (Hash)

    a hash of this array’s items as keys, mapped onto another array’s items as values.



71
72
73
74
# File 'lib/sixarm_ruby_ramp/array.rb', line 71

def onto(values)
  size==values.size or raise ArgumentError, "Array size #{size} must match values size #{size}"
  zip(values).to_h
end

#rotate!Array

Move the first item to the last by using Array#shift and Array#push

Examples:

[1,2,3,4].rotate! => [2,3,4,1]
['a','b','c'].rotate! => ['b','c','a']
[].rotate! => []

Returns:



26
27
28
29
30
31
# File 'lib/sixarm_ruby_ramp/array.rb', line 26

def rotate!
  if size>0
    push item=shift
  end
  self
end

#ruby_joinObject

Alias join because we’re going to override it



6
# File 'lib/sixarm_ruby_ramp/array/join.rb', line 6

alias :ruby_join :join

#shifted(number_of_items = 1) ⇒ Array Also known as: cdr, rest

Ruby programmers may prefer this alias wording:

list.first => 'a'
list.rest => ['b','c']

LISP programmers may prefer this alias wording:

list.car => 'a'
list.cdr => ['b','c']

Examples:

list=['a','b','c']
list.shift => 'a'
list.shifted => ['b','c']

with length

list.shifted(0) => ['a','b','c']
list.shifted(1) => ['b','c']
list.shifted(2) => ['c']
list.shifted(3) => []

Returns:

  • (Array)

    the rest of the items of self, after a shift.



175
176
177
178
179
# File 'lib/sixarm_ruby_ramp/array.rb', line 175

def shifted(number_of_items=1)
  (number_of_items.is_a? Integer) or (raise ArgumentError, "number_of_items must be an integer")
  (number_of_items >= 0) or (raise ArgumentError, "number_of_items must be >= 0")
  slice(number_of_items,self.length-number_of_items)
end

#shifted!(number_of_items = 1) ⇒ Array Also known as: cdr!, rest!

Delete the first number_of_items items.

If n is greater than the array size, then return []

Examples:

list=['a','b','c']
list.shifted!
list => ['b','c']

with length:

list=['a','b','c']
list.shifted!(2)
list => ['c']

Returns:

  • (Array)

    the array, minus the deleted items.



202
203
204
205
206
207
# File 'lib/sixarm_ruby_ramp/array.rb', line 202

def shifted!(number_of_items=1)
  (number_of_items.is_a? Integer) or (raise ArgumentError, "number_of_items must be an integer")
  (number_of_items >= 0) or (raise ArgumentError, "number_of_items must be >= 0")
  slice!(0,number_of_items)
  return self
end

#shuffleArray

Randomly arrange the array items.

This implementation is optimized for speed, not for memory use. See codeidol.com/other/rubyckbk/Arrays/Shuffling-an-Array/

Examples:

list=
list=['a','b','c']
list.shuffle!
list => ['c','a','b']

Returns:

  • (Array)

    a new array with the items shuffled.



20
21
22
# File 'lib/sixarm_ruby_ramp/array/shuffle.rb', line 20

def shuffle
  dup.shuffle!
end

#shuffle!Array

Randomly arrange the array items.

This implementation is optimized for speed, not for memory use. See codeidol.com/other/rubyckbk/Arrays/Shuffling-an-Array/

Examples:

list=
list=['a','b','c']
list.shuffle!
list => ['c','a','b']

Returns:

  • (Array)

    the array, with its items shuffled.



41
42
43
44
45
46
# File 'lib/sixarm_ruby_ramp/array/shuffle.rb', line 41

def shuffle!
  each_index do |i|
    j = rand(length-i) + i
    self[j], self[i] = self[i], self[j]
  end
end

#size?Boolean

Returns true if size > 0.

Examples:

[1,2,3].size? => true
[].size? => false

Returns:

  • (Boolean)

    true if size > 0



12
13
14
# File 'lib/sixarm_ruby_ramp/array.rb', line 12

def size?
  return size>0
end

#slicesObject

Slice the array by size– REMOVED. Change to sixarm_ruby_array_slice gem Array#slice_by_size method.



86
87
88
# File 'lib/sixarm_ruby_ramp/array.rb', line 86

def slices
  raise "Change to sixarm_ruby_array_slice gem Array#slice_by_size method."
end

#to_csv(ops = {}) ⇒ String

N.b. this method uses the multi-dimensional if the array’s first item is also an array.

Examples:

of a one-dimensional array


[1,2,3].to_csv => "1,2,3\n"

of a multi-dimensional array


[[1,2,3],[4,5,6]] => "1,2,3\n4,5,6\n"

of a blank array


[].to_csv => ""

Returns:

  • (String)

    a CSV (Comma Separated Value) string of this array.



236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
# File 'lib/sixarm_ruby_ramp/array.rb', line 236

def to_csv(ops={})

  return "" if size==0

  generator = RUBY_VERSION >= "1.9" ? CSV : CSV::Writer

  str=''
  if size>0 and self[0].is_a?Array
    generator.generate(str) do |csv|
      self.each do |row|
        csv << row
      end
    end
  else
    generator.generate(str) do |csv|
      csv << self.map{|item| item.to_s}
    end
  end
  return str
end

#to_tsv(ops = {}) ⇒ String Also known as: to_tdf

Each subarray becomes one ‘line’ in the output.

Examples:

of a blank array


[].to_csv => ""

Returns:

  • (String)

    a TSV (Tab Separated Value) string representation of a multi-dimensional array.



267
268
269
# File 'lib/sixarm_ruby_ramp/array.rb', line 267

def to_tsv(ops={})
  self.map{|row| row.join("\t")+"\n"}.join
end

#unionArray

In typical use, each item is an array.

Examples:

using Ruby Array pipe

arr=[[1,2,3,4],[3,4,5,6]]
arr.union => [1,2,3,4,5,6]

with proc

arr.map(&:foo).union
=> foos that are in any of the array items

with nil

[].union => []

Returns:

  • (Array)

    the union of the array’s items.



120
121
122
# File 'lib/sixarm_ruby_ramp/array.rb', line 120

def union
  inject{|inj,item| inj | item.to_a } || []
end