Making ruby gems respond to terminal commands

江枫思渺然 提交于 2019-12-07 06:10:51

问题


I am extremely new to ruby as well as gem making. I made a simple gem that webscrapes some information depending on the input. However, to use my gem I need to go into the interpreter (irb) and require my gem and then call the method with some parameters.

Suppose the gem is called foo. Suppose the method is called print_website(x) # where x is a string.

I want to be able to do something like:

$ foo test.com

and it should automatically call the method and execute it.

Thanks in advance! Please ask me for clarification if i was unclear! :D


回答1:


Try it

$ mkdir bin
$ touch bin/foo
$ chmod a+x bin/foo

Edit bin/foo

#!/usr/bin/env ruby

require 'foo'
#Anything you want.......

Add following to Gemfile

 s.executables << 'foo'

Push gem. Now you published command line utility




回答2:


Simple, just run bundle gem mygem -b when creating your gem.



来源:https://stackoverflow.com/questions/11555268/making-ruby-gems-respond-to-terminal-commands

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!