Requiring more than one file?

Discussion in 'Ruby' started by Marc Heiler, Jun 10, 2007.

  1. Marc Heiler

    Marc Heiler Guest

    require does accept ony word, which should be the name that has
    to be required. Since duck typing is en vogue, is it a bad idea to
    have require use something like this here instead?

    require %w( pp English pathname fileutils abbrev pp digest/md5 yaml)

    The reason I write this is because I quite often see this:

    %w( pathname fileutils English abbrev pp digest/md5 yaml ).each { |file|
    require file }

    which some people like to do compared to the a little cumbersome:

    require 'pathname'
    require 'fileutils'
    require 'English'
    require 'abbrev'

    Is there a reason why require accepts only one arg, and why it doesnt
    allow multiple args/Array as arg?
     
    Marc Heiler, Jun 10, 2007
    #1
    1. Advertisements

  2. Marc Heiler

    Tim Hunter Guest

    I don't understand why this is considered cumbersome. Certainly it's
    more readable than looping over an array, and it's not like this is
    something you have to type a lot, or read a lot.
     
    Tim Hunter, Jun 10, 2007
    #2
    1. Advertisements

  3. You can always do something like this:

    $ cat foo.rb
    def foo; puts 'foo'; end
    $ cat bar.rb
    def bar; puts 'bar'; end
    $ irb
    irb(main):001:0> module Kernel
    irb(main):002:1> alias :require_orig :require
    irb(main):003:1> def require *args
    irb(main):004:2> args.each { |a| require_orig(a) }
    irb(main):005:2> end
    irb(main):006:1> end
    => nil
    irb(main):009:0> require 'foo', 'bar'
    => ["foo", "bar"]
    irb(main):010:0> foo
    foo
    => nil
    irb(main):011:0> bar
    bar
    => nil
     
    Jesse Merriman, Jun 10, 2007
    #3
  4. Marc Heiler

    Marc Heiler Guest

    "I don't understand why this is considered cumbersome. Certainly it's
    more readable than looping over an array, and it's not like this is
    something you have to type a lot, or read a lot."

    Hi there,

    It becomes cumbersome if you do like 20 lines of requires.

    Thanks to Jesse Merriman. This is one of the
    reason ruby rocks.
     
    Marc Heiler, Jun 11, 2007
    #4
  5. Marc Heiler

    Marc Heiler Guest

    Whops, that was a quick too fast. It seems to work with:

    require 'yaml','pp'

    but not with:

    require %w( yaml pp )

    TypeError: can't convert Array into String


    One reason why I do not so much like require 'something' is because of
    the
    '' :)
     
    Marc Heiler, Jun 11, 2007
    #5
  6. require *%w( yaml pp )
    should work.
     
    Aureliano Calvo, Jun 11, 2007
    #6
  7. This yields a syntax error. Instead, you might try

    %w( yaml pp ).each {|r| require r}

    That's sort of the standard %w form of require.

    Ben
     
    Ben Bleything, Jun 11, 2007
    #7
  8. You need require *%w(...) with the redefinition that Jesse gave. Note
    the asterix.
    Well, then you can do this - even without hacking the standard require:

    %w{pathname fileutils English abbrev}.each {|r| require r}

    Or, formatted differently

    %w{
    pathname
    fileutils
    English
    abbrev
    }.each {|r| require r}

    IMHO changing "require" is not such a good idea as this change will be
    overridden if you use gems for example. If you want multiple arguments,
    then I'd rather define a new method

    def r(*a) a.flatten.each {|f| require f} end

    Then you can do

    r %w{
    pathname
    fileutils
    English
    abbrev
    }

    r 'pathname',
    'fileutils',
    'English',
    'abbrev'

    You can even replace the #each with #map to get all the return values.

    Cheers

    robert
     
    Robert Klemme, Jun 11, 2007
    #8
  9. Sorry if I've missed something in an earlier post, but "require"
    doesn't even seem to like having more than one argument. However, if
    it did, you could always use the "unarray" operator to do *%w( yaml
    pp ) in order to get the effect of 'yaml', 'pp'.

    $ irb
    irb(main):001:0> require *%w[ yaml pp ]
    ArgumentError: wrong number of arguments (2 for 1)
    from (irb):1:in `require'
    from (irb):1
    irb(main):002:0> require 'yaml', 'pp'
    ArgumentError: wrong number of arguments (2 for 1)
    from (irb):2:in `require'
    from (irb):2
    irb(main):003:0> p %w( yaml pp )
    ["yaml", "pp"]
    => nil
    irb(main):004:0> p *%w( yaml pp )
    "yaml"
    "pp"
    => nil
    irb(main):005:0> p 'yaml', 'pp'
    "yaml"
    "pp"
    => nil

    -Rob

    Rob Biedenharn http://agileconsultingllc.com
     
    Rob Biedenharn, Jun 11, 2007
    #9
  10. Marc Heiler

    Eric Hodel Guest

    I can't recall the last time I had more than eight requires in a
    file. I think I can stretch back and recall five, but they're in a
    file I never touch anyhow (a top-level require everything file).

    Putting them all on one line makes diffs hard to read.

    The world isn't going to run out of carriage returns or line feeds
    any time soon.
     
    Eric Hodel, Jun 12, 2007
    #10
  11. Marc Heiler

    Marc Heiler Guest

    Hi,

    I guess I should have phrased my question differently, seeing that it
    spawned
    side-questions including an even ... rather ... odd ... notice that "the
    world isn't going to run out of carriage returns or line feeds any time
    soon ...."

    The question should have simply been:

    Why does require allow only one argument?
     
    Marc Heiler, Aug 1, 2007
    #11
  12. We had that discussion a few weeks ago and as far as I remember the
    outcome was: it's the way it is and it's not worth the effort to change it.

    Kind regards

    robert
     
    Robert Klemme, Aug 1, 2007
    #12
  13. Perhaps the use for aditional parameters in next versions.
     
    Rubén Medellín, Aug 2, 2007
    #13
    1. Advertisements

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.