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
- #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
47 48 49 50 51 52 |
# File 'lib/hammock/monkey_patches/array.rb', line 47 def as_index_for &value_function inject({}) do |accum, elem| accum[elem] = value_function.call(elem) accum end end |
#discard(*args, &block) ⇒ Object
38 39 40 |
# File 'lib/hammock/monkey_patches/array.rb', line 38 def discard *args, &block dup.discard! *args, &block end |
#discard!(*args, &block) ⇒ Object
41 42 43 44 45 |
# File 'lib/hammock/monkey_patches/array.rb', line 41 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
60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 |
# File 'lib/hammock/monkey_patches/array.rb', line 60 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[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 |
#remove_framework_backtrace ⇒ Object
54 55 56 57 58 |
# File 'lib/hammock/monkey_patches/array.rb', line 54 def remove_framework_backtrace reverse.drop_while {|step| !step.starts_with?(RAILS_ROOT) }.reverse end |
#squash ⇒ Object
31 32 33 |
# File 'lib/hammock/monkey_patches/array.rb', line 31 def squash dup.squash! end |
#squash! ⇒ Object
34 35 36 |
# File 'lib/hammock/monkey_patches/array.rb', line 34 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 |