2

I have certain classes that are implemented as decorators in my Rails app which is on Ruby 1.8.7 . I keep getting the warning - warning: already initialized constant ABC . Here ABC is the name of the constant.

To solve this, I'm using the const_defined? method as suggested in this answer .

My doubt is - is const_defined? the right way to check if a class constant is already defined in ruby ? I clearly see that it can be used to check for a module constant.

I was able to see that the defined? method can also be used as mentioned in this answer. I tried to lookup the documentation of defined?, but I don't see a link to its documentation based on my search thus far.

I'm not sure which to choose among the two as I can't see a clear difference between the two at this point.

Could one please suggest which is best to use in this situation and why?.

Community
  • 1
  • 1
boddhisattva
  • 5,836
  • 9
  • 44
  • 71

3 Answers3

3

The keyword defined? is documented here.

It is better to ask if it is a constant, and use const_defined? if it is important that it is a constant. If you only care that it is defined, then use the keyword defined?

vgoff
  • 9,856
  • 3
  • 35
  • 54
  • Thanks for your answer. I'm using Ruby 1.8.7 , can you please point me to a documentation specific to this ruby version if exists. Using the const_defined? method within a class shouldn't be a problem? I see that it works but the reason why I'm asking this question is, const_defined? more sounds like something to use with Modules than classes and since it was defined under the ['Module' related documentation](http://ruby-doc.org/core-1.8.7/Module.html#method-i-const_defined-3F), I had some doubts. Can you kindly confirm? Thanks. – boddhisattva Sep 01 '14 at 15:50
  • @boddhisattva, DO NOT use Ruby 1.8.7, it is badly outdated. I mean badly. Use Ruby 2.x, I beg of you. – Boris Stitnicky Sep 01 '14 at 15:51
  • 1
    @BorisStitnicky: Where do these people even *get* ruby 1.8? Every major distro has updated, as far as I know. Even OSX! – Sergio Tulentsev Sep 01 '14 at 15:54
  • 1
    @boddhisattva: modules and classes are basically the same thing. In fact, Class inherits from Module, so every class **is** a module. – Sergio Tulentsev Sep 01 '14 at 15:56
  • @BorisStitnicky I'm completely with you on this :) . But sometimes, when it's not really your decision that counts you have to live with it until you find alternatives.. I completely understand where you're coming from given that the community has stopped supporting this version of Ruby. – boddhisattva Sep 01 '14 at 15:56
  • @SergioTulentsev: Thanks for confirming this and letting me know more about classes in the context of modules.. In my case, Ruby 1.8.7 is already setup, so I'm just using that itself. I understand may be finding a new Ruby 1.8 source would be hard to find :) – boddhisattva Sep 01 '14 at 16:04
0

Since you're using Ruby 1.8.7 it looks your only option is const_defined?, as I can't find any documentation from defined?

If you we're using Ruby 1.9 I would suggest to use the defined? and check this document for more information.

Paulo Fidalgo
  • 19,844
  • 7
  • 85
  • 108
0

There is one more option. This is because Ruby has this pesky #const_missing method, that can unexpectedly provide values even there, where #const_defined? would indicate false:

module M
  def self.const_missing sym; 42 end
end

M.const_defined? "A" #=> false
M::A #=> 42

For cases like these, you can use begin ... rescue ... end statement:

begin
  M::A; true
rescue NameError
  false
end
#=> true
Boris Stitnicky
  • 11,743
  • 4
  • 50
  • 69