Module: Hammock::ArrayPatches::InstanceMethods

Defined in:
lib/hammock/monkey_patches/array.rb

Instance Method Summary collapse

Instance Method Details

#as_index_for(&value_function) ⇒ Object



40
41
42
43
44
45
# File 'lib/hammock/monkey_patches/array.rb', line 40

def as_index_for &value_function
  inject({}) do |accum, elem|
    accum[elem] = value_function.call(elem)
    accum
  end
end

#discard(*args) ⇒ Object



32
33
34
# File 'lib/hammock/monkey_patches/array.rb', line 32

def discard *args
  self.dup.discard! *args
end

#discard!(*args) ⇒ Object



35
36
37
38
# File 'lib/hammock/monkey_patches/array.rb', line 35

def discard! *args
  args.each {|arg| self.delete arg }
  self
end

#ends_with?(*other) ⇒ Boolean

Returns true iff other appears exactly at the end of self.

Returns:

  • (Boolean)


21
22
23
# File 'lib/hammock/monkey_patches/array.rb', line 21

def ends_with? *other
  self[-other.length, other.length] == other
end

#hash_by(*methods, &block) ⇒ Object



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/hammock/monkey_patches/array.rb', line 53

def hash_by *methods, &block
  hsh = Hash.new {|h,k| h[k] = [] }
  this_method = methods.shift

  # First, hash this array into +hsh+.
  self.each {|i| hsh[i.send(this_method)] << i }

  if methods.empty?
    # If there are no methods remaining, yield this group to the block if required.
    hsh.each_pair {|k,v| hsh[k] = yield(hsh[k]) } if block_given?
  else
    # Recursively hash remaining methods.
    hsh.each_pair {|k,v| hsh[k] = v.hash_by(*methods, &block) }
  end

  hsh
end

#remove_framework_backtraceObject



47
48
49
50
51
# File 'lib/hammock/monkey_patches/array.rb', line 47

def remove_framework_backtrace
  reverse.drop_while {|step|
    !step.starts_with?(RAILS_ROOT)
  }.reverse
end

#squashObject



25
26
27
# File 'lib/hammock/monkey_patches/array.rb', line 25

def squash
  self.dup.squash!
end

#squash!Object



28
29
30
# File 'lib/hammock/monkey_patches/array.rb', line 28

def squash!
  self.delete_if &:blank?
end

#starts_with?(*other) ⇒ Boolean

Returns true iff other appears exactly at the start of self.

Returns:

  • (Boolean)


16
17
18
# File 'lib/hammock/monkey_patches/array.rb', line 16

def starts_with? *other
  self[0, other.length] == other
end