简体   繁体   English

Ruby on Rails-如何从控制台调用ruby类?

[英]Ruby on Rails - how to invoke a ruby class from console?

I have this class: 我有这个课:

module App
  module Tools
    module Pollers
      class Kpi
        ...

I am in the rails console and I am trying to do something like this: 我在rails控制台中,正在尝试执行以下操作:

x = App::Tools::Pollers::Kpi.new x = App :: Tools :: Pollers :: Kpi.new

The system does not give an error, but it doesn't do anything when I try to work with the new object. 系统没有给出错误,但是当我尝试使用新对象时它什么也没做。

Did I have to set up something in routes.rb to allow this kind of nesting of modules? 我是否必须在route.rb中设置一些内容以允许这种模块嵌套? Or am I just not working with the file correctly? 还是我只是不能正确使用文件? How do I output results to the screen of the console? 如何将结果输出到控制台的屏幕?

Here is what some console output looks like: 这是一些控制台输出的样子:

?> kpi_poller = App::Tools::Pollers::Kpi.new(date_1,date_2)
>> kpi_poller.do_launch
>> kpi_poller.do_launch("1");
?> ;
?> 

Thanks! 谢谢!

Try this: 尝试这个:

module App
  module Tools
    module Pollers
      class Kpi
        attr_accessor :kpii
        def initialize(val=1)
          @kpii = val*2
        end
      end
    end
  end
end

kpi_poller = App::Tools::Pollers::Kpi.new(3)
puts kpi_poller.kpii  # 6

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM