[For Ruby beginners] Explain how to freely delete array elements!

Looking for a "method to erase a particular element" in an array?

In this article, I will introduce the corresponding delete () and delete_if!

We will explain it as carefully as possible for Ruby beginners, so please stay with us until the end!

Delete () to delete a specific element of the array

First, I will introduce delete () that can delete the specified element.

This method can delete the element that matches the argument from the array just by specifying the element you want to delete as an argument.

delete()How to use


a = [1, 2, 3, 1, 2, 3]

#Remove only 3 elements from the array

a.delete(3)

a  #=> [1, 2, 1, 2]

The above is an example of using delete ().

First, pass an array as an argument to a, and instruct a to delete the element 3 using the delete () method.

And when executed correctly, the result is as above.

Delete_if that specifies the condition of the element of the array to be deleted

Next, I will explain about delete_if.

delete_if can delete a wide range of array elements compared to delete ().

For example, suppose you want to remove only odd elements in an array.

a = [1, 2, 3, 1, 2, 3]

#Remove only odd elements from the array

a.delete_if do |n|

 n.odd?

end

a  #=> [2, 2]

First, prepare an array.

It then commands delete_if to delete the element if it is odd.

As a result, odd numbers are removed.

Summary

It is convenient to remember such a method when you want to operate an array in a terminal etc.

It's a method that beginners tend to forget, so if you remember it, you might be able to differentiate yourself from others!

I'm thinking of taking up such information as an article from now on, so I hope you'll see it if you like!

see you!

Recommended Posts

[For Ruby beginners] Explain how to freely delete array elements!
[Ruby] How to use slice for beginners
[For beginners] How to implement the delete function
[Java] [For beginners] How to insert elements directly in a 2D array
For Java beginners: List, Map, Iterator / Array ... How to convert?
[For beginners] How to get the Ruby delayed railway line name
[For beginners] How to debug in Eclipse
How to build a Ruby on Rails environment using Docker (for Docker beginners)
How to use an array for HashMap keys
How to create pagination for a "kaminari" array
[For super beginners] How to use autofocus: true
How to implement Pagination in GraphQL (for ruby)
Scraping for beginners (Ruby)
How to implement login request processing (Rails / for beginners)
Rails / Ruby: How to get HTML text for Mail
[For beginners] How to operate Stream API after Java 8
[Spring Boot] How to create a project (for beginners)
How to delete custom Adapter elements using a custom model
How to use GitHub for super beginners (team development)
Explanation of Ruby on rails for beginners ④ ~ Naming convention and how to use form_Tag ~
How to delete BOM (UTF-8)
[Ruby] Searching for elements in an array using binary search
How to divide a two-dimensional array into four with ruby
How to use Ruby return
How to change a string in an array to a number in Ruby
[Ruby] How to comment out
How to retrieve the hash value in an array in Ruby
Learning Ruby with AtCoder 13 How to make a two-dimensional array
How to add elements without specifying the length of the array
Ruby: How to use cookies
How to output array values without using a for statement
[Ruby] How to write blocks
[Ruby] Get array elements alternately.
How to initialize Java array
[Rails] How to display error messages for comment function (for beginners)
[Ruby] How to batch convert strings in an array to numbers
[Ruby] How to count even or odd numbers in an array
Must-see for beginners! How to manage your Xcode project on Github
[RSpec on Rails] How to write test code for beginners by beginners
[For beginners] Ruby is said to be ruby, but what about it?
[Tips] How to solve problems with XCode and Swift for beginners
Explanation of Ruby on rails for beginners ⑤ ~ Edit and delete database ~
[Ruby on Rails] How to avoid creating unnecessary routes for devise
How to install JMeter for Mac
How to iterate infinitely in Ruby
How to install ruby through rbenv
How to use Ruby on Rails
How to install Bootstrap in Ruby
[Ruby] How to use any? Method
[Beginner] How to delete NO FILE
How to add the delete function
How to use Ruby inject method
How to make a Java array
How to execute Ruby irb (interactive ruby)
[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 build an environment for any version of Ruby using rbenv
(For beginners) [Rails] Time saving tech! How to install and use slim
[For Rails beginners] Summary of how to use RSpec (get an overview)
How to insert processing with any number of elements in iterative processing in Ruby
How to use binding.pry for view files