[Ruby] What is the slice method? Let's solve the example and understand the difference from slice!

Overview

I have summarized the differences between slice and slice !. I would appreciate it if you could comment and point out any expressions that are easier to understand!

table of contents

--slice and slice! - slice - slice!

--Practice --Problem --Answer (commentary)

--Summary --References

slice and slice!

[Example] Extract the first element from the array and output the original array

point

--Methods that can retrieve specified elements from arrays and strings --Numeric when specifying elements --Starting from 0

slice


array = [0, 1, 2, 3, 4, 5]
num = array.slice(1)

puts num
puts array

#Terminal

#result of puts num
# 1
# =>Extract specified element

# puts array
# 0
# 1
# 2
# 3
# 4
# 5

slice!

--A method with an exclamation mark (!) Is called a destructive method. --Not only extract elements but also change their original shape --Delete the specified element

array = [0, 1, 2, 3, 4, 5]
num = array.slice!(1)

puts num
puts array

#Terminal

#result of puts num
# 1 
# =>The elements that can be taken do not change

# puts array
# 0
# 2
# 3
# 4
# 5
# =>Extracted element (1) has been deleted

Practice

Let's solve the problem using slice using fruits as an example.

problem

Let's create a method that deletes the nth character from any character and outputs that character!

Answer (commentary)


def str_check(string, num)  #String(string)And what number do you want to delete(num)Have received
  string.slice!(num - 1)    #Because it starts from the 0th-You can delete the characters you want to retrieve by doing 1.
  puts string               # slice!You can output characters other than those deleted with
end

#Method call
str_check('apple', 1)
str_check('orange', 2)
str_check('grape', 4)

#Terminal output result

# pple
# oange
# grae

Summary

--Elements can be retrieved from strings and arrays by using the slice method --Specify a number when you want to retrieve an element --Starting from 0th --You can change the shape of the original string or array by using the slice! method.

References

-Ruby 3.0.0 Reference Manual [slice]

-Ruby 3.0.0 Reference Manual [slice!]

Recommended Posts

[Ruby] What is the slice method? Let's solve the example and understand the difference from slice!
What is the difference between an action and an instance method?
Easy to understand the difference between Ruby instance method and class method.
What is the difference between SimpleDateFormat and DateTimeFormatter? ??
What is the difference between a class and a struct? ?? ??
What is the difference between System Spec and Feature Spec?
[Rails] What is the difference between redirect and render?
What is the difference between skip and pending? [RSpec]
What is the difference between Java EE and Jakarta EE?
What is the pluck method?
What is the initialize method?
[Rails] What is the difference between bundle install and bundle update?
Let's override the difference between == (identity) and equals method (equivalence)
Understand the difference between each_with_index and each.with_index
What is the difference between a web server and an application server?
[Java] What is the difference between form, entity and dto? [Bean]
[Ruby] What happens if the method self is used as the return value?
[Ruby] From the basics to the inject method
What is the main method in Java?
Extract characters from Ruby strings slice method
If it is Ruby, it is efficient to make it a method and stock the processing.
[Promotion of Ruby comprehension (1)] When switching from Java to Ruby, first understand the difference.
Shows how many years and months the difference from a particular date is
[Java] Understand the difference between List and Set
What is JSP? ~ Let's know the basics of JSP !! ~
Understand the difference between abstract classes and interfaces!
[Ruby basics] How to use the slice method
[Ruby] slice method
Get the value from the array and find out what number it is included in
[Ruby] Cut out a string using the slice method
[Ruby] I thought about the difference between each_with_index and each.with_index
Understanding ruby's "|| =" specification and what is Rails presence method?
About the difference between classes and instances in Ruby
What is the difference between the responsibilities of the domain layer and the application layer in the onion architecture [DDD]
I tried to solve the Ruby karaoke machine problem (there is an example of the answer)
I tried to solve the Ruby bonus drink problem (there is an example of the answer)
Now in the third year, the misunderstanding that I noticed is the difference between the equals method and ==
Let's understand the function!
[Ruby] What is true?
What is the LocalDateTime class? [Java beginner] -Date and time class-
[Ruby] Questions and verification about the number of method arguments
About the difference between "(double quotation)" and "single quotation" in Ruby
Jersey --What is Difference Between bind and bindAsContract in HK2?
What I did in the version upgrade from Ruby 2.5.2 to 2.7.1
[Ruby] About the difference between 2 dots and 3 dots of range object.
[Ruby comprehension check] Can you explain what is happening? && and and
The difference between programming with Ruby classes and programming without it
I tried to solve the Ruby bingo card creation problem (there is an example of the answer)
[Ruby] Difference between each method and for statement. The elements are taken out one by one and processed.