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,592
  • 2
  • 37
  • 47
Kazik
  • 705
  • 1
  • 8
  • 20

1 Answers1

0

You need to remove the extension...

class_name = Object.const_get(file_name.capitalize[/^[^.]*/])
Uri Agassi
  • 36,848
  • 14
  • 76
  • 93