Class: Array
Overview
Array extensions
Instance Method Summary collapse
-
#choice ⇒ Object
Implemented in Ruby 1.9.
-
#choices(count) ⇒ Array
A new array filled with count calls to choice.
-
#divvy(number_of_slices) ⇒ Array<Array<Object>>
Divvy the array, like a pie, into n number of slices.
-
#intersect ⇒ Array
In typical usage, each item is an array.
-
#join(*fixes) ⇒ String
Concatenate the items into a string by join.
-
#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
This implementation is optimized for speed, not for memory use.
-
#shuffle! ⇒ Array
Randomly arrange the array items.
-
#size? ⇒ Boolean
True if size > 0.
-
#slices(slice_length) ⇒ Array<Array<Object>>
Slice the array.
-
#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
90 91 92 |
# File 'lib/webget_ruby_ramp/array.rb', line 90 def choice self[Kernel.rand(size)] end |
#choices(count) ⇒ Array
Returns a new array filled with count calls to choice.
101 102 103 104 105 |
# File 'lib/webget_ruby_ramp/array.rb', line 101 def choices(count) arr = Array.new count.times { arr << self.choice } return arr end |
#divvy(number_of_slices) ⇒ Array<Array<Object>>
Divvy the array, like a pie, into n number of slices.
If the array divides evenly, then each slice has size/n items.
Otherwise, divvy makes a best attempt by rounding up to give earlier slices one more item, which makes the last slice smaller:
If the array size so small compared to n that there is no mathematical way to n slices, then divvy will return as many slices as it can.
178 179 180 181 182 |
# File 'lib/webget_ruby_ramp/array.rb', line 178 def divvy(number_of_slices) (number_of_slices.is_a? Integer) or (raise ArgumentError, "number_of_slices must be an integer") (number_of_slices > 0) or (raise ArgumentError, "number_of_slices must be > 0") return slices((length.to_f/number_of_slices.to_f).ceil) end |
#intersect ⇒ Array
In typical usage, each item is an array.
228 229 230 |
# File 'lib/webget_ruby_ramp/array.rb', line 228 def intersect inject{|inj,item| inj & item.to_a } || [] end |
#join(*fixes) ⇒ String
Concatenate the items into a string by join.
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 |
# File 'lib/webget_ruby_ramp/array.rb', line 31 def join(*fixes) if fixes.is_a?(String) then return self.ruby_join(fixes) end case fixes.size when 0 return self.ruby_join() when 1 return self.ruby_join(fixes[0]) when 2 prefix=fixes[0].to_s suffix=fixes[1].to_s return self.map{|item| prefix + item.to_s + suffix}.ruby_join() when 3 infix =fixes[0].to_s prefix=fixes[1].to_s suffix=fixes[2].to_s return self.map{|item| prefix + item.to_s + suffix}.ruby_join(infix) else raise ArgumentError, "join() takes 0-3 arguments; you gave #{fixes.size}]" end end |
#onto(values) ⇒ Hash
This is identical to calling foo.zip(values).to_h
118 119 120 121 |
# File 'lib/webget_ruby_ramp/array.rb', line 118 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
73 74 75 76 77 78 |
# File 'lib/webget_ruby_ramp/array.rb', line 73 def rotate! if size>0 push item=shift end self end |
#ruby_join ⇒ Object
Alias join because we’re going to override it
12 |
# File 'lib/webget_ruby_ramp/array.rb', line 12 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']
262 263 264 265 266 |
# File 'lib/webget_ruby_ramp/array.rb', line 262 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 []
289 290 291 292 293 294 |
# File 'lib/webget_ruby_ramp/array.rb', line 289 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
This implementation is optimized for speed, not for memory use. See codeidol.com/other/rubyckbk/Arrays/Shuffling-an-Array/
332 333 334 |
# File 'lib/webget_ruby_ramp/array.rb', line 332 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/
313 314 315 316 317 318 |
# File 'lib/webget_ruby_ramp/array.rb', line 313 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.
59 60 61 |
# File 'lib/webget_ruby_ramp/array.rb', line 59 def size? return size>0 end |
#slices(slice_length) ⇒ Array<Array<Object>>
Slice the array.
If the slices don’t divide evenly, then the last is smaller.
145 146 147 148 149 150 151 152 153 154 155 |
# File 'lib/webget_ruby_ramp/array.rb', line 145 def slices(slice_length) (slice_length.is_a? Integer) or (raise ArgumentError, "slice_length must be an integer") (slice_length > 0) or (raise ArgumentError, "slice_length must be > 0") arr=[] index=0 while index<length arr.push self[index...(index+slice_length)] index+=slice_length end return arr end |
#to_csv(ops = {}) ⇒ String
N.b. this method uses the multi-dimensional if the array’s first item is also an array.
360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 |
# File 'lib/webget_ruby_ramp/array.rb', line 360 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.
391 392 393 |
# File 'lib/webget_ruby_ramp/array.rb', line 391 def to_tsv(ops={}) self.map{|row| row.join("\t")+"\n"}.join end |
#union ⇒ Array
In typical use, each item is an array.
207 208 209 |
# File 'lib/webget_ruby_ramp/array.rb', line 207 def union inject{|inj,item| inj | item.to_a } || [] end |