About the [ruby] operator


Good evening I go to a certain school I just graduated last month, but I am outputting what I learned. Last time, I made a method that can be used with [Ruby] strings, so this time I would like to touch on operators!

What is an operator?

Operators are symbols used for programming calculations and comparisons. Please remember this because it's a mess

I said, but most of them were done in elementary school + or something, so you don't have to be prepared

- subtraction
* multiplication
/ division
% Surplus
** Exponentiation
Math::PI Constant to match pi
Math.sqrt Find the square root

I've been learning programming for half a year, but I didn't use it, so please keep it in the corner of your head.

=> nil
irb(main):115:0> 1 + 1
=> 2
irb(main):116:0> 1 - 1
=> 0
irb(main):117:0> 1 * 2
=> 2
irb(main):118:0> 4 / 2
=> 2

Needless to say, the function of four arithmetic operations

irb(main):119:0> 3 / 4
=> 0

By the way, the decimal point is not displayed Because this number is an Integer class, and Integer represents a number as an integer.

irb(main):122:0> n = 3 / 4
=> 0
irb(main):123:0> n.class
=> Integer

When I checked the assigned n with class, I was able to confirm that it was "Integer".

% (Remainder)

It was described as a remainder in the above figure, but what is the story? The surplus is the remainder of the division.

irb(main):128:0> 9 % 7
=> 2

9/7 = 1 ・ ・ ・ Because it is a remainder of 2, 2 is displayed.


Exponentiation is simply exponentiation.

irb(main):131:0> 4 ** 2
=> 16

If you look at this, you can see 4 * 4 = 16


Math :: PI is pi But you can do finer calculations than 3.14

irb(main):138:0>  3 * 3 *3.14
=> 28.26
irb(main):139:0>  3 * 3 * Math::PI
=> 28.274333882308138

As you can see, the second decimal place is different

Math.sqrt This is a method to find the square root

Can be used with Math.sqrt (numbers)

irb(main):140:0> Math.sqrt(4)
=> 2.0

I asked for 4 but 2 for √4

irb(main):143:0> Math.sqrt(2)
=> 1.4142135623730951

People, people, people, nostalgic


I'll use operators a lot in the future so let's remember But it's enough to keep in mind the power below the power

Recommended Posts

About the [ruby] operator
About Ruby product operator (&) and sum operator (|)
About Ruby symbols
About ruby ​​form
About Ruby Hashes
About the method
About Ruby arrays
About Ruby inheritance
About ruby block
About Ruby Hashes
About Ruby Symbols
About Ruby variables
About Ruby methods
Output about the method # 2
About Ruby Kernel Module
About Ruby error messages
About the StringBuilder class
[Ruby] Operator ":: (colon colon)" [constant]
Ruby logical operator drill
Commentary: About the interface
About Ruby exception handling
About Ruby Hashes (continued)
About the asset pipeline
About the function double-java
About eval in Ruby
About Ruby instance methods
About the length method
About the Kernel module
About the authenticate method.
[Ruby] About instance generation
About the ancestors method
[Output] About the database
About the to_s method.
Thinking about logic Ruby
[Ruby] I thought about the difference between each_with_index and each.with_index
[Ruby] When the conditional operator continues twice (or more)
About the difference between classes and instances in Ruby
[Technical memo] About the advantages and disadvantages of Ruby
About the handling of Null
About specifying the JAXRS path
Output about the method Part 1
A note about the scope
About the description of Docker-compose.yml
Explanation about Ruby Range object
About the File :: Stat class
[Ruby on Rails] about has_secure_password
About regular expressions in Ruby
About Ruby hashes and symbols
About the same and equivalent
About the Android life cycle
Ruby About various iterative processes
About the explanation about functional type
About Ruby and object model
Is the ternary operator evil?
Consideration about the times method
About Ruby classes and instances
Explanation about Ruby String object
[Ruby] Questions and verification about the number of method arguments
About the difference between "(double quotation)" and "single quotation" in Ruby
[Note] About the Fizz_Buzz problem (How Ruby on Rails works)
About the language to be learned