Class: Array
- 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
-
#choice ⇒ Object
Implemented in Ruby 1.9.
-
#choices(count) ⇒ Array
A new array filled with count calls to choice.
-
#divvy ⇒ Object
Divvy the array– REMOVED.
-
#intersect ⇒ Array
In typical usage, each item is an array.
-
#join(*fixes) ⇒ String
Concatenate the items into a string by join.
-
#join_prefix_suffix(prefix, suffix) ⇒ String
Concatenate the items by joining using a prefix string and suffix string.
-
#join_prefix_suffix_infix(prefix, suffix, infix) ⇒ String
Concatenate the items by joining using a prefix string, suffix string, and infix string.
-
#onto(values) ⇒ Hash
This is identical to calling foo.zip(values).to_h.
-
#rotate! ⇒ Array
Move the first item to the last by using Array#shift and Array#push.
-
#ruby_join ⇒ Object
Alias join because we’re going to override it.
-
#shifted(number_of_items = 1) ⇒ Array
(also: #cdr, #rest)
Ruby programmers may prefer this alias wording: list.first => ‘a’ list.rest => [‘b’,‘c’].
-
#shifted!(number_of_items = 1) ⇒ Array
(also: #cdr!, #rest!)
Delete the first number_of_items items.
-
#shuffle ⇒ Array
Randomly arrange the array items.
-
#shuffle! ⇒ Array
Randomly arrange the array items.
-
#size? ⇒ Boolean
True if size > 0.
-
#slices ⇒ Object
Slice the array by size– REMOVED.
-
#to_csv(ops = {}) ⇒ String
N.b.
-
#to_tsv(ops = {}) ⇒ String
(also: #to_tdf)
Each subarray becomes one ‘line’ in the output.
-
#union ⇒ Array
In typical use, each item is an array.
Instance Method Details
#choice ⇒ Object
Implemented in Ruby 1.9
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.
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 |
#divvy ⇒ Object
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 |
#intersect ⇒ Array
In typical usage, each item is an array.
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.
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.
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.
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
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
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_join ⇒ Object
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']
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 []
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 |
#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/
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/
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.
12 13 14 |
# File 'lib/sixarm_ruby_ramp/array.rb', line 12 def size? return size>0 end |
#slices ⇒ Object
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.
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.
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 |
#union ⇒ Array
In typical use, each item is an array.
120 121 122 |
# File 'lib/sixarm_ruby_ramp/array.rb', line 120 def union inject{|inj,item| inj | item.to_a } || [] end |