[RUBY] Recently learned method, memorandum ①

Introduction

Keep a record of the methods you recently learned while learning Ruby so you don't forget them.

Recently used method

slice method

It is possible to extract the defined elements from arrays and character strings.

string = "abcde"
str = string.slice(4)
puts str
#=> "d"

puts string
#=> "abcde"

The original string does not change.

slice! method

string = "abcde"
str = string.slice!(4)
puts str
#=> "d"

puts string
#=> "abce"

Methods with (!) Are called destructive methods. As the name implies, it destroys. The slice! method removes the specified element from the original array or string.

scan method

Counts the character string specified by the argument from the target element and returns it as an array.

str = "abcdabcdabcd"
str.scan("ab")
#=> ["ab", "ab", "ab"]

even? method

Determines if the target number is even and returns a boolean value.

12.even?
#=> true

include? method

Determines if the specified element is included in the array and returns a boolean value.

array = ["ab", "cd"]
array.include?("ab")
#=> true
array.include?("ef")
#=> false

Recommended Posts

Recently learned method, memorandum ①
[Ruby] Method memorandum
Method
memorandum
memorandum