Rails:无法从初始化程序创建类的对象

时间:2017-12-28 16:24:25

标签: ruby-on-rails ruby object initialization instance

这是我的初始化程序:

puts 'running ...'
ObjectClass.new
puts 'finished'

在我的终端:

$ rails c
running ...
finished
Loading development environment (Rails 5.1.4)
irb(main):001:0> ObjectSpace.each_object(ObjectClass).select { |o| o }
=> []
irb(main):002:0> ObjectClass.new
=> #<ObjectClass:0x94c0650>
irb(main):003:0> ObjectSpace.each_object(ObjectClass).select { |o| o }
=> [#<ObjectClass:0x94c0650>]

如何在初始化文件中创建ObjectClass的实例。

1 个答案:

答案 0 :(得分:1)

ObjectClass.new放入@var@@var$var

初​​始化:

class ObjectClass; end
$var = ObjectClass.new