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! ⇒ Object
-
#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_lines(ops = {}) ⇒ Object
-
#to_csv_text(ops = {}) ⇒ String
Each subarray becomes one line in the output.
-
#to_tsv_lines(ops = {}) ⇒ String
Each subarray becomes one line in the output.
-
#to_tsv_text(ops = {}) ⇒ String
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
48 49 50 |
# File 'lib/sixarm_ruby_ramp/array.rb', line 48 def choice self[Kernel.rand(size)] end |
#choices(count) ⇒ Array
Returns a new array filled with count calls to choice.
59 60 61 62 63 |
# File 'lib/sixarm_ruby_ramp/array.rb', line 59 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.
98 99 100 |
# File 'lib/sixarm_ruby_ramp/array.rb', line 98 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.
146 147 148 |
# File 'lib/sixarm_ruby_ramp/array.rb', line 146 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
76 77 78 79 |
# File 'lib/sixarm_ruby_ramp/array.rb', line 76 def onto(values) size==values.size or raise ArgumentError, "Array size #{size} must match values size #{size}" zip(values).to_h end |
#rotate! ⇒ Object
30 31 32 33 34 35 |
# File 'lib/sixarm_ruby_ramp/array.rb', line 30 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']
180 181 182 183 184 |
# File 'lib/sixarm_ruby_ramp/array.rb', line 180 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 []
207 208 209 210 211 212 |
# File 'lib/sixarm_ruby_ramp/array.rb', line 207 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.
91 92 93 |
# File 'lib/sixarm_ruby_ramp/array.rb', line 91 def slices raise "Change to sixarm_ruby_array_slice gem Array#slice_by_size method." end |
#to_csv_lines(ops = {}) ⇒ Object
237 238 239 |
# File 'lib/sixarm_ruby_ramp/array.rb', line 237 def to_csv_lines(ops={}) map{|row| row.to_csv} end |
#to_csv_text(ops = {}) ⇒ String
Each subarray becomes one line in the output.
250 251 252 |
# File 'lib/sixarm_ruby_ramp/array.rb', line 250 def to_csv_text(ops={}) to_csv_lines.join end |
#to_tsv_lines(ops = {}) ⇒ String
Each subarray becomes one line in the output.
264 265 266 |
# File 'lib/sixarm_ruby_ramp/array.rb', line 264 def to_tsv_lines(ops={}) map{|row| row.join("\t")+"\n"} end |
#to_tsv_text(ops = {}) ⇒ String
Each subarray becomes one ‘line’ in the output.
278 279 280 |
# File 'lib/sixarm_ruby_ramp/array.rb', line 278 def to_tsv_text(ops={}) to_tsv_lines.join end |
#union ⇒ Array
In typical use, each item is an array.
125 126 127 |
# File 'lib/sixarm_ruby_ramp/array.rb', line 125 def union inject{|inj,item| inj | item.to_a } || [] end |