0

I have foo.rb and main.rb files which was created by another file.

foo.rb:

class Foo
  def initialize
   @val = 1
  end
end

main.rb:

file_name = gets.chomp()
require_relative(file_name)
class_name = file_name.capitalize
a = class_name.new()
p "This is val: #{a.val}"

But I get an error: undefined methodnew' for "Foo.rb":String (NoMethodError)`

My question: How can I pass class name as a value.

bjhaid
  • 9,342
  • 2
  • 35
  • 47
Kazik
  • 685
  • 7
  • 17

1 Answers1

0

You need to remove the extension...

class_name = Object.const_get(file_name.capitalize[/^[^.]*/])
Uri Agassi
  • 36,078
  • 12
  • 73
  • 92