We will conquer Ruby together! Third drop
Dripping on. In the third drop ( drop one , drop two ) we will get acquainted with numerical variables and learn about the principles of OOP.
In programming, an expression is a combination of numbers, operators, and variables that, when understood by a computer, produces a result in some form. Here are some examples: Consider and analyze a simple code: We give the variable a value of 5, for we set the value to (100). Then we add to and display the result. Everything is completely clear, the only hitch can occur on the third line. It would seem more adequate to write , but this is a piece of Ruby sugar. We can also use y and , and increases the variable by one.
Another code: 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 analyze the second line. First of all, pay attention to - well-known to all the "up / down" (as well as , , , , ) are working in Ruby. Next we see the familiar from other languages condition . And where, then ? Another Ruby sugar, in fact the second line is completely similar to the code: Conditions can be combined. To get the opposite effect, you can use the word : The code is clear. Move on.
Here's what awaits us here:
Let's try to run the code - the result will be fourfold execution
In Ruby (and in other PLs too), one of the mechanisms for creating loops is an iterator- This is a certain 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. Let's see the other iterators applicable to the numbers in Ruby: I think the purpose of each of them is clear. Code again: As a result, the program “counts to five” - we passed the iteration state to the code. At the beginning of the looped code, the number "1 to 5" is sent to the variable .
Surely you noticed that in all the programs written earlier we did not have to declare variables, determine their type - Ruby does this for us (we continue to lick sugar). Let's try to divide:
Sometimes we find ourselves in such a situation that we can’t control the incoming numbers, however there is a solution: For integers there is a special method (i.e. an action) that converts them to floating-point numbers on the fly. The opposite action (rounding to the integer part) is performed by the method .
In the last drop, I talked about the fact that we still manage to get away from the PLO. In fact, I was cunning - in Ruby, everything we manipulate with is objects. ALL! This distinguishes the language from C ++ and Java, where there are primitive types and phrases that do not return values.
When writing code in Ruby, like any other OO code, we primarily create models, rather than trying to repeat the entire process in the code. For example, if you need to write an app 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 inviting you to create classes and objects to model the necessary components. In our example, you can create a Recipes class with lowercase attributes name and author and an attribute in the form of an array of ingredients . Class assignment- simulate any thing in your program. A class is a prototype, a drawing of "nouns" in your project: objects . Class instances or objects (interchangeable terms) then take these prototypes and put 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 recipes (for example, keep a list of ingredients, add ingredients to this list, etc. ) and monitor the integrity of recipes (for example, that there are only numbers in the quantities of ingredients, that each recipe should have a name, etc.) - all these actions on objects, “verbs”, are methods .
Let's get back to the beginnings of this drop. Yes, indeed, any number is also a full-fledged object. In the example, the
Enough - the drop was good, large, it gave us everything we needed to seriously switch to implementing OOP in Ruby, which will give us the opportunity to move on!
As always, I look forward to your comments, feedback and comments!
I missed a little with the fourth drop, and it was buried under other articles - but it is;) HERE!
Numbers and Expressions
In programming, an expression is a combination of numbers, operators, and variables that, when understood by a computer, produces a result in some form. Here are some examples: Consider and analyze a simple code: We give the variable a value of 5, for we set the value to (100). Then we add to and display the result. Everything is completely clear, the only hitch can occur on the third line. It would seem more adequate to write , but this is a piece of Ruby sugar. We can also use y and , and increases the variable by one.
8
2 + 3
"a" + "b" + "c"
100 - 5 * (2 - 1)
x + yx = 5
y = x * 20
x += y
puts xxyx * 20yxx = x + yx *=x /=x += 1Comparison Operators and Conditions
Another code: 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 analyze the second line. First of all, pay attention to - well-known to all the "up / down" (as well as , , , , ) are working in Ruby. Next we see the familiar from other languages condition . And where, then ? Another Ruby sugar, in fact the second line is completely similar to the code: Conditions can be combined. To get the opposite effect, you can use the word : The code is clear. Move on.
age = 22
puts "Sovsem Molodoi!" if age < 25age < 25==>=<=<=>!=ifthenendif age < 25 then 
puts "Sovsem Molodoi!"
endunlessage = 24
puts "You're NOT a teenager" unless age > 12 && age < 20Iterators
Here's what awaits us here:
4.times do puts "Ruby" endLet's try to run the code - the result will be fourfold execution
puts. We analyze. First, the number 4 is taken, then the method timesthat is applicable to all numbers in Ruby is called for it (method? What is the method? This action - we'll talk about this later). Instead of passing data to the method, we pass it the code between doand end. The method timesthen executes this same code 4 times. As always, Ruby wants us to be more comfortable writing programs, and therefore gives another relief. A completely similar code, without doand end: 4.times { puts "Ruby" }In Ruby (and in other PLs too), one of the mechanisms for creating loops is an iterator- This is a certain 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. Let's see the other iterators applicable to the numbers in Ruby: I think the purpose of each of them is clear. Code again: As a result, the program “counts to five” - we passed the iteration state to the code. At the beginning of the looped code, the number "1 to 5" is sent to the variable .
1.upto(5) { ...код цикла... }
10.downto(5) { ...код цикла... }
0.step(50, 5) { ...код цикла... }1.upto(5) { |number| puts number }numberFloating point
Surely you noticed that in all the programs written earlier we did not have to declare variables, determine their type - Ruby does this for us (we continue to lick sugar). Let's try to divide:
puts 10 / 3. The result is 3. Since the input numbers are integers, the result remains intact. Ruby wanted to help, but he did not succeed. Let's prompt him puts 10.0 / 3.0. The solution was simple! Sometimes we find ourselves in such a situation that we can’t control the incoming numbers, however there is a solution: For integers there is a special method (i.e. an action) that converts them to floating-point numbers on the fly. The opposite action (rounding to the integer part) is performed by the method .
x = 10
y = 3
puts x.to_f / y.to_f.to_f.to_iA bit about OOP
In the last drop, I talked about the fact that we still manage to get away from the PLO. In fact, I was cunning - in Ruby, everything we manipulate with is objects. ALL! This distinguishes the language from C ++ and Java, where there are primitive types and phrases that do not return values.
When writing code in Ruby, like any other OO code, we primarily create models, rather than trying to repeat the entire process in the code. For example, if you need to write an app 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 inviting you to create classes and objects to model the necessary components. In our example, you can create a Recipes class with lowercase attributes name and author and an attribute in the form of an array of ingredients . Class assignment- simulate any thing in your program. A class is a prototype, a drawing of "nouns" in your project: objects . Class instances or objects (interchangeable terms) then take these prototypes and put 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 recipes (for example, keep a list of ingredients, add ingredients to this list, etc. ) and monitor the integrity of recipes (for example, that there are only numbers in the quantities of ingredients, that each recipe should have a name, etc.) - all these actions on objects, “verbs”, are methods .
Let's get back to the beginnings of this drop. Yes, indeed, any number is also a full-fledged object. In the example, the
4.times {puts "Ruby"}number 4 - an object, is an instance of the class Integer(we do not declare this in the code, Ruby does it for us), the method timesthat is written in the class is applied to it (again we do not see this, but this is implied). Moreover, even in the expression, the x = 1 + xunit is an object, but +its method! So, without knowing the principles of OOP, we, without noticing it, became the “victims” of object-oriented programming :) Enough - the drop was good, large, it gave us everything we needed to seriously switch to implementing OOP in Ruby, which will give us the opportunity to move on!
As always, I look forward to your comments, feedback and comments!
I missed a little with the fourth drop, and it was buried under other articles - but it is;) HERE!