Keep a record of the methods you recently learned while learning Ruby so you don't forget them.
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.
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.
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"]
Determines if the target number is even and returns a boolean value.
12.even?
#=> true
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