Class: Array

Inherits:
Object show all
Includes:
ActiveSupport::CoreExtensions::Array::Access, ActiveSupport::CoreExtensions::Array::Conversions, ActiveSupport::CoreExtensions::Array::ExtractOptions, ActiveSupport::CoreExtensions::Array::Grouping, ActiveSupport::CoreExtensions::Array::RandomAccess, English::Array
Defined in:
lib/gems/english-0.3.1/lib/english/array.rb,
lib/mack-facets/extensions/array.rb,
lib/gems/activesupport-2.2.2/lib/active_support/core_ext/array.rb,
lib/gems/activesupport-2.2.2/lib/active_support/core_ext/blank.rb

Overview

:nodoc:

Instance Method Summary collapse

Methods included from ActiveSupport::CoreExtensions::Array::RandomAccess

#rand

Methods included from ActiveSupport::CoreExtensions::Array::Grouping

#in_groups, #in_groups_of, #split

Methods included from ActiveSupport::CoreExtensions::Array::ExtractOptions

#extract_options!

Methods included from ActiveSupport::CoreExtensions::Array::Conversions

included, #to_formatted_s, #to_param, #to_query, #to_sentence, #to_xml

Methods included from ActiveSupport::CoreExtensions::Array::Access

#fifth, #forty_two, #fourth, #from, #second, #third, #to

Methods included from English::Array

#conjunction

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



86
87
88
89
90
# File 'lib/mack-facets/extensions/array.rb', line 86

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

#include?(pat) ⇒ Boolean

Returns:

  • (Boolean)


7
8
9
10
11
12
13
14
15
16
# File 'lib/mack-facets/extensions/array.rb', line 7

def include?(pat)
  if pat.is_a?(Regexp)
    self.each do |v|
      return true if v.to_s.match(pat)
    end
    return false
  else
    return _original_include?(pat)
  end
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



94
95
96
97
98
# File 'lib/mack-facets/extensions/array.rb', line 94

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



36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/mack-facets/extensions/array.rb', line 36

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

#random_eachObject

This allows you to easily recurse of the array randomly.



69
70
71
# File 'lib/mack-facets/extensions/array.rb', line 69

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

#randomize(&block) ⇒ Object

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



51
52
53
54
55
56
57
# File 'lib/mack-facets/extensions/array.rb', line 51

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.



60
61
62
63
64
65
66
# File 'lib/mack-facets/extensions/array.rb', line 60

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

#subset?(other) ⇒ Boolean

Returns:

  • (Boolean)


73
74
75
76
77
78
# File 'lib/mack-facets/extensions/array.rb', line 73

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

#superset?(other) ⇒ Boolean

Returns:

  • (Boolean)


80
81
82
# File 'lib/mack-facets/extensions/array.rb', line 80

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