[Ruby] How to extract a specific value from an array under multiple conditions [select / each]

To be able to

You can use multiple conditions to find a particular value in an array.

Example) From the numbers 1 to 10, derive the one that is 5 or less and is divided by 2. You will be able to solve problems like this.

Method

Use Array # each and Array # select.

numbers = (1..10).to_a
rule = [["<", 5],["%", 2]]

rule.each do |b|
    numbers.select! do |a|
        if b[0] == "<" 
            a <= b[1]
        else
            a % b[1] == 0
        end
    end
end

p numbers # [2,4]

What do you mean

It is necessary to turn the condition (rule this time) with each and put the one you want to determine (number this time) in it. I used to be in trouble on the contrary, and recently I noticed this method, so I summarized it.

I would be happy if it could be a solution for someone who was stuck with the same problem.

Recommended Posts

[Ruby] How to extract a specific value from an array under multiple conditions [select / each]
How to change a string in an array to a number in Ruby
How to retrieve the hash value in an array in Ruby
Terraform: (and Ansible: and Chef :) Extract a hash from an array where a key has a specific value.
[Java] Get a random value from an array
How to start a subscript from an arbitrary number in Ruby iterative processing
[Note] [Beginner] How to write when changing the value of an array element in a Ruby iterative statement
How to use an array for a TreeMap key
How to output standard from an array with forEach
[Java] How to erase a specific character from a character string
Ruby Regular Expression Extracts from a specific string to a string
[Ruby] How to split each GraphQL query into a file
How to divide a two-dimensional array into four with ruby
[iOS] [Objective-C] How to update a widget from an Objective-C app
How to create a form to select a date from the calendar
[Ruby] Extract only the elements that match the conditions and create a new array. filter and select
Learning Ruby with AtCoder 13 How to make a two-dimensional array
How to make th: value of select have multiple values
[ruby] Creating a program that responds only to specific conditions
[Integration test code] How to select an element from date_select
[Ruby] How to batch convert strings in an array to numbers
[Java] How to search for a value in an array (or list) with the contains method
[Ruby] How to count even or odd numbers in an array
[Ruby] I want to put an array in a variable. I want to convert to an array
How to output the value when there is an array in the array
[Swift5] How to communicate from ViewController to Model and pass a value
Effective Java Item 25 Select a list from an array First half
How to make a Java array
[Ruby] How to use is_a? Differences from kind_of? and instance_of ?. How to check the type and return a boolean value.
How to return a value from Model to Controller using the [Swift5] protocol
[Swift] Summary of how to remove elements from an array (personal note)
[Java] How to turn a two-dimensional array with an extended for statement
How to add a new hash / array
Extract each digit number from a 3-digit integer
[Ruby] I want to make an array from a character string with the split method. And vice versa.
[Ruby] Get unique elements from an array
Output the sum of each name and its contents from a multiple array
[Java] How to convert one element of a String type array to an Int type
How to check if an instance variable is defined in a Ruby class
How to make a judgment method to search for an arbitrary character in an array
How to get an arbitrary digit from a number of 2 or more digits! !!
[Ruby On Rails] How to retrieve and display column information of multiple records linked to a specific id at once
[Ruby] How to convert from lowercase to uppercase and from uppercase to lowercase
[Ruby] Calculation by extracting elements from an array
How to get a heapdump from a Docker container
How to use an array for HashMap keys
How to create pagination for a "kaminari" array
How to create search conditions involving multiple models
[Ruby] How to generate a random alphabet string
[ruby] How to receive values from standard input?
How to find the total value, average value, etc. of a two-dimensional array (multidimensional array)-java
Be careful when deleting multiple elements from an array etc. with a for statement
How to request by passing an array to the query with HTTP Client of Ruby
[ruby] How to assign a value to a hash by referring to the value and key of another hash