Ruby learning 5

Methods and so on 5

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.

Decimal rounding, rounding up, rounding

ceil = round up to the nearest whole number

irb(main):001:0> 1.9.ceil
=> 2

floor = Truncate after the decimal point

irb(main):004:0> 1.9.floor
=> 1

round = Round to the nearest whole number (strictly, returns the closest integer)

irb(main):006:0> 1.5.round
=> 2
irb(main):007:0> 1.4.round
=> 1

join method

The elements of the array are separated by the character specified by the argument, and the combined character string is returned.

arr = ["Useless", "Useless", "Useless", "Uselessァ", "Tsu!"]
p arr.join("!")

=> "Useless!Useless!Useless!Uselessァ!!"

inject method and conditional operator

I'm stuck with a mock question. First from the code.

numbers = [3,89,40,39,29,10,50,59,69]
num = numbers.inject do |i,j|
  i > j ? i : j
end

When an argument is attached, the inject method assigns the argument to the first block variable (i) and the first element of the array to the second block variable (j). Execute the expression after do. When the execution is finished, assign the calculation result to i and the next element of the array to j. Execute the expression. After that, it repeats and returns the result when the elements of the array reach the end. If no argument is given, the first and second elements of the array are assigned before starting.

Conditional operator

 i > j ? i : j

After executing the expression on the left side (from? To the left) that asks the truth, put the value returned when true is on the left of (:) and the value returned when false is placed on the right of (:). So this expression means "returns i if i is greater than j, j otherwise". so, Since this is executed by the inject method, it becomes "code that compares the elements of the array, leaves a large number, and finally returns the largest number in the array".

step method

1.step(100, 2) do |n|
  puts n
end

Starting from the value of the receiver, the expression is executed in increments of the second argument until the value becomes the first argument. The numerical value of the receiver is entered as the initial value in the block variable n. When you translate the code, "After outputting the number" 1 "of the receiver (puts n), add 2 to the number and execute again (output 3). Run until this number reaches 100. " This time, odd numbers are output, and at the end, 99.2 is added and it exceeds 100, so it ends here. What you are doing is an odd number of outputs from 1 to 100.

Recommended Posts

Ruby learning 4
Ruby learning 5
Ruby learning 3
Ruby learning 2
Ruby learning 6
Ruby learning 1
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 # 23 For Loops
Ruby Learning # 16 Return Statement
Ruby Learning # 2 Drawing a Shape
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
Java learning (0)
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
Refactoring Ruby
Completed Progate Ruby Learning Course II
Servlet learning
Ruby Learning # 22 Building a Guessing Game
Learning output ~ 11/3 ~
Ruby setting 2
Ruby problem ⑦
Maven learning
[Ruby] Block
Refactoring Ruby
ruby calculator