📜 ⬆️ ⬇️

Let's beat Ruby together! Drop nine

Today we have a really small drop - a bit of residual, but serious and important information about Ruby, which may be of interest to those who write in other languages.

Freezing objects



The freeze method in the Object class protects an object from changes, turning it into a constant. After the object is “frozen”, any attempt to change it will turn into a TypeError error. Method frozen? will let you know if the object is frozen:
')

a = b = 'Original String'
b.freeze
puts a.frozen? # true
puts b.frozen? # true
a = 'New String'
puts a
puts b
puts a.frozen? # false
puts b.frozen? # true


Variables a and b first refer to one object, so true in the first two cases.

Sometimes Ruby independently copies objects and freezes copies. For example, when you use a string as a hash key, Ruby freezes its copy and then uses it. Therefore, even if the string changes, it will not affect the key.

Object serialization



In Java , it is possible to serialize objects, allowing you to save them in binary form to a file and extract them as needed. Ruby calls this type of serialization marshaling (marshaling), using the Marshal built-in library. The main methods are dump and load :

 f = File.open( 'peoples.sav', 'w' ) Marshal.dump( ["bred", "bert", "kate"], f ) f.close File.open( 'peoples2.sav', 'w' ){ |friendsfile| Marshal.dump( ["anny", "agnes", "john" ], friendsfile ) } myfriends = Marshal.load(File.open('peoples.sav' )) morefriends = Marshal.load(File.open('peoples2.sav' )) puts myfriends puts morefriends 


Unlike Marshal , the YAML library allows you to save data in text format, about it some other time.

Modules and Impurities



Modules in Ruby are similar to classes in that they contain a set of methods, constants, other modules and class definitions. Modules are defined as classes, only the word module used instead of class . Unlike classes, it is impossible to create objects based on a module; a module cannot have subclasses. Instead, you add the missing functionality of a class or individual object using a module. Modules - single, no hierarchy and inheritance. (In general, the Module class has a superclass - Object , but any created module of the superclass has no).

There are two module assignments. First, they serve as centralized storage of constants and methods, for example:

 module Trig PI = 3.1416 #   def Trig.sin(x) # ... end def Trig.cos(x) # ... end end 


Secondly, the modules allow to divide the functionality between the classes, when the module is included in the class, its methods are added to the class. This method is called an admixture (mixin):

 module MyModule GOODMOOD = "happy" BADMOOD = "grumpy" def greet return "I'm #{GOODMOOD}. How are you?" end def MyModule.greet return "I'm #{BADMOOD}. How are you?" end end class MyClass include MyModule def sayHi puts( greet ) end end ob = MyClass.new ob.sayHi puts(ob.greet) 


Ruby (unlike C ++) does not allow multiple inheritance, replace it with mixin 's.

Epilogue



This, of course, is not all that I want to tell about Ruby. However, in practice, pure Ruby is not so interesting - we cannot, for the time being, go beyond the console, which limits us quite a lot. I think that you, like me, have a desire to move to a huge Rails training ground, where we can maximize the accumulated knowledge of the language and develop further in Ruby. So I’m suggesting that - let's keep the study of the Rails framework / without closing the Ruby drop / How do you like it? Can? Waiting for comments!

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


All Articles