1

I am trying to write a script in ruby which involves converting a string to Class Names. Since I am using pure ruby I cannot use .constantize. I have tried using Object.const_get('String') but not sure why it is throwing a uninitialized constant String (NameError)

I have require 'active_support' on the top of the file

11
  • 1
    Would you mind to try Kernel.const_get 'String'? Commented May 29, 2018 at 16:55
  • 2
    can you say any info about your ruby version / platform? Object.const_get('String') works for me. Commented May 29, 2018 at 16:57
  • @mudasobwa I tried that as well. But I got uninitialized constant Kernel::String (NameError) Commented May 29, 2018 at 16:58
  • @maxpleaner That's working for me in the rails console but when I put that in the script it's not working. I am using ruby 2.4.4 Commented May 29, 2018 at 16:59
  • 1
    Additionally since you are requiring ActiveSupport you can use the constantize method via ActiveSupport::Inflector.constantize("String") or by addition require 'active_support/core_ext/string' after require 'active_support' and then "String".constantize or even "string".classify.constantize will work for you Commented May 29, 2018 at 18:11

3 Answers 3

3

The conventional way of assigning a name to an anonymous class is as follows.

bub = Class.new do
  def b
    'hi'
  end
end

str = 'Bubba'
Object.const_set(str, bub)

Bubba.is_a?(Class)
  #=> true
Bubba.new.b
  #=> "hi"

Is that what you want to do? If so, as you see, you need to use Module#const_set.

Sign up to request clarification or add additional context in comments.

Comments

0

Do you try to use const_get only for a class or it is under a namespace like ModuleA::ModuleB::ClassName?

Also converting a string to a class name makes a new class or assigns the value to it?

I am asking these questions because the answer will affect the method you have to use. Maybe const_set instead of const_get is the correct approach, I don't know.

Comments

0

From the comments you gave it looks like 'String' is just an example and not the value that you literally pass to const_get. The actual value apparently is 'Assignment', is this correct?

When you execute Object.const_get('Assignment') and you receive the uninitialized constant error it indicates that at this point the class Assignment has not been loaded yet.

When you are using Rails then a lot of autoloading takes place if the files are in the right folder and the classes follow the naming conventions. Since you are running a "standalone" ruby script, autoloading does not take place and you will need to load the file yourself.

Adding a line like

require_relative "somepath/assignment"

should work. somepath needs to be adapted to the directory/file layout you have. It will load the file and execute the ruby code in that file. If assignment.rb defines something like

class Assignment
end

Then the const_get will work.

That being said: what is your exact use case for this? Why do you need to dynamically find classes? Also Note that this opens up your app to (an unlikely) potential security issues if you let user input define what classes are loaded.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.