Module: SelectBy::CoreExtensions::Array::SelectingBy
- Defined in:
- lib/select_by/core_extensions/array/selecting_by.rb
Defined Under Namespace
Classes: InvalidArgumentError, InvalidSelectingMethodError
Constant Summary collapse
- SELECTING_METHODS =
[:select, :reject, :detect, :partition, :find_all, :find]
Instance Method Summary collapse
- #detect_by(hash_or_method = nil, &block) ⇒ Object (also: #find_by)
- #partition_by(hash_or_method = nil, &block) ⇒ Object
-
#reject_by(hash_or_method = nil, &block) ⇒ Object
works the same as select_by but returns elements that do not evaluate to true.
-
#select_by(hash_or_method = nil, &block) ⇒ Object
(also: #find_all_by)
if a hash is provided use each hash key as a method name and value as what the method should return if any of key/value pairs are not callable or do not match the reciever based on equality then reject the element of the array.
Instance Method Details
#detect_by(hash_or_method = nil, &block) ⇒ Object Also known as: find_by
55 56 57 58 59 60 61 62 63 |
# File 'lib/select_by/core_extensions/array/selecting_by.rb', line 55 def detect_by(hash_or_method = nil, &block) array = if hash_or_method.nil? self else selecting_by(:select, hash_or_method, true) end (block_given? ? array.map(&block) : array).first end |
#partition_by(hash_or_method = nil, &block) ⇒ Object
45 46 47 48 49 50 51 52 53 |
# File 'lib/select_by/core_extensions/array/selecting_by.rb', line 45 def partition_by(hash_or_method = nil, &block) array = if hash_or_method.nil? [self, []] else selecting_by(:partition, hash_or_method) end block_given? ? array.map{|inner_array| inner_array.map(&block)} : array end |
#reject_by(hash_or_method = nil, &block) ⇒ Object
works the same as select_by but returns elements that do not evaluate to true
41 42 43 |
# File 'lib/select_by/core_extensions/array/selecting_by.rb', line 41 def reject_by(hash_or_method = nil, &block) select_or_reject_by(:reject, hash_or_method, &block) end |
#select_by(hash_or_method = nil, &block) ⇒ Object Also known as: find_all_by
if a hash is provided use each hash key as a method name and value as what the method should return if any of key/value pairs are not callable or do not match the reciever based on equality then reject the element of the array. if argument that is provided is a callable method (calling to_s on the object returns a method the element responds to) then call that method on each element only returning the elements that return a truthy value (similar to select(&:method)).
if you provide a code block the method will apply that code block to each returned element
[“Smith”, “peter”].detect_by(:size => 5) # => “Smith” [“Smith”, “peter”].detect_by(:size => 5, &:downcase) # => “smith”
- “Smith”, “peter”].select_by(:size => 5) {|name| name.downcase} # => [“smith”, “peter”
- “Smith”, “peter”].reject_by(:size => 5, &:downcase) # => [
-
when the object does not respond to the key the selecting block assumes it is falsey
- “Smith”, “peter”].reject_by(:peter => 5, &:downcase) # => [“smith”, “peter”
- “Smith”, “peter”].select_by(:peter => 5, &:downcase) # => [
- “Smith”, “peter”].reject_by(:peter => 5, &:downcase) # => [“smith”, “peter”
36 37 38 |
# File 'lib/select_by/core_extensions/array/selecting_by.rb', line 36 def select_by(hash_or_method = nil, &block) select_or_reject_by(:select, hash_or_method, &block) end |