
We will conquer Ruby together! Eighth drop
Let's re-read the fourth drop right now to recall the implementation of the PLO in Ruby. Repeat? Move on. In this drop, we trim all the resulting tails associated with object-oriented programming in Ruby.
Let's take another look at the code from the fourth drop:
A similar program:
When a new class is defined (usually using
Using
In the code, we have identified two methods (
Note that
In the fourth drop, we already talked about inheritance at the “zoo”, but we used it there
In this example, we also refused cats :) As always, the simplest class is higher in hierarchy. The only difference is the keyword
Even a newly created, empty object already "responds" to a number of methods. We list the methods with the code:,
you can find out if Reply to the message you want to send to him using the method
The block (
Some more useful information, a little important theory and a lot of code. This was the last drop of this kind, I promise;) Then we will start coding seriously, but for now you have time to go over your eyes and drop all the info in a bunch. Comments are expected!
PS: As you can see, now all the drops are in one blog. Don’t forget to subscribe to it - it’s even easier to follow the releases!
new and initialize
Let's take another look at the code from the fourth drop:
class Dog
def set_name( aName )
@myname = aName
end
def get_name
return @myname
end
def gav
return 'r-r-r-r!'
end
end
dog1 = Dog.new
dog1.set_name( 'Fido' )
puts(dog1.get_name)
puts(dog1.gav)
A similar program:
class Dog
def initialize(name, age)
@name = name
@age = age
end
def get_name
return @name
end
def get_age
return @age
end
end
d = Dog.new('Fido', 2)
puts "My name is #{d.get_name} and I'm #{d.get_age}"
When a new class is defined (usually using
class Name ... end
), an object of type is created Class
. When called Name.new
to create a new object, the instance method new
from is called Class
, which in turn activates allocate
to allocate memory for the object before the initialize
new object's method is called . The phases of the construction and initialization of the object are separate and can be rewritten. Initialization occurs through the instance method initialize
, construction through new
. initialize
- not a constructor (debatable, of course, but in different sources - a different opinion). Using
initialize
It has two distinct advantages over setting instance variables using type methods set_name
. First of all, a complex class can contain many instance variables and all of them can be declared using one line with initialize
no need to write methods for each. Also, if all variables are declared during the creation of the object (as written above immediately after it new
is called precisely initialize
), you will never have undefined variables ( nil
) left .Even easier
In the code, we have identified two methods (
get_name
and get_age
) to return two instance variables. Since this is a simple and frequently used idiom, Ruby provides a simplification: he attr_reader
will define these methods for us: class Dog
attr_reader :name, :age
def initialize(name, age)
@name = name
@age = age
end
end
d = Dog.new("Fido", 2)
puts "My name is #{d.name} and I'm #{d.age}"
Note that
attr_reader
symbols are used in (see last drop) and how the request for a value in the output has changed. attr_writer
determine the set
methods ( set_name
in the first listing, for example), and attr_accessor
combines the capabilities of a reader and a rider (see the example with the zoo in the fourth drop).initialize + inheritance
In the fourth drop, we already talked about inheritance at the “zoo”, but we used it there
attr_accessor
. What will inheritance look like if we abandon them and return to the initialize
methods? Not much more complicated: class Pet
def initialize(name, age)
@name = name
@age = age
end
def get_name
return @name
end
def get_age
return @age
end
end
class Dog < Pet
def initialize(name, age)
@name = name
@age = age
super
end
end
class Snake < Pet
def initialize(name, age, length)
@name = name
@age = age
@length = length
super(name, age)
end
def get_length
return @length
end
end
d = Dog.new('Fido', 2)
s = Snake.new('Lili', 2, 85)
puts "Dog: My name is #{d.get_name} and I'm #{d.get_age}"
puts "Snake: My name is #{s.get_name}, I'm #{s.get_age} & I'm #{s.get_length} cm"
In this example, we also refused cats :) As always, the simplest class is higher in hierarchy. The only difference is the keyword
super
. It denotes variables that descendant classes must pass to a higher class. Just super
pass all the variables, super()
not a single one. General methods go to the parent class, the descendants have only the initialization of variables and their own methods.Object Methods
Even a newly created, empty object already "responds" to a number of methods. We list the methods with the code:,
puts d.methods.sort
where d
is any object. Of all it is worth highlighting object_id
(all Ruby objects have a unique number, which will deduce the method), class
(deduce the class to which the object belongs) and the opposite instance_of?
(return true if the object belongs to the class from the parameter, for example, puts 10.instance_of?(Fixnum)
) you can find out if Reply to the message you want to send to him using the method
respond_to?
. Most often it is used in the condition:if d.respond_to?("gav")
d.gav
else
puts "Sorry, don't understand."
end
Proc in Ruby
The block (
do ... end
or {...}
) is not an object, but it can be converted into a class object Proc
using a method lambda
. Activates the block method call
from Proc
. Methods may contain procs:def method proc
puts 'Start of method'
proc.call
puts 'End of method'
end
say = lambda {puts 'Hello'}
method say
Epilogue
Some more useful information, a little important theory and a lot of code. This was the last drop of this kind, I promise;) Then we will start coding seriously, but for now you have time to go over your eyes and drop all the info in a bunch. Comments are expected!
PS: As you can see, now all the drops are in one blog. Don’t forget to subscribe to it - it’s even easier to follow the releases!