Ruby : How to write a gem? [closed] - ruby

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
We don’t allow questions seeking recommendations for books, tools, software libraries, and more. You can edit the question so it can be answered with facts and citations.
Closed 7 years ago.
Improve this question
I'd like to write a package for Ruby and make it available as a gem.
What are the tools, steps and pitfalls ?
Are there any good tutorials, screencasts, etc., which helped you learning how to do it ?

Rubygems.org's Guides is one of the best resources for writing your own gem.
If you're using Bundler in your app, you might want to look at Ryan Bigg's guide to Developing a RubyGem using Bundler and the Railscast on creating gems with Bundler.
If you're interested in tools to help you write gems:
Jeweler - Opinionated tool for creating and managing Rubygem projects. There's also a Gemcutter and Jeweler Railscast.
Hoe - From the guys at seattlrb.
gem-this adds a bunch of helpful rake tasks.
Some tutorials/guides:
Creating Your First Gem
Using bundler and rvm to build a rubygem - Using bundler and rvm to create a gem
Gem Packaging: Best Practices
Ruby Gem Recipe - Intro guide to creating a gem using bundler and jeweler
How to build a ruby gem and host it on gemcutter - tutorial using echoe and gemcutter
The Truth About Gemspecs - goes over gemspecs and tips for dealing with them
Packaging with RubyGems - a quickstart guide for Jeweler
gem that - James Adam - reviews tools that help build gems (hoe, newgem, echoe, gemhub, jeweler, gem this)
Using Gemcutter's Api from the Commandline
New Gem with Bundler – Sample Rakefile - Useful rakefile for deploying and publishing a gem
Let's Write a Gem
How To Build A Ruby Gem With Bundler, Test-Driven Development, Travis CI And Coveralls, Oh My!

This is how I usually create and release Gems:
Sign-up for https://github.com
Sign-up for https://rubygems.org
$ gem install ore rubygems-tasks rdoc rspec
$ mine awesome_gem
cd awesome_gem/ and edit the README.rdoc and awesome_gem.gemspec, write code in lib/awesome_gem/ and adding RSpec tests in specs/.
when you're ready to release, update the ChangeLog.rdoc file, run rake spec and rake rerdoc, open up html/index.html and double-check for any typos.
rake release
(Optional) submit a link and explanation of your new awesome gem to http://rubyflow.com
Sit back and bask in the glory of your first Gem. :)

You need not start writing a gem, just write some code, write some tests, use it however you want, and once you are happy with it, use gem this to generate the relevant Rakefile.
It helps if you stick to the approaches other gems take (have a lib directory, avoid naming files in ways that could clash with other gems, write some tests if you can, have a readme), but it's not necessary.
Once you have something you want to share, put it on github and push it to gemcutter.
Don't over think it, don't use hoe or other overkill tools, have fun, don't to anything I wouldn't do.

Related

How to release large Ruby Gem?

