We drip further. In the third drop (
drop one ,
drop two ) we will get acquainted with numerical variables and learn about the principles of OOP.
Numbers and Expressions
In programming, an expression is a combination of numbers, operators, and variables, which, when understood by a computer, produces a result in some form. Here are some examples:
8
2 + 3
"a" + "b" + "c"
100 - 5 * (2 - 1)
x + y
Consider and analyze simple code:
x = 5
y = x * 20
x += y
puts x
We give the variable
x
value 5, for
y
set the value
x * 20
(100). Then add
y
to
x
and print the result. Everything is perfectly clear, the only hitch may occur on the third line. It would seem adequate to write
x = x + y
, but this is a piece of Ruby sugar. We can also use
x *=
y and
x /=
, and
x += 1
will increase the variable by one.
')
Comparison Operators and Terms
Another code:
age = 22
puts "Sovsem Molodoi!" if age < 25
The computer not only performs the specified operations (otherwise it would be just a calculator), it uses logic to determine the direction of work. The result of code execution is predictable. Let's sort the second line. First of all, pay attention to
age < 25
- well-known “over / under” (as well as
==
,
>=
,
<=
,
<=>
!=
) Also work in Ruby. Further we see a condition familiar from other languages:
if
. And where is the
end
? Another Ruby sugar, in fact, the second line is completely similar to the code:
if age < 25 then
puts "Sovsem Molodoi!"
end
Conditions can be combined. To get the opposite effect, you can use the word
unless
:
age = 24
puts "You're NOT a teenager" unless age > 12 && age < 20
The code is clear. Go ahead.
Iterators
Here is what awaits us here:
4.times do puts "Ruby" end
Let's try to run the code - the result is four times the execution of
puts
. We analyze. First of all, the number 4 is taken, then the
times
method is called for it, which is applicable to all numbers in Ruby (the method? What is the method? This action - let's talk about this later). Instead of passing data to the method, we pass it the code between the
do
and
end
. The
times
method then executes this code 4 times. As always, Ruby wants us to write programs more conveniently, and therefore gives one more indulgence. Completely similar code, without
do
and
end
:
4.times { puts "Ruby" }
In Ruby (and in other PLs too), one of the mechanisms for creating loops is an
iterator - this is an object that allows you to iterate over all the elements of a set. In our case, it sets a loop, or iterates, in four repetitions. Look at other iterators applicable to Ruby numbers:
1.upto(5) { ... ... }
10.downto(5) { ... ... }
0.step(50, 5) { ... ... }
I think the purpose of each of them is clear. Again the code:
1.upto(5) { |number| puts number }
As a result, the program "will count to five" - ​​we passed the state of the iteration to the code. At the beginning of the code being cycled, the number “from 1 to 5” is sent to the variable
number
.
Floating point
Surely, you noticed that in all the programs written earlier we did not have to declare variables, to determine their type - Ruby does it for us (we continue to lick sugar). Let's try to divide:
puts 10 / 3
. The result is 3. Since the incoming numbers are integers, the result remains an integer. Ruby wanted to help, but he failed. Let's tell him:
puts 10.0 / 3.0
. The solution was easy!
Sometimes we find ourselves in such a situation that we cannot control incoming numbers, however, there is also a solution here:
x = 10
y = 3
puts x.to_f / y.to_f
For integers, there is a special method (i.e., an action)
.to_f
that converts them into floating point numbers on the fly. The reverse action (rounding to the integer part) is performed by the
.to_i
method.
A little bit about the PLO
In the last drop, I talked about the fact that we have so far managed to move away from the PLO. In fact, I was cunning - in Ruby everything with which we manipulate are objects. EVERYTHING! This distinguishes the language from C ++ and Java, where primitive types and phrases that do not return values ​​are present.
When writing Ruby code, like any other OO code, we first create models, rather than trying to repeat the whole process in the code. For example, if you need to write an application for a cookbook, you most likely want to create a list of recipes. For modeling, you will have to use some sorting methods to reproduce different types of data, synchronize the positions of the necessary data in each list, and other nonsense. OOP makes work easier by offering you to create classes and objects to simulate the necessary components. In our example, you can create a
Recipe class with string attributes
name and
author and an array of
ingredients . The purpose of the
class is to simulate a
thing in your program. A class is a prototype, a drawing of “nouns” in your project:
objects . Instances of a class or objects (interchangeable terms) then take these prototypes and launch them into action. In the example, objects can be created for each recipe in the list, which will be instances of the
Recipes class, which in turn will contain data and do things related to the recipes (for example, keep a list of ingredients, add ingredients to this list, etc. ) and keep track of the integrity of the recipes (for example, that only numbers in quantities of ingredients, that each recipe should have a name, etc.) - all these actions on objects, “verbs”, are
methods .
Let's return to the beginnings of this drop. Yes, indeed, any number is also a complete object. In the example
4.times {puts "Ruby"}
number 4 is an object, is an instance of the class
Integer
(we do not declare this in the code, Ruby does it for us), the
times
method that is registered in the class is applied to it (again we not visible, but it is implied). Moreover, even in the expression
x = 1 + x
unit is an object, and
+
is its method! So, not knowing the principles of OOP, we have, without even noticing it, became the “victims” of object-oriented programming :)
Enough - the drop turned out to be good, large, it gave us everything we needed to seriously move on to the implementation of OOP in Ruby, which will give us the opportunity to move on!
As always, I welcome your comments, feedback and comments!
With the fourth drop I missed a little, and she was buried under other articles - but she is there;)
HERE!