Class: Array

Inherits:
Object
  • Object
show all
Defined in:
lib/array_extensions.rb

Overview

Array Extensions

Author

Joel Parker Henderson, [email protected]

Copyright

Copyright © 2006-2009 Joel Parker Henderson

License

CreativeCommons License, Non-commercial Share Alike

License

LGPL, GNU Lesser General Public License

Ruby Array base class extensions.

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



47
48
49
# File 'lib/array_extensions.rb', line 47

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]


57
58
59
60
61
# File 'lib/array_extensions.rb', line 57

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


113
114
115
# File 'lib/array_extensions.rb', line 113

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


154
155
156
# File 'lib/array_extensions.rb', line 154

def intersect
  inject{|inj,x| inj & x.to_a }
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



32
33
34
35
# File 'lib/array_extensions.rb', line 32

def rotate!
 push x=shift
 self
end

#size?Boolean

Return true if size > 0

Examples

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

Returns:

  • (Boolean)


19
20
21
# File 'lib/array_extensions.rb', line 19

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


84
85
86
87
88
89
90
91
92
# File 'lib/array_extensions.rb', line 84

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

#unionObject

Return the union of the array’s items. In typical use, each item is an array.

Examples

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

Examples with proc

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


137
138
139
# File 'lib/array_extensions.rb', line 137

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