Undefine variable in Ruby
There are remove_class_variable, remove_instance_variable and remove_const methods but there is currently no equivalent for local variables.
Is it possible to undefine a variable in ruby?
I found this answer
Object.send(:remove_const, "TESTING")
If I remember correctly, using send
here is kind of a hack, because remove_const is really a private method. You want to ask yourself why you need to define constants PRODUCTION, DEVELOPMENT and TESTING at the same time while they really are mutually exclusive ; that's why I upvoted grepsedawk's answer.
How to uninitialize/undefine a variable in Ruby so I don't get warnings in console?
In ruby whenever you use a capital letter first letter you are creating a constant. You will be warned when it is redefined.
For example
class
THIS_IS_A_CONSTANT = 42
ThisIsAlsoAConstant = "The answer!"
@@this_is_a_class_variable
def a_method
@this_is_a_member_variable = true
this_is_a_local_variable = true
end
end
If you create a constant inside a method, you will get re-definition error the second time you call the method.
Is undefined variable equal to nil in Ruby?
Undefined variable is not equal to nil.
Undefined instance variable returns nil (again, if it is undefined).
y
raises an exception
@y
returns nil
Destroy a variable in ruby
As of now (MRI 2.2 and before), there's no way to do this.
erb idiom to handle undefined variable
I would do this:
<% if defined?(environment) %>
<% Array(environment).each do |f| %>
one line: <%= f %>
<% end %>
<% end %>
I didn't understand why you joining on new lines and then splitting on them again, so I removed it from the example.
What is the undef object of Ruby?
This is an educated guess on my part, maybe Matz will see this question at some point and give us a definitive answer, hopefully this will do in the meantime.
As you might know, ruby was somewhat influenced by perl at least early on (which is why we have variables like $@ and $_ etc.). Perl has an undef
keyword/function (e.g. if you declare a variable without initialising - its value is undefined). I would say, that at some time in the past Ruby was also meant to have something similar (i.e. variables would be able to have an undefined value). How do we know this? By the context in which it is found.
As you can see, that comment describes how the object_id
of the various Ruby objects is derived. Some details on that can be found here. But, in essence we know the following:
false.object_id == 0
true.object_id == 2
nil.object_id == 4
This is what the comment suggests and this is indeed the case, you can crack open an irb
session and try it out for yourself. It looks like undef
was meant to have an object_id
of 6.
Now, undef
is indeed a reserved word in Ruby, but it is not a special object like nil
, false
and true
, it is - as we know - a keyword used to undefine a method.
So, to answer your question, there is no undef
object, it has no class and you can't access it. The purpose that undef
was meant to serve is instead being served by the nil
object in the Ruby that we know today. But, it has remained in the code as a legacy of times gone by, for the more curious of us to find and puzzle over.
How to undefine namespaced class in Ruby?
Constants are defined in their respective parent module, with top-level constants being defined on the Object
class.
Thus, ActiveRecord::Base
is a constant(Base
) which is defined on the ActiveRecord
module. Now, in order to remove this constant, you have to call the remove_const
method on the ActiveRecord
module:
ActiveRecord.send(:remove_const, :Base)
Alternatively, you could also traverse the path directly from Object
, i.e.
Object.const_get(:ActiveRecord).send(:remove_const, :Base)
Related Topics
Flattening Nested Hash to a Single Hash with Ruby/Rails
Ruby 'Encode': "\Xc3" from Ascii-8Bit to Utf-8 (Encoding::Undefinedconversionerror)
How to Download via Http Only Piece of Big File with Ruby
Peer-To-Peer File Sharing with Web Sockets
Passing Multiple Error Classes to Ruby's Rescue Clause in a Dry Fashion
When to Use Memoization in Ruby on Rails
How to Url Encode a String in Ruby
How to Require Active Record Working Outside of Rails
How to Automate Chrome Request Blocking Using Selenium-Webdriver for Ruby
How to Convert a Ruby Hash to Xml
Select Arrays Between Date Ranges with Ruby
Is There Goto Statement in Ruby
How to Test a File Upload in Rails