-1

What is the language construct that corresponds to python del statement in Ruby. By what other means I can explicitly release memory in Ruby.

jb.
  • 23,300
  • 18
  • 98
  • 136
user3477465
  • 183
  • 2
  • 2
  • 6
  • @delnan Thanks for telling those. I was not aware of such many things `del` can do. – Arup Rakshit Jul 15 '14 at 06:12
  • 1
    Also please don't compare Ruby with Python and vice versa. They're not even the same remotely. – James Mills Jul 15 '14 at 06:20
  • 1
    @JamesMills Even if they were radically different (which they don't seem to be, in many areas, at least to me) what's wrong with asking for the moral equivalent of language feature X in language Y? It's not like the question opened "So we know Ruby is just Python but worse. There is no way to ..." –  Jul 15 '14 at 06:27
  • @delnan Because you tagged it with both Ruby and Python. Some Pythonistas won't' necessarily know or be familiar with the semantics of Ruby and vice versa. – James Mills Jul 15 '14 at 08:28

2 Answers2

2

Is there any corresponding method of del in Python for Ruby?

del does many different things in . In , it is generally preferred that a method do only one thing, so there are many methods that provide different aspects of what del does in Python.

Unfortunately, it is rather hard to tell, what exactly del does in Python. Here's a pretty telling quote from the Language Reference:

Rather than spelling it out in full details, here are some hints.

  • For deleting an entry from an Array, use Array#delete_at
  • For deleting a slice from an Array, use Array#slice!
  • For removing an entry from a Hash, use Hash#delete
  • For removing an instance variable, use Object#remove_instance_variable
  • For removing a class variable, use Module#remove_class_variable
  • For removing a constant, use Module#remove_const
  • For removing a local variable, there is no method in Ruby. Local variables go out of scope at the end of a block, method, class, module or script body, and those should be small anyway, so in general, you shouldn't need to explicitly remove local variables. If you need to limit the scope of a local variable, you can always wrap it in a block:

    # some code here
    -> {
      some_local_var = 'blah'
      # code using some_local_var
    }.()
    # more code here, some_local_var is no longer in scope
    
  • There's also no method for removing a global variable. Global variables should only be used in quick, short, throwaway scripts, where it doesn't matter.

Or, how should I release the memory space in Ruby?

Ruby has automatic memory management (just like Python, BTW). It will release the memory for you.

Jörg W Mittag
  • 363,080
  • 75
  • 446
  • 653
1

At the Python's Language Reference, there's this part of what del does:

Deletion of a name removes the binding of that name from the local or global namespace.

In Ruby, there’s no way to unset a variable once set. You can set a variable to nil, which allows it to be GC, but the variable will remain in the symbol table as long as it is in scope.

Tiago Farias
  • 3,397
  • 1
  • 27
  • 30
  • You can remove [constant variables](http://ruby-doc.org/core-2.1.2/Module.html#method-i-remove_const), [instance variables](http://ruby-doc.org/core-2.1.2/Object.html#method-i-remove_instance_variable) and [class variables](http://ruby-doc.org/core-2.1.2/Module.html#method-i-remove_class_variable), but not local variables or global variables. – Jörg W Mittag Jul 15 '14 at 12:47