📜 ⬆️ ⬇️

Objects and methods

Ruby— Object Oriented Language.


As in other Object-oriented languages, everything the program works with is an object.
Each object is an instance of a certain class, and the functionality of the object is determined by the class itself.
All objects that are instances of the same class can perform the same actions called methods.
In order to apply a method to an object, it suffices, after specifying the object, to put a "." (Full stop), and then specify the method

We continue!

The simplest objects:
')
true is a logical quantity meaning true; the only representative of the class TrueClass;

false - a logical value meaning false; the only representative of the class FalseClass;

numbers — representatives of the Numeric class, which contains fractional (Float) and integer (Integer) subclasses;

strings — representatives of the String class;

nil— “nothing”; The only representative of the class NilClass.


Numbers in Ruby



Numbers in Ruby are instances of the class Integer, which combines 2 subclasses: Fixnum and Bignum

For example, the number 123456 belongs to the class Fixnum
Moreover, if you write 123_456 underscore will be ignored, this is done for the convenience of writing large numbers.

The number -526 belongs to the class Fixnum, negative.

The number 123_456_789_123_456_789 belongs to the class Bignum
Large numbers, underscores, are also ignored.

The number 0xaabb is hexadecimal
0 for octal, 0x for hex, 0b for binary

The number 0b001_001 is binary

Fractional numbers are instances of the class Float and are set in decimal notation, while the “.” Symbol is used to separate the fractional part.
For example: 3.14

The following operators are used to calculate arithmetic expressions:

+ "Addition"
- “subtraction”
* Multiplication
/ "Division"
** “exponentiation”
% "remainder of the division"


For example:
puts 5-3
puts 6/8
puts 6.0 / 8
puts 8 * 7
puts ((2000/10) - (4 * 5) + (2 ** 3)) * 3


The result of the work will be as follows:



Ruby has several methods that allow you to convert objects from one class to another.

Method to_f — convert an object to an instance of the Float class
Method to_i — convert an object to an instance of the Fixnum or Bignum class

And a couple more methods:
ceil - finding the smallest integer not less than the specified
floor — the largest integer not greater than this
round — rounds to the nearest integer
abs— getting the absolute value of a number

Further, an example of work


Strings in ruby

In Ruby, the String class is responsible for strings; strings of characters are enclosed in quotes, or apostrophes.

\ n - go to next line
\ t - tab


The expression between "# {" and "}" is replaced by the result of its calculation.

For example:


The following methods exist for working with strings:
"" String "+" string "" - sticks two lines.

““ String ”* n” - repeats the string nth number times.

“String” [position] ”, or“ string ”[position] .chr” —Returns the ASCII code of the character at the specified line position (counting starts from zero)

"" String "[0..3]" - Returns a substring enclosed in the specified range (counting the ends)

"String" [0,3] "- Returns a substring starting from the specified position and having the specified length

““ String ”.capitalize” —Replaces the first character of the string (if it is a letter of the Latin alphabet) with the capital

““ String ”.chop” —Delete the last character of the string (two characters if they are "\ r \ n")

"" String ".delete ('ea')" - Removes the specified characters from the string, the range of characters can be specified

"" String ".index (" cd ")", or "" string ".index (" cd ", 3)"; - Defines the position number from which the specified substring starts; You can specify the position number from which the search starts.

““ String ”.length”, or ““ string ”.size” —Define the length of the string (in bytes)

““ String ”.ljust (8)”, ““ string ”.rjust (8)”, ““ string ”.center (8)” - Complete the string with spaces to the specified width, aligning respectively with the left edge, right edge or centered

““ String ”.reverse” —Returns a string containing characters in reverse order

““ String ”.strip” —Drinks spaces at the beginning and end of a line

"" String ".squeeze", "" string ".squeeze ('* -')" - Leaves only one repeating character in the group; It is allowed to set the list of characters to which this action applies.

"" String ".tr ('25 ',' 47 ')" - Replaces all found occurrences of characters with the specified


For example:

puts "Koro" + "vkin" # Korovkin
puts "12345678" .size # 8


To be continued.

Source: https://habr.com/ru/post/11007/


All Articles