Where are my ruby gems? - ruby

Linux newbie question I guess..
How can I find out where gem installs the gems on my Ubuntu 10 system? I want to read gem sourcecode and perhaps change things up a bit.

Running gem environment should give you a fair bit of information about your RubyGems installation. The field you're looking for is 'INSTALLATION DIRECTORY'.
That said, if you want to modify some gems, there may be better ways to go about it. Most gems, for example, are hosted on github.com. If you make a fork there and make any improvements, you can submit them back to the maintainer in the form of pull requests. Most developers are happy to accept patches (that's why they're on github, right?), and you get a warm fuzzy for making the world a slightly better place for everybody else using that gem.

From your application directory you can run bundle show rails to reveal the path of a specific gem.
Jump to the directory like this
cd `bundle show rails`
Quickly open a gem in Sublime
subl `bundle show rails`

Related

How can I install a gem as if it was specified in a Gemfile?

I want to install a gem via gem install, but I need it to resolve with dependencies of the current project.
Basically I want the functionality that bundler gives me when I specify gem 'xyz' in a Gemfile, but I don't want to add that specific gem into the Gemfile.
I tried doing bundle exec gem install ... but it doesn't seem to work.
edit: The reason why I don't want to add it to the Gemfile is that it might be something like metric_fu, metrical, saikuro, rails_best_practices, etc. Simply gems that are kind of utility use and might only cluttler the project.
I might only want to use them temporarily, or install them, try out, if it doesn't work out the way I want do rvm gemset empty and bundle install again to clean up.
The point of Bundler is, in part, to prevent you from doing things like that (to prevent you from injecting gems from outside when your project doesn't declare them).
Looking for a way of doing that is looking for a bug in Bundler. If you did manage to find some way of skirting Bundler's enforcement mechanisms, you should probably not use it; instead, you may consider filing it as a bug with Bundler's issue tracker.
Now we come to the real questions: what can you do? and what should you do?
You should use either RVM gemsets or Bundler to isolate your application and its gem dependencies. You don't need both. I would recommend Bundler for this purpose over RVM gemsets.
You should add to your Gemfile any gems that you want to use and that integrate with your application (i.e., that either load your application or that are loaded as part of your application). This is not a requirement for any gems that refrain from integrating with your application.
You should refrain from committing a changed Gemfile or Gemfile.lock to version control until you are satisfied that your application continues to operate acceptably (tests pass, new gem does something useful, etc.).
Or you should stop using Bundler, because you want to do things it is explicitly designed to prevent you from doing (not recommended).
At the risk of sounding dumb, why not add it to the gemfile? You can always add it to its own group if you don't want to have to install it everywhere.
A slightly different approach is, if you're using version control, such as Git, to create a new branch and install the gems. If it doesn't work out, uninstall the gem (I'm not sure this will be done by bundle update on the old branch) and trash the branch. If it does, work, merge your stuff into the old the branch.
Though I do believe the other answers and comments have some very good points.

Getting newer gems

I'm really just a beginner to ruby, so hopefully this is an easy one. I've got to the point where I'm starting to look into some of the gems that the community have put together. I decided to check out something that would help my application consume rss feeds. So, headed over to rubygems (which is where i thought people go to get these kinds of things) and searched for rss. I found this one;
http://rubygems.org/gems/simple-rss
instructions were to just install the gem with
gem install simple-rss
So far, so good. When i came to actually use the gem, the documentation I received from doing the above was a bit naff, so i searched a bit further and found the git repo;
https://github.com/cardmagic/simple-rss
The documentation there (their code examples) complain about missing methods etc. and after a bit of digging I came to the conclusion that I must have downloaded an older version of the gem than the git trunk.
So, my question is, should I be using rubygems to get the latest gems, and if not, what other resources are out there to help find the latest builds of the comminities gems?
As far as finding a good gem for a task — use Ruby Toolbox, since it also shows you how actively maintained a gem is. Here's, for example, a section on feed parsing.
If you want to get the latest gem code that hasn't been released yet, you could download the code directly from github and build the gem yourself. However, it's easier to use bundler for that. It allows you to create a Gemfile for your project looking something like the following.
gem 'simple_rss', :git => "git://github.com/cardmagic/simple-rss.git"
Then run bundle command to download and build these gems from their corresponding sources.
In general, bundler is a great solution for managing gem dependencies for any ruby project. It provides ways to quickly reference any released gems, automatically builds gems directly from a git source, git refs, or paths on your filesystem, and has other convenient features.
By far the best place for all things Ruby & Ruby on Rails for the devs is the Ruby Toolbox

Packaging precompiled binaries inside of a gem

I've got a ruby web app that uses lilypond to generate sheet music based on user input. I'd like to move the hosting to heroku (I've recently used heroku on a few projects and really liked it, plus my traffic is low enough that it'd be free host it on heroku, for a while at least). However, heroku's dyno architecture doesn't allow you to ssh in and install whatever packages you want...instead, you give it a gems manifest, and it will install the gems for you.
So, if I'm going to deploy to heroku, I'm going to need to package lilypond as a gem. I've released a few pure-ruby gems, but haven't dealt with native extensions or precompiled binaries, or anything like that.
Is it possible to take some precompiled binaries and package it inside a gem? Ideally, this would include binaries for OS X (which I develop on) and debian linux (which is what's running on heroku), and would install the proper binary when the gem was installed.
it is possible, since precompiled binary gems for windows are the norm. Take a look at rake compiler, perhaps.
also https://github.com/rdp/ruby_tutorials_core/wiki/gem (https://en.wikibooks.org/wiki/Ruby_Programming/RubyGems) might help
-r
I think you've got a few options here:
You could get the Lilypond source and package it into a gem with a native C extension. There are some useful guides on how to do that at http://guides.rubygems.org/c-extensions/ and http://patshaughnessy.net/2011/10/31/dont-be-terrified-of-building-native-extensions
There's also a gem called gitara but I haven't been able to find any information about using it on Heroku. It might be worth emailing the author and asking if he knows anything about that.
You could create a Heroku buildpack that installs Lilypond as part of your deployment. I wasn't able to find any for Lilypond, but there are plenty of examples that do similar things - for example, this one installs Imagemagick (which is included by default on Heroku, so probably not necessary anymore - but hopefully the code is helpful). More documentation at https://devcenter.heroku.com/articles/buildpack-api and https://devcenter.heroku.com/articles/buildpack-binaries
Based on my reading, I think the buildpack option is the best way to go.
Hopefully this helps!
Instead of precompiling, you should be able to just list the gem in your .gems file, see the Heroku documentation. Of course, this requires your gem builds the native code correctly - this is still a task, but hopefully an easier one.

Ruby: just-in-time gem installation?

Would it be possible to override the default "require" by adding automatic download-and-install code for any missing includes (provided that the missing include is published as a ruby gem).
Which would not work in situations where Ruby is not interfaced with a shell. But still I think it would be an interesting idea.
Is there such a mechanism in existence today?
Edit:Removed portion about password check. I just checked and gem install doesn't seem to require me to type my password.
You would be able to hijack require method so as gems are installed when an attempt is made to require them, but still you won't have access to newly installed gem in current process, because gem index has to be reloaded.
I understand the intentions but I think exercise might not be worth it.
When installing a fresh gem the gem will be installed in the GEM_HOME. If that is not writable then it will try in the user's home .gem directory (on *NIX at least).
You could certainly script this. In a way Rail's rake gems:build is just this. Just not on demand.
But, I would recommend against this. You could run into build, versioning, dependency and network issues. And probably security issues as well.
PS: Francis Hwang did something related a while ago, although only as a require, not a require gems.
http://fhwang.net/2005/11/01/urirequire-I-got-yer-Web-2-0-right-here
A better option would be to use bundler and distribute the required gems with the application.
It is also quite simple to write a script to bootstrap the installation of gems if you didn't
want to distribute them with your code (using the bundle install/check commands)

What are the important Ruby commands? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
I'm not sure of all of them, but what are the commands to do things like update Ruby, download a new gem, or update an existing gem? What other important things are there?
Since it might matter, I'm running Windows.
By Ruby commands you probably mean the command line programs for Ruby. These are also called Ruby Helper programs. Here are a few:
ruby - The interpreter itself. Run Ruby scripts or statements.
gem - Ruby Package Manager. Great for automatically downloading or updating small Ruby modules like XML libraries, web servers, or even whole Ruby programs.
irb - Interactive Ruby Prompt. This is an entire Ruby shell that will let you execute any Ruby code you want. You can load libraries, test code directly, anything you can do with Ruby you can do in this shell. Believe me, there is quite a lot that you can do with it to improve your Ruby development workflow [1].
ri - Quick shell access to Ruby documentation. You can find the RDoc information on nearly any Ruby Class or method. The same kind of documentation that you would find on the online ruby-docs.
erb - Evaluates embedded Ruby in Ruby Templated documents. Embedded Ruby is just like embedding php into a document, and this is an interpreter for that kind of document. This is really more for the rails crowd. An alternative would be haml.
rdoc - Generate the standard Ruby documentation for one of your Ruby classes. Its like Javadocs. It parses the Ruby source files and generates the standard documentation from special comments.
testrb and rake. I'm not familiar enough with these. I'd love it if someone could fill these in!
Hopefully this was what you were looking for!
Useful command: Rake
In addition to the commands listed by Joseph Pecoraro, the 'rake' command is also pretty standard when working with Ruby. Rake makes it easy to automate (simple) tasks; like building a RubyGem or running your unit tests.
With rake, the only important command to remember is 'rake -T', which shows a list of rake tasks available in the current directory.
Updating a Ruby gem
To get back to your specific question:
To update a specific gem, you can do two things: simply update the gem:
gem update <gemname>
This will update the gem to the latest version.
Install a Ruby gem
If you want to update to a specific version, you must install it:
gem install <gemname> -v <gemversion>
You can leave out the -v options. Rubygems then installs the latest version.
How to help yourself
Two useful gem commands to remember are:
gem help
This shows how to get help with rubygems.
gem help commands
This shows all commands available to rubygems.
From here you can get more specific help on a command by using gem help:
gem help update
sudo gem install gemname
sudo gem update gemname
Okay. I see what you're going for but again try to go abstract because I know someone will give you a direct answer (which people should up-vote over this).
Everyone should get comfortable with man pages. But even if you are, you'll find that these commands lack decent man pages. However, those that do will point you to cmd --help and you will find some decent documentation there. I linked each of the commands above to a hopefully useful resource that will lead you to an answer if you're worried about command line switches. I see someone already posted the commands so I won't repeat those for gem. But I'd go further and say:
sudo gem update [gemname]
The default behavior will update all installed gems.
Also, as a bonus there is a neat gem called cheat. The idea is that instead of typing man cmd you will type cheat cmd and you can get a community editable man page for that command. Or better yet, it doesn't have to be a command, it can be an entire topic. Coincidentally to install cheat you would do:
sudo gem install cheat
And then:
cheat gem
That will list out a "man page" written by users like you about the gem command. The commands that you asked for are on that page. Anyone can add new pages, update existing pages, and contribute to the community. If you're interested here is a quick addition you can make to have autocompletion for the cheat command from the command line.
I know I have long winded answers ;)
Is there a similar command to update Ruby itself?
Alas, no there is not. I'm afraid that if you want to update Ruby itself you will have to either download an installer from the Ruby website, or compile it from source.
I should mention though that compiling from source is very easy and offers developers quite a bit of neat flexibility. You can add a suffix to the generated commands so that you can have standalone Ruby 1.8 and Ruby 1.9 builds both at the same time. That can be very helpful for testing.
Finally, its always a danger to update an operating systems built in commands unless it occurs through an official update. Installed applications may be expecting to a Ruby 1.8 in the standard location and crash if they meet an updated version. Any updates you make should just not overwrite one that came with an OS. (If any app crashes then its the fault of the app's developers for not specifying the absolute path to the OS version).
#John Topley: Thanks. Is there a
similar command to update Ruby itself?
Not really. You don't say which operating system you're using. I use Mac OS X and tend to build Ruby from source.

Resources