Module: Hammock::ArrayPatches::InstanceMethods
- Defined in:
- lib/hammock/monkey_patches/array.rb
Instance Method Summary collapse
- #as_index_for(&value_function) ⇒ Object
- #discard(*args, &block) ⇒ Object
- #discard!(*args, &block) ⇒ Object
-
#ends_with?(*other) ⇒ Boolean
Returns true iff
other
appears exactly at the end ofself
. - #hash_by(*methods, &block) ⇒ Object
- #pick(&block) ⇒ Object
- #rand ⇒ Object
- #remove_framework_backtrace ⇒ Object
- #squash ⇒ Object
- #squash! ⇒ Object
-
#starts_with?(*other) ⇒ Boolean
Returns true iff
other
appears exactly at the start ofself
.
Instance Method Details
#as_index_for(&value_function) ⇒ Object
51 52 53 54 55 56 |
# File 'lib/hammock/monkey_patches/array.rb', line 51 def as_index_for &value_function inject({}) do |accum, elem| accum[elem] = value_function.call(elem) accum end end |
#discard(*args, &block) ⇒ Object
42 43 44 |
# File 'lib/hammock/monkey_patches/array.rb', line 42 def discard *args, &block dup.discard! *args, &block end |
#discard!(*args, &block) ⇒ Object
45 46 47 48 49 |
# File 'lib/hammock/monkey_patches/array.rb', line 45 def discard! *args, &block args.each {|arg| delete arg } each {|i| delete(i) if yield(i) } if block_given? self end |
#ends_with?(*other) ⇒ Boolean
Returns true iff other
appears exactly at the end of self
.
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
64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 |
# File 'lib/hammock/monkey_patches/array.rb', line 64 def hash_by *methods, &block hsh = Hash.new {|h,k| h[k] = [] } this_method = methods.shift # First, hash this array into +hsh+. each {|i| hsh[this_method == :self ? i : 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 |
#pick(&block) ⇒ Object
25 26 27 28 29 |
# File 'lib/hammock/monkey_patches/array.rb', line 25 def pick &block value = nil detect {|i| value = yield(i) } value end |
#rand ⇒ Object
31 32 33 |
# File 'lib/hammock/monkey_patches/array.rb', line 31 def rand self[Kernel.rand(length)] end |
#remove_framework_backtrace ⇒ Object
58 59 60 61 62 |
# File 'lib/hammock/monkey_patches/array.rb', line 58 def remove_framework_backtrace reverse.drop_while {|step| !step.starts_with?(RAILS_ROOT) }.reverse end |
#squash ⇒ Object
35 36 37 |
# File 'lib/hammock/monkey_patches/array.rb', line 35 def squash dup.squash! end |
#squash! ⇒ Object
38 39 40 |
# File 'lib/hammock/monkey_patches/array.rb', line 38 def squash! delete_if &:blank? end |
#starts_with?(*other) ⇒ Boolean
Returns true iff other
appears exactly at the start of self
.
16 17 18 |
# File 'lib/hammock/monkey_patches/array.rb', line 16 def starts_with? *other self[0, other.length] == other end |