Class: Array

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

Instance Method Summary collapse

Instance Method Details

#countObject

This will give you a count, as a Hash, of all the values in the Array. %wspam eggs ham eggs spam.count # => => 2, “ham” => 1, “spam” => 3



76
77
78
79
80
# File 'lib/extensions/array.rb', line 76

def count
  k = Hash.new(0)
  self.each{|x| k[x] += 1}
  k
end

#invertObject

This will invert the index and the values and return a Hash of the results. %wyellow orange.invert # => => 0, “orange” => 2, “yellow” => 1



84
85
86
87
88
# File 'lib/extensions/array.rb', line 84

def invert
  h = {}
  self.each_with_index{|x,i| h[x] = i}
  h
end

#parse_splat_argsObject

This method is useful when you have a method that looks like this: def foo(*args)

do something

end The problem is when you use the * like that everything that comes in is an array. Here are a few problems with this: foo() When you pass an array into this type of method you get the following nested array:

[1,2,3]

The parse_splat_args method, if called, would do this: args.parse_splat_args # => [1,2,3] Now say you called this method like such: foo(1) args would be [1] args.parse_splat_args # => 1 Finally foo args.parse_splat_args # => nil



21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/extensions/array.rb', line 21

def parse_splat_args
  unless self.empty?
    args = self#.flatten
    case args.size
    when 1
      return args.first # if there was only one arg passed, return just that, without the array
    when 0
      return nil # if no args were passed return nil
    else
      return args # else return the array back, cause chances are that's what they passed you!
    end
  end
end

#pick_randomObject

This will pick a random value from the array



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

def pick_random
  self[rand(self.length)]
end

#random_eachObject

This allows you to easily recurse of the array randomly.



59
60
61
# File 'lib/extensions/array.rb', line 59

def random_each
  self.randomize.each {|x| yield x}
end

#randomize(&block) ⇒ Object

This will return a new instance of the array sorted randomly.



36
37
38
39
40
41
42
# File 'lib/extensions/array.rb', line 36

def randomize(&block)
  if block_given?
    self.sort {|x,y| yield x, y}
  else
    self.sort_by { rand }
  end
end

#randomize!(&block) ⇒ Object

This calls the randomize method, but will permantly replace the existing array.



45
46
47
48
49
50
51
# File 'lib/extensions/array.rb', line 45

def randomize!(&block)
  if block_given?
    self.replace(self.randomize(&block))
  else
    self.replace(self.randomize)
  end
end

#subset?(other) ⇒ Boolean

Returns:

  • (Boolean)


63
64
65
66
67
68
# File 'lib/extensions/array.rb', line 63

def subset?(other)
  self.each do |x|
    return false if !(other.include? x)
  end
  true
end

#superset?(other) ⇒ Boolean

Returns:

  • (Boolean)


70
71
72
# File 'lib/extensions/array.rb', line 70

def superset?(other)
  other.subset?(self)
end