Class: Array

Inherits:
Object show all
Defined in:
lib/m_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 # => “ham”=>1, “spam”=>3



87
88
89
90
91
# File 'lib/m_array.rb', line 87

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

#delete_from_array(args) ⇒ Object

This allows you to delete an array of values from another array.

1,2,3,4,5].delete_from_array() # => [1,4


37
38
39
# File 'lib/m_array.rb', line 37

def delete_from_array(args)
  self.collect{ |x| x unless args.include?(x)}.compact
end

#delete_from_array!(args) ⇒ Object

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



42
43
44
# File 'lib/m_array.rb', line 42

def delete_from_array!(args)
  self.replace(delete_from_array(args))
end

#from_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 from_args method, if called, would do this: args.from_args # => [1,2,3] Now say you called this method like such: foo(1) args would be [1] args.from_args # => 1 Finally foo args.from_args # => nil



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

def from_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

#invertObject

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



95
96
97
98
99
# File 'lib/m_array.rb', line 95

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

#pick_randomObject

This will pick a random value from the array



65
66
67
# File 'lib/m_array.rb', line 65

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

#random_eachObject

This allows you to easily recurse of the array randomly.



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

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

#randomize(&block) ⇒ Object

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



47
48
49
50
51
52
53
# File 'lib/m_array.rb', line 47

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.



56
57
58
59
60
61
62
# File 'lib/m_array.rb', line 56

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

#subset?(other) ⇒ Boolean

Returns:

  • (Boolean)


74
75
76
77
78
79
# File 'lib/m_array.rb', line 74

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

#superset?(other) ⇒ Boolean

Returns:

  • (Boolean)


81
82
83
# File 'lib/m_array.rb', line 81

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