Ruby array methods that can be used with Rails (other than each)

Ruby array methods covered in this article

  1. select method
  2. reject method
  3. keep_if method
  4. delete_if method
  5. find method
  6. map method

Ruby version: Confirmed operation with 2.5.1.

The reason for writing

Until now, I've only used the ʻeach method` when it comes to array methods. (I'm a beginner about 2 months after I started learning programming in earnest.) When developing a personal application with Rails, I often say "I want to process the array data obtained from the DB table well and display only the necessary elements, but I can't do it well". While researching various things to solve the problem, I learned a lot, "Is there such a convenient method!", So I will write it as a memorandum as well as to establish my knowledge.

I hope it will be helpful to anyone.

Referenced

Junichi Ito's "Introduction to Ruby for Professionals"

Mr. Ito's Qiita articles, books, etc. are very easy to understand and are always indebted.

Array prepared as a sample for operation check this time

users = [
  {name: 'Matsuda', score: 80},
  {name: 'Otsuka', score: 60},
  {name: 'Koike', score: 90},
  {name: 'Inoue', score: 40},
  {name: 'Matsumoto', score: 50},
  {name: 'Fujisawa', score: 85}
]

I will explain each method while checking the operation below.

1. select method

The select method performs the processing specified in the block for each element of the array. Returns an array of only the elements whose return value is true.

Actual operation confirmed

select.rb


#I want only users with a score of 70 or higher!

users = [
  {name: 'Matsuda', score: 80},
  {name: 'Otsuka', score: 60},
  {name: 'Koike', score: 90},
  {name: 'Inoue', score: 40},
  {name: 'Matsumoto', score: 50},
  {name: 'Fujisawa', score: 85}
]

#Collect only users with score 70 or higher using select
goukaku_users = users.select { |user| user[:score] >= 70 }

#Check the contents of the newly created variable
p goukaku_users
# =>[{:name=>"Matsuda", :score=>80}, {:name=>"Koike", :score=>90}, {:name=>"Fujisawa", :score=>85}]

#I was able to create an array only for users with a score of 70 or higher!

2. reject method

The reject method is the opposite of the select method, and is a method that returns an array that collects only the elements whose return value of processing in the block is false.

Actual operation confirmed

reject.rb


 #I want only users with a score of 70 or less!

users = [
  {name: 'Matsuda', score: 80},
  {name: 'Otsuka', score: 60},
  {name: 'Koike', score: 90},
  {name: 'Inoue', score: 40},
  {name: 'Matsumoto', score: 50},
  {name: 'Fujisawa', score: 85}
]

 #Collect only users with score 70 or less with reject method
fugoukaku_users = users.reject { |user| user[:score] > 70 }

#Check the contents of the newly created variable
p fugoukaku_users
# => [{:name=>"Otsuka", :score=>60}, {:name=>"Inoue", :score=>40}, {:name=>"Matsumoto", :score=>50}]

#I was able to create an array only for users with a score of 70 or less!

3. keep_if method

The keep_if method also retrieves each element of the array in order and performs the specified processing in the block. As a result, if the return value is true, the element passed to the block is left in the array, and if the return value is false, it is deleted from the array.

Actual operation confirmed

keep_if.rb


#I want only users with a score of 70 or higher!

users = [
  {name: 'Matsuda', score: 80},
  {name: 'Otsuka', score: 60},
  {name: 'Koike', score: 90},
  {name: 'Inoue', score: 40},
  {name: 'Matsumoto', score: 50},
  {name: 'Fujisawa', score: 85}
]

 # keep_Process only users with score of 70 or more with if method
 #Removed users element with score less than 70
users.keep_if { |user| user[:score] >= 70 }

# keep_After the if method
p users
# => [{:name=>"Matsuda", :score=>80}, {:name=>"Koike", :score=>90}, {:name=>"Fujisawa", :score=>85}]

#The original array users can now be an array of only users with a score of 70 or higher!

4. delete_if method

The delete_if method also retrieves each element of the array in order and performs the specified processing in the block. As a result, if the return value is true, the element passed to the block is deleted from the original array. If the return value is false, leave it in the array.

Actual operation confirmed

delete_if.rb


#I want only users with a score of 70 or less!

users = [
  {name: 'Matsuda', score: 80},
  {name: 'Otsuka', score: 60},
  {name: 'Koike', score: 90},
  {name: 'Inoue', score: 40},
  {name: 'Matsumoto', score: 50},
  {name: 'Fujisawa', score: 85}
]

 # delete_Process only users with score of 70 or less with if method
 #Removed users element with score greater than 70
users.delete_if { |user| user[:score] > 70 }

# delete_After the if method
p users
# => [{:name=>"Otsuka", :score=>60}, {:name=>"Inoue", :score=>40}, {:name=>"Matsumoto", :score=>50}]

#The original array users has been changed to an array of only users with a score of 70 or less!

The keep_if and delete_if methods are the same as the select method and reject method explained in 1 and 2 !? I thought, but when compared

select / reject The original array remains the same, but the elements that meet the conditions are collected and a completely new array is returned. Therefore, when reusing it, assign it to a variable. delete_if / keep_if Delete the elements that meet the conditions from the original array. In other words, the shape of the original array itself is changed. Therefore, it can be reused with the original variable name.

