Class: Array

Inherits:
Object show all
Defined in:
lib/webget_ramp/array.rb

Instance Method Summary collapse

Instance Method Details

#choiceObject

Return a random item from the array

Examples

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

Implemented in Ruby 1.9



82
83
84
# File 'lib/webget_ramp/array.rb', line 82

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

#choices(n) ⇒ Object

Return a new array filled with n calls to choice

Examples

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


93
94
95
96
97
# File 'lib/webget_ramp/array.rb', line 93

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

#divvy(number_of_slices) ⇒ 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:

Examples

[1,2,3,4,5].divvy(2) => [[1,2,3],[4,5]]
[1,2,3,4,5,6,7].divvy(3) => [[1,2,3],[4,5,6],[7]]

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.

Examples

[1,2,3,4,5,6].divvy(4) => [[1,2],[3,4],[5,6]]


163
164
165
# File 'lib/webget_ramp/array.rb', line 163

def divvy(number_of_slices)
  return slices((length.to_f/number_of_slices.to_f).ceil)
end

#intersectObject

Return the intersection of the array’s items. In typical usage, each item is an array.

Examples

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

Examples with proc

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


210
211
212
# File 'lib/webget_ramp/array.rb', line 210

def intersect
  inject{|inj,x| inj & x.to_a }
end

#join(*fixes) ⇒ Object

Concatenate the items into a string by join.

Typical Array#join with infix

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

Improved join with infix, prefix, suffix

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

Improved join with just prefix and suffix

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


26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/webget_ramp/array.rb', line 26

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{|s| prefix + s.to_s + suffix}.ruby_join()
  when 3
    infix =fixes[0].to_s
    prefix=fixes[1].to_s
    suffix=fixes[2].to_s
    return map{|s| prefix + s.to_s + suffix}.ruby_join(infix)
  else
    raise "join(fixes[#{fixes.size}]"
  end
end

#onto(values) ⇒ Object

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

Example

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

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



110
111
112
# File 'lib/webget_ramp/array.rb', line 110

def onto(values)
  zip(values).to_h
end

#rotate!Object

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']

Return self



67
68
69
70
# File 'lib/webget_ramp/array.rb', line 67

def rotate!
  push x=shift
  self
end

#ruby_joinObject

Alias join because we’re going to override it



9
# File 'lib/webget_ramp/array.rb', line 9

alias :ruby_join :join

#shifted(n = 1) ⇒ Object Also known as: cdr, rest

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

Example

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

Example with length

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

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']


244
245
246
# File 'lib/webget_ramp/array.rb', line 244

def shifted(n=1)
 slice(n,self.length-n)
end

#shifted!(n = 1) ⇒ Object Also known as: cdr!, rest!

Delete the first n items. Returns the array, not the deleted items.

Example

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

Example with length:

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

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



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

def shifted!(n=1)
 slice!(0,n)
 return self
end

#shuffleObject



313
314
315
# File 'lib/webget_ramp/array.rb', line 313

def shuffle  
  dup.shuffle!  
end

#shuffle!Object



290
291
292
293
294
295
# File 'lib/webget_ramp/array.rb', line 290

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

#size?Boolean

Return true if size > 0

Examples

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

Returns:

  • (Boolean)


54
55
56
# File 'lib/webget_ramp/array.rb', line 54

def size?
  return size>0
end

#slices(slice_length) ⇒ Object

Return items in groups of n items (aka slices)

Examples

[1,2,3,4,5,6,7,8].slices(2) => [[1,2],[3,4],[5,6],[7,8]]
[1,2,3,4,5,6,7,8].slices(4) => [[1,2,3,4],[5,6,7,8]]

If the slices don’t divide evenly, then the last is smaller.

Examples

[1,2,3,4,5,6,7,8].slices(3) => [[1,2,3],[4,5,6],[7,8]]
[1,2,3,4,5,6,7,8].slices(5) => [[1,2,3,4,5],[6,7,8]]


134
135
136
137
138
139
140
141
142
# File 'lib/webget_ramp/array.rb', line 134

def slices(slice_length)
  a=[]
  i=0
  while i<length
    a.push self[i...(i+slice_length)]
    i+=slice_length
  end
  return a
end

#to_csv(ops = {}) ⇒ Object

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

Example of a one-dimensional array

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

Example of a multi-dimensional array

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

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



338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
# File 'lib/webget_ramp/array.rb', line 338

def to_csv(ops={})

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

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

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

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

Each subarray becomes one ‘line’ in the output.



363
364
365
# File 'lib/webget_ramp/array.rb', line 363

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

#unionObject

Examples with proc

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


193
194
195
# File 'lib/webget_ramp/array.rb', line 193

def union
  inject{|inj,x| inj | x.to_a }
end