Ruby learning 6

Review the problems you made a mistake in the exercise

Currently, I am studying to get the Ruby engineer certification exam silver. It's about time for the exam, so I'll review the part that was caught in the mock exam.

Destructive method to constant

HOGE = "hoge"
HOGE.gsub!("hoge", "piyo")
print HOGE

I thought that a warning would appear in this output result (because the constant was changed), but it was different. Warning statements are made for constant "reassignment", so you can change the value without warning for destructive method calls.

Grammar using?

p ?A

It's short, but what's this grammar? Output one character after? As a character string. Error if you write more than 2 characters. I have no idea what it's used for in my brain.

Class inheritance

class Hoge < Kernel ; end

I get an error when I do this, because the Kernel is a module. Use include to inherit modules.

to_i method

p "123abc".to_i
=> 123

I understood that strings should be integers, but I didn't know when there were non-integers. If non-integers are mixed, the character up to one character other than the integer is output as an integer. Therefore, even in the case of "123abc123" to_i, 123 is output.


a = [1,2,3,4]
b = [1,3,5,7]

p a || b

=> [1,2,3,4]

I thought the output was [1,2,3,4,5,7], but that is the case with a | b. a ||In the case of b, when a is true, a is spit out without evaluating b and the process ends.

Regular expressions

puts "0123456789-".delete("^13-56-")

=> 13456-

"Delete matches 1 and 3 to 5 and 6-so you should delete this, I fully understand." ← I don't know If ^ is included in the regular expression, it will be "other than XX", so delete the character string "other than" that matches the above.

chr method ord method

p 75.chr
p "K".ord

=> "K"

chr method converts integers as character codes On the contrary, the ord method converts characters to integers that support character codes.


Recommended Posts

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 # 17 If Statements
Ruby Learning # 21 While Loops
Ruby Learning # 31 Object Methods
Ruby Learning # 35 Interactive Ruby (irb)
Ruby Learning # 9 Math & Numbers
Ruby Learning # 28 Handling Errors
Ruby Learning # 26 Reading Files
Ruby Learning # 23 For Loops
Ruby Learning # 16 Return Statement
Ruby on rails learning record -2020.10.03
Ruby on rails learning record -2020.10.04
Ruby on rails learning record -2020.10.05
Ruby on rails learning record -2020.10.09
Ruby Learning # 18 If Statements (Con't)
Ruby on Rails basic learning ①
Ruby Learning # 11 Building a Calculator
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.07 ①
Ruby on rails learning record -2020.10.06
Ruby Learning # 19 Building a Better Calculator
[Ruby] Array
Completed Progate Ruby Learning Course III
Ruby basics
Ruby basics
Learning Ruby with AtCoder 6 [Contest 168 Therefore]
Ruby Review 2
Ruby addition
Completed Progate Ruby Learning Course II
Ruby Learning # 22 Building a Guessing Game
Learning output ~ 11/3 ~
Ruby setting 2
Ruby problem ⑦
Maven learning
[Ruby] Block
Refactoring Ruby
ruby calculator
Ruby settings 1
Refactoring Ruby
Learning output
Ruby basics
Ruby memo