【问题标题】:make a ruby code as a console command制作一个 ruby​​ 代码作为控制台命令
【发布时间】:2012-09-25 19:01:11
【问题描述】:

我用珠宝商创建了一个 gem,我想要一个命令 hat 来调用我的 gem 中的特定函数。 在Python中我可以把这个

entry_points="""
      [console_scripts]
      hat = hat:hat
      """ 

setup.py 中它可以工作,但它是如何在 ruby​​ 中完成的?

【问题讨论】:

    标签: ruby console


    【解决方案1】:
    $ mkdir bin
    $ touch bin/foo
    $ chmod a+x bin/foo
    

    编辑 bin/foo

    #!/usr/bin/env ruby
    
    require 'foo'
    #Anything you want.......
    

    将以下内容添加到 Gemfile

     s.executables << 'foo'
    

    根据:

    Making ruby gems respond to terminal commands

    还有:

    http://visionmedia.github.com/commander/

    【讨论】:

      【解决方案2】:

      对于控制台,您可以像在 Python 中一样将内容添加到您的 .irbrc 文件中。

      不过,在irb 控制台的上下文中,方法必须在main 命名空间中定义。显然,您要小心在此处添加的内容,因为如果您开始大量添加任意名称的内容,可能会引起混乱。

      【讨论】:

      • 我不希望它在 irb 中,我希望它在我的终端中,就像在“github.com/defunkt/hub”中一样
      • 哦,那种控制台。 Sunny J. 有答案。在这种情况下,您的意思是“shell命令”。 “控制台”通常指的是rails console
      猜你喜欢
      • 1970-01-01
      • 2016-01-29
      • 2010-11-18
      • 2011-05-28
      • 2016-09-25
      • 2015-10-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多