class Array
Public Instance Methods
extract!() { |element| ... }
click to toggle source
Removes and returns the elements for which the block returns a true value. If no block is given, an Enumerator is returned instead.
numbers = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] odd_numbers = numbers.extract! { |number| number.odd? } # => [1, 3, 5, 7, 9] numbers # => [0, 2, 4, 6, 8]
# File lib/sigterm_extensions/core_ext/array/extract.rb, line 8 def extract! return to_enum(:extract!) { size } unless block_given? extracted_elements = [] reject! do |element| extracted_elements << element if yield(element) end extracted_elements end
extract_options!()
click to toggle source
Extracts options from a set of arguments. Removes and returns the last element in the array if it's a hash, otherwise returns a blank hash.
def options(*args) args.extract_options! end options(1, 2) # => {} options(1, 2, a: :b) # => {:a=>:b}
# File lib/sigterm_extensions/core_ext/array/extract_options.rb, line 22 def extract_options! if last.is_a?(Hash) && last.extractable_options? pop else {} end end