I´m finishing a Ruby Gem that depends on Chromium (jxBrowser). Chromium is quite large and has versions for linux, mac and windows. Releasing this gem to RubyGem is not possible, as the gem size is larger than supported by RubyGem. So, are there any recommendations on where/how to release this? I´d love to keep it in RubyGem as my other gens were released there. Should I release an installer in RubyGem and put the files in GitHub? What´s the best way?
Thanks for any hints and suggestions....
You can ask your users to install the gem from git (bundler: http://bundler.io/git.html, Install Gem from Github Branch?).
This will result in a line like
gem 'hard_drive_expander', github: 'rodrigo/hard_drive_expander'
in a Gemfile (or a bit a lengthier process for gem install - do you intend 'library' kind of usage or standalone installations). Note that depending on your scenario you could have an installer gem that depends on the "github-hosted" gem, or downloads and builds/installs it (both seem like dirty solutions to me though, its not what I expect or commonly see).
Although github does place quotas on your repositories, you will probably not hit them (https://help.github.com/articles/what-is-my-disk-quota/).
Another option is to host it yourself (http://guides.rubygems.org/run-your-own-gem-server/).
Sorry for the "linky" answer.
However, #icguida and #engineersmnky s comments to your question are very worth considering: Do you really need to include chromium?
Update
There is a gem that will hook into gem to allow for usage like this: gem specific_install https://github.com/githubsvnclone/rdoc.git. The gem is called specific_install: https://github.com/rdp/specific_install .

Why don't we commit our gemfile.lock for gems?

The Gem Development guide says that the Gemfile.lock file "should always be checked into version control." However, this is NOT true for Gems. For Applications, like your Rails apps, Sinatra apps, etc., it is true. The same does not go for Gems.
For clarification, please see Yehuda's guidance on the roles of .gemspec, Gemfile, and Gemfile.lock files: Clarifying the roles of .gemspec and Gemfile
I don't understand his reason. What if my gem depends on say Rails 3 and is not compatible with Rails 4? Don't I need to then commit my Gemfile.lock which specifies that my gem is only compatible with Rails 3?
This article is a bit dated (~6 years old) although it holds merit in the fact that the gemspec will hold the version controlling. If your gem has a dependency on a specific rails version you would add:
s.add_dependency "rails", "3.2"
When a user tries to use your gem on rails > 3.2 the dependency kicks in.
Hope that answers your question. If not, please clarify and I will update this post.

Where is the ruby documentation for gems? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
We don’t allow questions seeking recommendations for books, tools, software libraries, and more. You can edit the question so it can be answered with facts and citations.
Closed 7 years ago.
Improve this question
In python in an interactive console like ipython i can just type ? and get documentation on pretty much any library or function.
How do I do this in ruby? Whether I'm using pry or irb I can't find much useful documentation at all.
I'm trying to use Faraday but typing ri Faraday doesn't give any documentation. Typing ri String gives documentation for String, but gems like faraday don't produce any documentation whatsoever.
What am I doing wrong?
If you install the gem using gem install --ri faraday then the ri documentation will be generated for you.
This should be the default, but since Ri doc generation can take a while, a lot of people disable it by putting a line in ~/.gemrc saying gem: --no-ri --no-rdoc (providing those options as defaults for the gem command).
Even if that's the case you can override it on a case by case basis by adding that parameter when you install a gem
Check out the pry-doc extension for pry.
If you want to read that doc, You'll are able to run
gem server
That will create a webservice in you host on port 8808, that you will be able to access via browser.
If you want to see where that info is located, run gem env you'll see all paths into GEM PATHS section.
gem env
RubyGems Environment:
.......2.2.2/etc
- RUBYGEMS PLATFORMS:
.....
- GEM PATHS:
- /home/hbranciforte/.rvm/gems/ruby-2.2.2
- /home/hbranciforte/.rvm/gems/ruby-2.2.2#global
- GEM CONFIGURATION:
.....
- REMOTE SOURCES:
- https://rubygems.org/
- SHELL PATH:
.....
and if you look at your GEM PATHS you will find a folder called doc.
hbranciforte#hbranciforte-MacBookPro:gems/ruby-2.2.2 $ cd /home/hbranciforte/.rvm/gems/ruby-2.2.2
hbranciforte#hbranciforte-MacBookPro:gems/ruby-2.2.2 $ ls
bin build_info cache doc environment extensions gems specifications wrappers
Sometimes (almost I usually do) run your project with bundle --path bundle/vendor
That creates a folder bundle/vendor where it will store your gems and doesn't store doc (that makes bundler much faster than gem install)

Making an executable GEM from a script

I started developing ruby lately and I really like it, But I feel somehow lost. I developed a script that does "whatever" and that scripts requires many gems like nokogiri and colorize. I now want to deploy the script, so after reading a while I found many people saying that deploying as a gem is the best approach. So my question is simple? Is there any tool that I can use to create a gem of my script file and include all the gem dependencies(nokogiri) in the new gem?
I am using ubuntu!
Thanks alot
Building a gem consists of basically creating a simple directory structure for your script, and a special file known as a gemspec that will list all its dependencies. That gemspec can be used with rubygems to create a gem file (*.gem), which can be installed using rubygems or uploaded to rubygems.org for public consumption.
There are several tools that automate part of this process. A relatively simple one is the Bundler gem, which will both take care of dependencies during development, and make it easy for you to package your gem. This article contains enough information to get you started with gem development using bundler.
The best way to make a gem is to use the bundler program to build a skeleton:
# bundler gem (gem_name)
bundler gem geil_gem
This will create a template gemspec file and give you the basic structure needed for your gem to have a setup command, a console and be ready to build into a working gem (both found in the bin folder of your skeleton project). From here you can add a command binary or build out the gem as a library using the lib directory or start with tests by adding rspec to the gemspec and creating a test folder.

How to create a new Ruby gem?

To create a new Ruby gem, should I use Jeweler or should I use Bundler's built-in gem skeleton to create a base gem? What are the differences that matter?
Use Bundler
From the command line:
bundle gem your_new_gem
This will create a directory called your_new_gem with just a basic set of files and directory structure that are now considered best-practice. It's quick, easy, and a great place to start.
Creating a Gem isn't that difficult and I would advise to try building a gem from scratch, without any tools. After you know what's involved (creating a gemspec, building and pushing it to rubygems.org), you can use tools to speed up the process. My guess is you won't because making a gem is hardly the trouble at all.
I would go with Jeweler. The Bundler skeleton is only going to give you the basics. Jeweler has alot more options to work with and many helpful rake tasks for versioning, pushing to github, creating the gemspec, building and installing.
If you are working with Rails 3 engines, I have a Jeweler fork (definitely a work-in-progress) that will generate the app skelaton and include the engine file. You just have to run the jeweler command with --rails3-engine as an option. Here is the fork if you are interested:
https://github.com/johnmcaliley/jeweler
I would recommend using the built-in bundler command.
bundle gem your_gem_name
There are some rules that you should follow when creating a gem. Such as naming conventions and versioning rules.
I recently wrote a post on creating gems in netguru's blog. I think you'll find what you need in there.
https://netguru.co/blog/posts/creating-a-gem-a-step-by-step-tutorial
Hope this helps.
Here's an alternative that's worth looking at: ore
Bundler gives you a single template for ruby gems, whereas ore has multiple built in templates, plus the ability to create your own. It also supports Git, SVN (urgh) and Mercurial.
You can build a gem in RubyMine too. File > New Project > New Gem. It is that easy. But I want to make some notes about this approach:
For debugging, RubyMine will use the Fast Debugger gem, ruby-debug-ide. I know that most people now are using Pry with Byebug, but ruby-debug-ide is an interface which glues ruby-debug to IDEs like Eclipse (RDT), NetBeans and RubyMine.
Under Run > Edit Configurations > + > Ruby, I add a new debug configuration, according to the documentation here: https://www.jetbrains.com/help/ruby/run-debug-configuration-gem-command.html#1
Under Configuration, under 'Ruby Script', I add the path to the ruby gem file under lib: lib/my_gem.rb
Under Configuration, under 'Ruby SDK', I specify an RVM gemset I am using.
Under Bundler section, I check 'Run the script in context of bundler'. This would use bundle exec, which will read the dependencies in my Gemfile in my project's root. Now for gems, the Gemfile contains a method call "gemspec", which in turn reads the dependencies in dependencies in my_gem.gemspec. There, I have dependencies passed to the Gem::Specification.new block:
spec.add_development_dependency "bundler", "~> 1.7"
spec.add_development_dependency "rake", "~> 10.0"

Resources