5

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
  • first you need to execute require('foo'), and then you can try foo test.com. if may work. Commented Jul 19, 2012 at 6:49

2 Answers 2

5

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

Sign up to request clarification or add additional context in comments.

2 Comments

Hi! I am still confused as to where I must actually call the method and how to provide it with ARGV[0] and ARGV[1]. Thanks!
That was awesome thanks! But one more thing, where would i add a dependancy? I require 'nokogiri' to run this and it says there was a load error and it couldnt load nokogiri. Where should I type the require nokogiri line? As of now it is in the file with the method. Thanks!
0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.