Making ruby gems respond to terminal commands

2019-04-29 22:37发布

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

2条回答
戒情不戒烟
2楼-- · 2019-04-29 23:11

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

查看更多
爱情/是我丢掉的垃圾
3楼-- · 2019-04-29 23:18

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

查看更多
登录 后发表回答