How can we remove an object (having an integer identifier)?

I would like to delete an object, I cannot. Here is an example:

irb(main):001:0> str = "hello"
"hello"
irb(main):003:0> str.object_id
2164703880
irb(main):004:0> str = nil
nil
irb(main):005:0> str.object_id
4

As you can see, I can just set the object variable to nil (and then, of course, its object identifier will be 4). And after that, the garbage collector will automatically delete the unused object with the identifier: 2164703880.

But no, I do not want this. I want to delete this object.

Thanks for any ideas, suggestions.

+3
source share
2 answers

You cannot define a local variable in Ruby. You can use remove_class_variable, remove_instance_variable and remove_const, but you cannot do this for local variables.

, , , . , , , str, . .

, , - Proc. , Proc, Ruby . proc, , , :

Proc.new{ |;str| str = "hello"; puts str.object_id }.call
  2227691880
  => nil
defined?(str)
  => nil

, Ruby - - , , . , .

+1

, , , -

>> str = "hello"
str = "hello"
=> "hello"
>> str2 = str
str2 = str
=> "hello"
>> str.object_id
str.object_id
=> 2157491040
>> str2.object_id
str2.object_id
=> 2157491040
>> str = nil
str = nil
=> nil
>> str.object_id
str.object_id
=> 4
>> str2.object_id
str2.object_id
=> 2157491040
>> 

, str2 , - str "".

0

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


All Articles