RubyGems have many things going for them: they are a local, central repository for all external libraries, they have explicit versioning, you get inter-gem dependency checking, default integration with the RubyForge gem server, and the ability for you to run your own gem server. For free. Neat.
Hidden in the back, in no way obvious as to its existence nor how to use it, is my favouritest feature: command line executables that are platform-neutral.
Write your executable inside a gem, and regardless what platform the gem is deployed to they will be able to execute the application.
Examples of gems with command line applications
Whilst rails and camping gems come with libraries of code and complementary test suites, cheat has nothing but its cheat app. The gem exists to deploy a command line application that is written in Ruby.
Create your own command line app in Ruby
Install the latest version of newgem (minimum v0.5)
gem install newgem
Create a new gem:
> newgem -b hello_world,print_date my_fun_apps creating: my_fun_apps creating: my_fun_apps/CHANGELOG creating: my_fun_apps/README creating: my_fun_apps/lib creating: my_fun_apps/lib/my_fun_apps creating: my_fun_apps/lib/my_fun_apps.rb creating: my_fun_apps/lib/my_fun_apps/version.rb creating: my_fun_apps/bin creating: my_fun_apps/bin/hello_world creating: my_fun_apps/bin/print_date creating: my_fun_apps/Rakefile creating: my_fun_apps/test creating: my_fun_apps/test/all_tests.rb creating: my_fun_apps/test/test_helper.rb creating: my_fun_apps/test/my_fun_apps_test.rb creating: my_fun_apps/examples creating: my_fun_apps/setup.rb NOW - update my_fun_apps/Rakefile with gem description, etc
-b app[,app2] parameter generates the scaffolding for two executables – hello_world and print_date. If you need more apps in a gem after running
newgem scaffolding, its very simple, as shown below.
puts "Hello world"
And build and install your gem:
> rake package (in C:/InstantRails/ruby_apps/my_fun_apps) rm -r .config Successfully built RubyGem Name: my_fun_apps Version: 0.0.1 File: my_fun_apps-0.0.1.gem mv my_fun_apps-0.0.1.gem pkg/my_fun_apps-0.0.1.gem > gem install pkg/my_fun_apps-0.0.1.gem Attempting local installation of 'pkg/my_fun_apps-0.0.1.gem' Successfully installed my_fun_apps, version 0.0.1 Installing RDoc documentation for my_fun_apps-0.0.1...
or simply use the install task to do both jobs (on unix machines):
> rake install
Note: If you get a build error it may be because you don’t have the ability to generate the tar file version of the gem. Change
p.need_tar = true to
p.need_tar = false near the end of the Rakefile and run the above lines again.
And presto! You have two new command line applications:
> hello_world Hello world > print_date Wed Oct 18 12:05:56 W. Europe Daylight Time 2006
Adding more executables to a gem
Two step process:
- Create a new text file in the
binfolder with the name of the application, and fill it with Ruby goodness. Perhaps investigate SimpleConsole for advanced console coding wizardry.
- In the Rakefile, add the application name to the BIN array, at the top.
And then repackage and install.
Deploying for communal usage
That’s another topic for another day.
- Closing in on The Dream: “one-click-to-deploy Rails apps” Got a simple app you want to build? Allocate...
- My RubyGems development tools and workflow The Open Source Developers Conference (osdc) is a nifty...
- newgem 1.0.0 all thanks to Cucumber The New Gem Generator (newgem) was exciting, moderately revolutionary, and...
- Unit Testing iPhone apps with Ruby: rbiphonetest Everything to love about Ruby: the concise, powerful language;...
- What is *jour and why they are killer apps for RailsCamp08 Uploaded with plasq’s Skitch! RailsConf 2008 was a few weeks...