Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Making ruby gems respond to terminal commands

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

like image 937
VarunMurali Avatar asked Jul 19 '12 06:07

VarunMurali


People also ask

How do I run a gem command?

Press Ctrl+Space to show the available gem executables. In this field, type the command line arguments of the script. Specify the working directory used by the running task. For example, this option is in effect when the running script loads other scripts by relative paths.

What is gem in command line?

The interface for RubyGems is a command-line tool called gem which can install and manage libraries (the gems). RubyGems integrates with Ruby run-time loader to help find and load installed gems from standardized library folders.

How do I push a gem to RubyGems?

Note that you need an account at RubyGems.org to do this. From the main menu, select Tools | Gem | Push Gem. In the Run tool window, specify your RubyGems credentials. Your gem will be published to RubyGems.org.

How do gems work in Ruby?

Gems can be used to extend or modify functionality in Ruby applications. Commonly they're used to distribute reusable functionality that is shared with other Rubyists for use in their applications and libraries. Some gems provide command line utilities to help automate tasks and speed up your work.


1 Answers

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

like image 145
Shamith c Avatar answered Sep 19 '22 21:09

Shamith c