Currently, I am studying to get the Ruby engineer certification exam silver. I still have a lot of understanding of the language, so I will output from the basics.
A method that deletes the last character of a string.
str = "abeshi".chop
p str
=> "abesh"
However, if the end is \ r \ n, both characters will be deleted. I think this is because only in the Windows environment, the newline character requires both \ r and \ n.
str = "tawaba\r\n".chop
p str
=> "tawaba"
Specify an integer argument and return the value corresponding to the index of the specified argument from an array (Array) or a character string (String).
array = ["abeshi", "tawaba", "uwaraba", "howatya"]
p array.slice(2)
=> "uwaraba"
Returns values within the range by separating the arguments with commas.
array = ["abeshi", "tawaba", "uwaraba", "howatya"]
p array.slice(1,3)
=> ["tawaba", "uwaraba", "howatya"]
A method that sorts the contents of an array in order.
num = [2, 1, 4, 8, 9, 7, 6, 3, 5]
p num.sort
=> [1, 2, 3, 4, 5, 6, 7, 8, 9]
By setting sort !, you can sort destructively.
A method that modifies the object itself
The pattern is the same as the contents of num.
num = [2, 1, 4 ,8 , 9, 7, 6, 3, 5]
num.sort
p num
=> [2, 1, 4 ,8 , 9, 7, 6, 3, 5]
A pattern in which the contents of num have been changed.
num = [2, 1, 4 ,8 , 9, 7, 6, 3, 5]
num.sort!
p num
=> [1, 2, 3, 4, 5, 6, 7, 8, 9]
Recommended Posts