Ruby learning 1

Methods and so on 1

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.

chop method

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"

slice method

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"]

sort method

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.

Supplement: What is a destructive method?

A method that modifies the object itself

Non-destructive method

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]

Destructive method

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

Ruby learning 4
Ruby learning 5
Ruby learning 3
Ruby learning 2
Ruby learning 6
Ruby learning 1
Ruby Learning # 25 Comments
Ruby Learning # 13 Arrays
Ruby Learning # 1 Introduction
Ruby Learning # 34 Modules
Ruby Learning # 14 Hashes
Ruby Learning # 33 Inheritance
Ruby Learning # 15 Methods
Ruby Learning # 30 Initialize Method
Ruby learning points (basic)
Ruby Learning # 29 Classes & Objects
Ruby Learning # 20 Case Expressions
Ruby Learning # 24 Exponent Method
Ruby Learning # 17 If Statements
Ruby Learning # 21 While Loops
Ruby Learning # 31 Object Methods
Ruby Learning # 27 Writing Files
Ruby Learning # 35 Interactive Ruby (irb)
Ruby Learning # 9 Math & Numbers
Ruby Learning # 28 Handling Errors
Ruby Learning # 26 Reading Files
Ruby Learning # 2 Drawing a Shape
Ruby on rails learning record -2020.10.03
Ruby on rails learning record -2020.10.05
Ruby on rails learning record -2020.10.09
Ruby on Rails basic learning ①
Ruby Learning # 99 Personal Programming Notebook
Ruby on rails learning record-2020.10.07 ②
Ruby Learning # 10 Getting User Input
Ruby Learning # 32 Building a Quiz
Ruby Learning # 8 Working With String
Ruby on rails learning record -2020.10.06
Java learning (0)
[Ruby] Array
Completed Progate Ruby Learning Course III
Ruby basics
Learning Ruby with AtCoder 6 [Contest 168 Therefore]
Ruby Review 2
Ruby addition
Refactoring Ruby
Completed Progate Ruby Learning Course II
Servlet learning
Learning output ~ 11/3 ~
Ruby setting 2
Ruby problem ⑦
Maven learning
[Ruby] Block
Refactoring Ruby
Ruby settings 1
Refactoring Ruby
Learning output
Ruby basics
Ruby memo
Ruby Review 1
[Ruby] Module
Learning Ruby with AtCoder 7 [Contest 168 Triple Dots]