It's similar, but there seems to be such a difference.

5. find method

The find method returns the first element for which the return value of the process specified in the block is true.

Actual operation confirmed

find.rb


#I want to get the first user with a score of 70 or more in the array users!

users = [
  {name: 'Matsuda', score: 80},
  {name: 'Otsuka', score: 60},
  {name: 'Koike', score: 90},
  {name: 'Inoue', score: 40},
  {name: 'Matsumoto', score: 50},
  {name: 'Fujisawa', score: 85}
]

 #Get the first user with a score of 70 or higher with the find method
goukaku_user = users.find { |user| user[:score] >= 70 }

#Check the contents of the newly created variable
p goukaku_user
# => {:name=>"Matsuda", :score=>80}

#I was able to get the first user with a score of 70 or higher in the array!

Just in case, try changing the condition of the score in the block from 70 to 85 and verify again.

find.rb


#I want to get the first user with a score of 85 or higher in the array users!

users = [
  {name: 'Matsuda', score: 80},
  {name: 'Otsuka', score: 60},
  {name: 'Koike', score: 90},
  {name: 'Inoue', score: 40},
  {name: 'Matsumoto', score: 50},
  {name: 'Fujisawa', score: 85}
]

#Get the first user with a score of 85 or higher with the find method
goukaku_user = users.find { |user| user[:score] >= 85 }

#Check the contents of the newly created variable
p goukaku_user
# => {:name=>"Koike", :score=>90}

#I was able to get the first user with a score of 85 or higher in the array!

6. map method

The map method returns the return value as a new array as a result of the processing specified in the block for each element.

Actual operation confirmed

map.rb


#I want to add "san" to the name of the array users!

users = [
  {name: 'Matsuda', score: 80},
  {name: 'Otsuka', score: 60},
  {name: 'Koike', score: 90},
  {name: 'Inoue', score: 40},
  {name: 'Matsumoto', score: 50},
  {name: 'Fujisawa', score: 85}
]

san_users = users.map { |user| user[:name] + 'Mr.' }

#Check the contents of the newly created variable
p san_users
# => ["Mr. Matsuda", "Mr. Otsuka", "Mr. Koike", "Inoue", "Mr. Matsumoto", "Mr. Fujisawa"]

that? It was a little different from what I expected. The map method seems to behave a little differently from the array methods described so far.

In particular, Instead of evaluating the elements of the original array and transforming the elements to return the values, the return values of the processing results are stored in the array one by one and the newly created array is returned.

If you know the javaScript map method, it may be easier to understand.

map.rb


#So if you want to return in hash format while maintaining the original shape, write as follows
san_users = users.map { |user| {name: user[:name]+'Mr.', score: user[:score]} }

p san_users
# => [{:name=>"Mr. Matsuda", :score=>80}, {:name=>"Mr. Otsuka", :score=>60}, {:name=>"Mr. Koike", :score=>90}, {:name=>"Inoue", :score=>40}, {:name=>"Mr. Matsumoto", :score=>50}, {:name=>"Mr. Fujisawa", :score=>85}]

#I was able to create an array with "san" added to the name!

Finally

Thank you for reading the article.

I am keenly aware that developing with Rails and knowing that I do not know the method will greatly affect the range of things I can do and code refactoring. I am feeling the importance of the basics again.

If you find any mistakes in the content, please feel free to point out and give us your opinion!

Recommended Posts

Ruby array methods that can be used with Rails (other than each)
[Ruby] Methods that can be used with strings
Organize methods that can be used with StringUtils
SwiftUI View that can be used in combination with other frameworks
[Rails] "pry-rails" that can be used when saving with the create method
Range where variables can be used with ruby [Scope]
Scala String can be used other than java.lang.String method
Summary of css selectors that can be used with Nookogiri
Create a page control that can be used with RecyclerView
Firebase-Realtime Database on Android that can be used with copy
Ruby on Rails 5 quick learning practice guide that can be used in the field Summary
Ruby methods often used in Rails
Can statements other than loops be labeled?
Until ruby can be used on windows ...
Simple slot machine implementation that can be used with copy and paste
About the problem that the server can not be started with rails s
Performance analysis and failure diagnostic tools that can be used with OpenJDK
[Rails 6] method :: delete cannot be used with link_to
A ruby ​​script that creates an rsa private key that can be used with OpenSSL from any two prime numbers
About the matter that hidden_field can be used insanely
Convenient shortcut keys that can be used in Eclipse
Syntax and exception occurrence conditions that can be used when comparing with null in Java
Four-in-a-row with gravity that can be played on the console
[rails] Problems that cannot be registered / logged in with devise
Static analysis tool that can be used on GitHub [Java version]
Build an environment where pip3 can be used with CentOS7 + Python3
Summary of ORM "uroboroSQL" that can be used in enterprise Java
File form status check sheet that can be deleted with thumbnails
[Ruby on Rails] Posting function that only logged-in users can post
Power skills that can be used quickly at any time --Reflection
Summary of JDK that can be installed with Homebrew (as of November 2019)
Introduction to Java that can be understood even with Krillin (Part 1)
[Java 8] Until converting standard input that can be used in coding tests into a list or array