Where are gem installs coming from? - ruby

I am wondering, while I'm sifting through samples, how I locate the source library when installing gems. For example, I have
require 'oauth2'
and to install, I run
gem install oauth2
What repository is pulled from for gem installs? I want to use reflection to reverse engineer some of the calls in the samples.

By default gem pulls from Rubygems, the main gem repository, but you can install from anywhere if you give it a URL or use the --source option to specify an alternate source. Some prefer to use private gem hosting for their dependencies.
Why not use a Gemfile to manage your dependencies? It makes it a lot more clear:
source 'https://rubygems.org/'
gem 'oauth2'
Then you install:
bundle install
Then you can see where it got installed:
bundle show oauth2
From there you can look at the source. You can also look at the gem listing page for oauth2 where links to documentation and source are provided.

There are a few useful options for looking at source code for ruby gems.
As #tadman has mentioned you generally can expect the default gem source to be hosted at https://rubygems.org and using Bundler is also highly recommended.
In many cases the open source code will be hosted on https://github.com. I tend to prefer to fork the gem source and download my fork locally where I can analyze it in my editor without fear of accidentally breaking anything since your forked repo will contain the full git history/repo etc.
If you're new to ruby, I would highly recommend you use a ruby version manager, i.e. RVM or perhaps RBENV, although I tend to find RVM a bit simpler and less problematic.
My other personal favorite ruby code hacking/inspecting tool is PRY

Related

Ruby: CLI script failing when requiring gems installed through git

I am patching a script, and want to run code from a repo I manage that has patches.
The gem in question is not installed through a published gem but through a github link
When requiring any gem that is normally installed. The script works. But requiring any gem that is installed through a github link fails. Any suggestions?
If I understand the problem correctly, there are a few solutions:
Clone the gem that's only available via the github link, build the gem locally, install it. You should be able to require it
You might be able to manage the project with bundler and a Gemfile. Instructions here for the syntax. Bundler basically does what I suggested above, for you. I don't think gem can install a gem from a remote natively?
Would love to see some more clarification, and if you're using a Gemfile the relevant snippets
So the issue was I was running the script in question using ./bin/path/script
This will not work if the script includes github referenced gems, you need to prefix this with bundle exec which is not immediately obvious, given that when you use non-github referenced gems, it works fine without it.
Now running bundle exec ./bin/path/script will work for both, it's probably just better using that wherever possible.

Bundler config to either look for gems in custom path or download from custom source

How do I configure bundler so that when I run bundle install it looks for gems under /my/custom/path first and if it doesn't find there then try to fetch them from a ruby gem remote repository hosted under https://a.nice.host and downloads those into ./local/relative/path (relative to cwd for example). I would like to avoid the bundler looking at default gem installation system path or rubygems.org
The syntax for sourcing a gem from a local folder is:
gem 'some-gem-name', path: '/my/custom/path'
And the syntax for specifying a custom source is:
gem 'another-gem-name', source: 'https://a.nice.host'
And to install gems into a specific local folder, you can run:
bundle install --path ./local/relative/path
Now, that's probably all the tools you need, in truth... (And in fact, especially for that last requirement, you may instead wish to look into rvm gemsets, or using bundle install --deployment.)
But you did also ask about "looking in a local folder first, and only falling back to a remote source if it doesn't exist". That's quite an odd requirement (usually you'd only want to explicitly opt-in to fetching gems from a local path?!), but to answer this question as you've asked it...
A Gemfile is literally just ruby code! So you can define this logic using... You guessed it, ruby! For example:
if File.exists?('/my/custom/path')
gem 'some-gem-name', path: '/my/custom/path'
else
gem 'some-gem-name', source: 'https://a.nice.host'
end
If this (unusual) pattern needs to be repeated in multiple places, you could wrap it into some helper method.
For more information on the configuration options of bundler, please see the documentation.

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 .

What is a Ruby gem?

I've searched on Google, and I just found the uses of gem. As in, gem install, etc.
Are gems collections of .rb scripts?
If I build a series of scripts, for example that wraps the functionality of Google translate, is the preferred way of distributing that for usage a gem?
If not, how would I distribute this code?
According to RubyGems Wiki - RubyGems is a package manager for the Ruby programming language that provides a standard format for distributing Ruby programs and libraries (in a self-contained format called a "gem"), a tool designed to easily manage the installation of gems, and a server for distributing them.
The gem command is used to build, upload, download, and install Gem packages.
Gem Usage
RubyGems is very similar to apt-get, portage, and yum in functionality.
Installation:
gem install mygem
Uninstallation:
gem uninstall mygem
Listing installed gems:
gem list --local
Gem Package Building
The gem command may also be used to build and maintain .gemspec and .gem files.
Build .gem from a .gemspec file:
gem build mygem.gemspec
For more info, refer to RubyGems Manuals.
Here are some nice tutorials :)
http://railscasts.com/episodes/135-making-a-gem
http://railscasts.com/episodes/245-new-gem-with-bundler
A gem is a module/Library that you can install and use in every project on your server.
A plugin is a module/Library that you can use inside your project
Indeed, if you make some code what you like to share you can make a gem or plugin of it. You can publish it on for example github.com. You can check the source of the existing gems on github if you like to know how to make a gem as well.
Gem Package Building
Step : gem build your_gem_name.gemspec
simple steps follow click here

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