Understanding Ruby's basic management utilities? - ruby

I'm new to Ruby and RVM, but I've managed to get both set up on my system by following a simple tutorial. In the tutorial, commands like
gem install rails
which gem
bundle install
are used, without much explanation of the Ruby management ecosystem. My confusion is where all these utility programs come from?
I only installed ruby 1.9.3 and rvm, but somehow 'gem', 'which', and 'bundle' are executable programs in my path. How exactly did they get there and where can I read about the arguments they take and similar utilities I may not have discovered yet. There are no man-pages for them.
Finally, I believe rvm is analogous to virtualenv in Python and gem is analogous to pip?
Thanks for the help getting started.

In Ruby 1.9 the Rubygems package manager is included with standard Ruby, so that's where gem comes from. More info with gem -h or and http://guides.rubygems.org/.
which is a standard utility in unixoid systems and has nothing to do with Ruby (man 1 which for details).
bundle comes from Bundler, which is a commonly used gem for managing an application's dependencies. You generally have to install this manually, unless RVM does that nowadays. bundle -h has more info for you.

Related

Ruby, Versions, Gems, Versions and more, Oh My

Apologies for the topic title, couldn't resist.
Basically, For no reason whatsoever apart from trying out different things. I want to implode RVM and try out rbenv. Nothing against RVM, just want to try different peoples software and try different things out.
I can get rid of RVM and install rbenv no problem. Could you guys confirm a few things for me.
Firstly, if I do the above, and then install a few different ruby versions. I know I can place a .ruby-version file in a projects root directory to specify a specific ruby version but then when I say gem install <gemname> I take it that is installed globally for that ruby version.
If I then use bundler per project to manage what gems are used and I specify a gem version, what happens if the version specified in the lock file is different to the currently installed version and what happens if it's vice versa?
I just need a bit of clarification on what's doing what in regards to ruby, versions, gems and their versions.
One of the (many) lovely things about rbenv is that you can so easily look right at your ruby installation to see what's going on. For example, on my machine I can open ~/.rbenv and there's versions containing my ruby installations, such as 1.9.3-p327. Opening that, I can keep drilling down to see my gems installed for that version of ruby (in ~/.rbenv/versions/1.9.3-p327/lib/ruby/gems/1.9.1/gems). It is then trivial to run gem list or gem install somegem and confirm that what's being affected is this very collection of gems - if this is the current ruby (set in rbenv global and starred in rbenv versions).
In short, everything about your system thinks that ruby means this version of ruby and that its library is this library.
For Bundler, you'll need to install rbenv-bundler. A nice tutorial (easily found with Google) is here: http://dan.carley.co/blog/2012/02/07/rbenv-and-bundler/

What are the advantages of RVM on linux?

Please understand that I'm not trying to bash RVM. I would just like to understand why it's so popular in the Ruby community.
There appears to be two main reasons to use RVM - to manage/switch between different versions of Ruby, and to manage a specific gemset per project. However there seem to be much cleaner ways of achieving the same thing on linux:
I can install different ruby versions
from the repositories through the
regular package manager, or build
from source and install as a package.
I can switch between versions using
update-alternatives (on Debian, Ubuntu, Fedora at least).
I can use Bundler to manage sets of
gems for different projects, as
outlined in this blog post.
Given the above, I'm confused as to why I would want to use RVM to manage Ruby versions and gems. The idea of using a bash script to install software on my system outside the package manager feels very hackish, a feeling backed up by this blog post.
Not being a Mac user I am totally guessing here, but I thought it might be that OS X is very popular in the Ruby community. If there is no system like update-alternatives for switching ruby versions on OS X, RVM might be the best solution and this idea has carried over to linux too.. like I said, this is just a guess.
So why is RVM so popular, and why should I prefer it over the regular package manager + Bundler on Debian or Ubuntu?
If you need to manage many project with diferent versions of Ruby and special Rails like me (I have 3 project to manage wich uses Rails 2.3.5, Rails 3.0.5 and Rails 3.1 rc1) do RVM is nessesary solution. As you may know Rails 2.3.x has no bundler support and uses old gems so version managing is the way to make developing easier. I hope I helped you alittle.
RVM is portable
update-alternatives works globally, RVM works in shell/script context - I suppose it is modyfying env paths. Now imagine having two apps on one server: one 1.8.7 and another 1.9.2
Usage of Ruby EE requires manual installation - with RVM it is matter of one command to install it.
I had too much problems with mixed gem dirs with 1.8.7 only or 1.9.2 only gems in Debian.
Easy backup - just copy .rvm dir.
Consistent clustering (same platform) - just share one .rvm dir.

Using RVM, how to ensure gems and non-gems see the same version of ruby?

