Best practice to require external code dependency ?

Discussion in 'Ruby' started by Bernard Lambeau, Jan 14, 2011.

  1. [Note: parts of this message were removed to make it a legal post.]

    Hi Rubyist,

    I'm trying to summarize what I've read about requiring external code
    dependencies/gems without making wrong assumptions about the environment:

    * https://gist.github.com/54177
    * http://weblog.rubyonrails.org/2009/9/1/gem-packaging-best-practices

    I've end up with the following method:

    https://github.com/blambeau/noe/blob/master/lib/noe/loader.rb

    Do you see "enhancement of" / "agreement about" such a pattern? Any reading
    I should further read ??

    Thanks
    B
     
    Bernard Lambeau, Jan 14, 2011
    #1
    1. Advertising

  2. Bernard Lambeau

    Luis Lavena Guest

    On Jan 14, 6:46 am, Bernard Lambeau <> 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)

    --
    Luis Lavena
     
    Luis Lavena, Jan 14, 2011
    #2
    1. Advertising

  3. [Note: parts of this message were removed to make it a legal post.]

    Thanks Luis,

    * If I write a pure library, your answer makes sense as the dependencies
    will be managed as you say.
    * The same is true when the code is invoked by rake, ok.
    * What if I have a bin/foo executable? Should I make something special there
    if I have version requirements on external libs ??

    Thanks again!
    B

    On Fri, Jan 14, 2011 at 2:22 PM, Luis Lavena <> wrote:

    > On Jan 14, 6:46 am, Bernard Lambeau <> 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)
    >
    > --
    > Luis Lavena
    >
    >
     
    Bernard Lambeau, Jan 14, 2011
    #3
  4. Bernard Lambeau

    Luis Lavena Guest

    On Jan 14, 11:02 am, Bernard Lambeau <> 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.

    --
    Luis Lavena
     
    Luis Lavena, Jan 14, 2011
    #4
  5. [Note: parts of this message were removed to make it a legal post.]

    I see (I was currently re-reading https://gist.github.com/54177, 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

    On Fri, Jan 14, 2011 at 3:10 PM, Luis Lavena <> wrote:

    > On Jan 14, 11:02 am, Bernard Lambeau <> 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.
    >
    > --
    > Luis Lavena
    >
    >
     
    Bernard Lambeau, Jan 14, 2011
    #5
  6. Bernard Lambeau

    Luis Lavena Guest

    On Jan 14, 11:26 am, Bernard Lambeau <> wrote:
    > [Note:  parts of this message were removed to make it a legal post.]
    >
    > I see (I was currently re-readinghttps://gist.github.com/54177, 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?


    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.

    --
    Luis Lavena
     
    Luis Lavena, Jan 14, 2011
    #6
  7. [Note: parts of this message were removed to make it a legal post.]

    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:

    https://gist.github.com/779830

    B

    On Fri, Jan 14, 2011 at 5:15 PM, Luis Lavena <> wrote:

    > On Jan 14, 11:26 am, Bernard Lambeau <> wrote:
    > > [Note: parts of this message were removed to make it a legal post.]
    > >
    > > I see (I was currently re-readinghttps://gist.github.com/54177, 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?

    >
    > 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.
    >
    > --
    > Luis Lavena
    >
    >
     
    Bernard Lambeau, Jan 14, 2011
    #7
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. david wolf
    Replies:
    0
    Views:
    479
    david wolf
    Mar 31, 2006
  2. PeterKellner
    Replies:
    3
    Views:
    383
    clintonG
    Jun 8, 2006
  3. xie bo
    Replies:
    1
    Views:
    374
    Jim Langston
    Jul 7, 2006
  4. Marc Aymerich
    Replies:
    0
    Views:
    190
    Marc Aymerich
    Mar 22, 2011
  5. oldyork90
    Replies:
    1
    Views:
    175
    Jeremy J Starcher
    Sep 10, 2008
Loading...

Share This Page