How to check if a class exists in Ruby

How to check if a class exists in Ruby?

My code is:

puts "enter the name of the Class to see if it exists" nameofclass=gets.chomp eval (" #{nameofclass}...... Not sure what to write here") 

I was thinking about using:

 eval "#{nameofclass}ancestors. ....." 
+52
ruby class
Jul 27 '09 at 8:38
source share
12 answers

You can use Module.const_get to get the constant that the string refers to. It will return a constant (as a rule, constants refer to classes). Then you can check if the constant is a class.

I would do something in this direction:

 def class_exists?(class_name) klass = Module.const_get(class_name) return klass.is_a?(Class) rescue NameError return false end 

Also, if possible, I always avoided using eval when accepting user input; I doubt it will be used for any serious application, but you should be aware of the security risks.

+69
Jul 27 '09 at 9:33
source share

maybe you can do it with a specific?

eg:

 if defined?(MyClassName) == 'constant' && MyClassName.class == Class puts "its a class" end 

Note: a class check is required, for example:

 Hello = 1 puts defined?(Hello) == 'constant' # returns true 

To answer the original question:

 puts "enter the name of the Class to see if it exists" nameofclass=gets.chomp eval("defined?(#{nameofclass}) == 'constant' and #{nameofclass}.class == Class") 
+42
Jul 27 '09 at 8:52
source share

You can avoid the need to throw a NameError from Module.const_get if you are looking for a constant in a specific area by calling Module#const_defined?("SomeClass") .

A common area for calling this is an object, for example: Object.const_defined?("User") .

See: " Module ".

+22
May 29 '10 at 1:19
source share
 defined?(DatabaseCleaner) # => nil require 'database_cleaner' defined?(DatabaseCleaner) # => constant 
+11
Aug 26 '09 at 13:15
source share

Class names are constants. Can you use the defined? method defined? to determine if a constant has been defined.

 defined?(String) # => "constant" defined?(Undefined) # => nil 

Can you learn more about how defined? works if you're interested.

+7
Jul 27 '09 at 8:52
source share

Here's a more concise version:

 def class_exists?(class_name) eval("defined?(#{class_name}) && #{class_name}.is_a?(Class)") == true end class_name = "Blorp" class_exists?(class_name) => false class_name = "String" class_exists?(class_name) => true 
+6
Nov 14 '10 at 21:14
source share
 Kernel.const_defined?("Fixnum") # => true 
+5
May 23 '14 at 13:53
source share

Here is what I sometimes do to solve this problem. You can add the following methods to the String class as follows:

 class String def to_class Kernel.const_get self rescue NameError nil end def is_a_defined_class? true if self.to_class rescue NameError false end end 

Then:

 'String'.to_class => String 'unicorn'.to_class => nil 'puppy'.is_a_defined_class? => false 'Fixnum'.is_a_defined_class? => true 
+3
Apr 11 2018-11-11T00:
source share

I used this to see if the class was loaded at runtime:

 def class_exists?(class_name) ObjectSpace.each_object(Class) {|c| return true if c.to_s == class_name } false end 
+1
Jun 25 2018-10-18T00:
source share

In one line, I would write:

 !!Module.const_get(nameofclass) rescue false 

which will return true only if the given nameofclass belongs to a particular class.

+1
Jan 29 '13 at 11:11
source share

I assume that you will take some action if the class is not loaded.

If you want to require a file, why not just check the require output?

 require 'already/loaded' => false 
0
Dec 20 2018-11-12T00:
source share

If you want something packed, finishing_moves gem adds class_exists? .

 class_exists? :Symbol # => true class_exists? :Rails # => true in a Rails app class_exists? :NonexistentClass # => false 
0
Oct 19 '17 at 19:39 on
source share



All Articles