I'm trying to install bdb on Ubuntu as a gem as follows,
gem install bdb
I'm not using sudo, since I am installing with rvm. The ruby version on my system is 1.9.1 but the version I'm using for this installation is 1.8.7 (I'm installing rbot, you see). The problem I've encountered is:
checking for db_version() in -ldb-4.something... no
I assume this means that rubygems is not finding ldb4.something in the places it is looking. Is this because I installed libdb-4.something (7, actually) using sudo, and my system's ruby version is different from the ruby version I'm using for bdb? Using rvm, how should I manage this sort of situation? Maybe I'm doing things wrong, but it seems to me that my problem is that not everything I'm using is a gem, and the stuff that isn't gems is seeing the wrong version of ruby.
(if it turns out that my problem is something completely unrelated: I am still interested in the question. How do things I've installed on my system with a particular version of ruby get along with gems installed using rvm under different versions of ruby?)
"How do things I've installed on my system with a particular version of ruby get along with gems installed using RVM under different versions of ruby?"
They don't, and that is the point. RVM is for managing different versions of Ruby so that they are isolated from each other.
This means that the Ruby versions that you use for system (when you do sudo gem install that would be installing to system) have nothing to do with the current environment (when properly set up.)
When you are in a RVM environment, each version of Ruby installed is an isolated environment.
What I would recommend is this:
Go to the RVM Website and check it out.
At the end of the page, continue with your reading about how to use gemsets.
go to your project directory
rvm use 1.8.7#your_project_environment
create your .rvrmc file so that you will automatically be using this environment in your project on the future visits to this project, this file can contain "rvm use 1.8.7#your_project_environment" which will achieve this.
install all required gems
If you have additional questions, feel free to visit Wayne E. Seguin and a ton of other users of RVM and we will be happy to help you out. Of course, the first search for answers is the documentation on the web site.

Ruby: How to include/install .bundle?

I'm new to Ruby. I have a .bundle file. I put it in the source folder and did
require('my.bundle')
But when I call the methods in the bundle, the definition is not found. Do I have to install them or include them in some other way to access them?
I am on Ruby version 1.8.7 (latest version on Mac).
I highly recommend using RVM to manage your Ruby installation, including your gems, so if you don't already have that, get it and follow the instructions for installing it. Make sure you do the part about modifying your bash startup script or you'll see weird behavior, like the wrong Ruby being called. Also, use the steps in "RVM and RubyGems" to install your gems or you can run into weird behavior with gems being installed under the wrong or an unexpected Ruby.
Second, use the gem command to install gems:
gem install gem_to_install
replacing "gem_to_install" with the name of the gem you want, and it will be installed into the appropriate gem folder for your Ruby.
If you are on Ruby 1.92, and trying to require a gem to use as a module in your code, use:
require 'gemname'
if it is installed via the gem command. And, if it is a module you wrote or have in your program's directory or below it, use:
require_relative 'path/to/gem/gemname'
If you are on a Ruby < 1.9 you'll also need to add require 'rubygems' above your other require lines, then use require './path/to/gem/gemname'.
Thanks, but my .bundle is not in gems. How do I install/require a .bundle file I already have?
If you wrote it look into rubygems/gemcutter or bundler for info on bundling and managing gems.
You can install a gem without using the app by going into the directory containing the gem and running setup.rb. See http://i.loveruby.net/en/projects/setup/doc/usage.html for a decent writeup or the official docs at: http://docs.rubygems.org/read/chapter/3

Do you have to do duplicate gem installs for JRuby & MRI?

I have JRuby and Ruby (MRI) installed. It seems that I need to install gems twice - once for each of these platforms. Is this necessary or am I doing it wrong? After I installed the rails gem for MRI, should I have pointed JRuby to it, or was it necessary for me to also call: "jruby -S gem install rails"
You need to install gems for each different install of ruby that you have.
If you set GEM_HOME you can share your gem installations.
Some gems target specific platforms, e.g. Mongrel (there's a MRI one and a JRuby one). Also, JRuby cannot use gems that have native extensions (i.e. C code) unless they use the FFI (which most do not - yet).
Personally I have separate gem repos for MRI and JRuby. The little bit of extra hassle is worth the peace of mind when trying to track down a problem.
It's pretty easy to see what each repo has installed:
jruby -S gem list --local
vs.
gem list --local
You could even write a ruby script to sync one gem list to the other, but you'd have to be careful about platform specific gems....
I hit this problem when creating my gem, jimmy_jukebox, but made my gem work with both.
First, JRuby doesn't handle fork...exec (and even replies incorrectly to Process.respond_to?(:fork)), so I had to rescue NotImplementedError and use Spoon.spawnp instead.
I then created (in my gem's /bin directory) paired executables -- play_jukebox and jplay_jukebox; and load_jukebox and jload_jukebox -- each with the correct shebang line (/usr/bin/env ruby or /usr/bin/env jruby).
I'd love to know a better way. But I'd rather handle everything within a single gem than maintain and distribute multiple gems.

Resources