dubstep
January 14, 2011, 10:46am
1
Hi Rubyist,
I’m trying to summarize what I’ve read about requiring external code
dependencies/gems without making wrong assumptions about the
environment:
I’ve end up with the following method:
Do you see “enhancement of” / “agreement about” such a pattern? Any
reading
I should further read ??
Thanks
B
On Jan 14, 6:46am, Bernard L. [email protected] wrote:
Do you see “enhancement of” / “agreement about” such a pattern? Any reading
I should further read ??
Use plain require.
If you’re working from Rake, which is already a gem, it will load
rubygems so no need for you to do the require.
Also, having gem ‘abc’, ‘>= 0’ is the same as doing the require of
that gem directly (it will always load the latest version), having
‘gem’ in your code makes it already dependent on RubyGems, which
breaks the statements in your first link.
I normally don’t define any gem method in my libraries since my
dependencies will be loaded with the require (in case I’m creating a
gem that depends on other, RubyGems will active these dependencies
before I do require)
On Jan 14, 11:02am, Bernard L. [email protected] wrote:
What if I have a bin/foo executable? Should I make something special there
if I have version requirements on external libs ??
Actually no.
RubyGems will wrap your bin/foo script with a loader that looks like
this:
require ‘rubygems’
version = “>= 0”
if ARGV.first =~ /^(.*) $/ and Gem::Version.correct? $1 then
version = $1
ARGV.shift
end
gem ‘mygem’, version
load Gem.bin_path(‘mygem’, ‘foo’, version)
So your foo script don’t need to require rubygems.
And when developing, if you run through rspec, it will already load
rubygems for you.
If you want to test from the command line, then you can:
ruby -rrubygems -Ilib bin/foo
Or simple put RUBYOPT=-rrubygems
and have ruby -Ilib bin/foo
So, under your environment will have rubygems and nowhere in your
code.
On Jan 14, 11:26am, Bernard L. [email protected] wrote:
ruby -rrubygems bin/foo
How are version requirements met in that scenario? I assume they are not and
that rubygems will load last versions of all gems?
They are not. Up to you deal with the dependencies.
Should I configure rvm’s gemsets on my devel computer ? or do you use
Bundler to manage that case ??
Don’t add bundler code to your library either, as bundler depends on
RubyGems, you can:
ruby -rrubygems -rbundler/setup bin/foo
Above line will work on 1.9, but not on Ruby 1.8.x
Either you need a wrapper script to do this:
ruby -Ilib -rrubygems -e “require ‘bundler/setup’; load ‘bin/foo’”
I can’t comment more on Bundler as I don’t use it, sorry.
I see (I was currently re-reading 54177’s gists · GitHub , which
is
clear in fact – my apologies).
I understand that users of my library will have a good foo executable
that
works fine
Now, as a developer I would like to be able to run (no rake, no rspec,
and
so on):
ruby -rrubygems bin/foo
How are version requirements met in that scenario? I assume they are not
and
that rubygems will load last versions of all gems?
Should I configure rvm’s gemsets on my devel computer ? or do you use
Bundler to manage that case ??
Thanks again for clarification!
B
Thanks a lot Luis, it makes things a lot clearer for me!!
For the sake of history, the initial code I was refering to in my first
message is now here:
not_so_flexible_require
#
# This method allows requiring dependencies with some flexibility.
#
# Implemented algorithm makes greedy choices about the environment:
# 1. It first attempts a simple <code>Kernel.require(name)</code> before
# anything else (even bypassing version requirement)
# 2. If step 1 fails with a LoadError then it falls back requiring the
# gem with specified version (defaults to >= 0) and retries step 1.
# 3. If step 2 fails with a NameError, 'rubygems' are required and step
# 2 is retried.
This file has been truncated. show original
B