Forum: Ruby Requiring more than one file?

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Robert H. (Guest)
on 2007-06-10 23:24
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?
Tim H. (Guest)
on 2007-06-11 00:17
(Received via mailing list)
Marc H. wrote:
>
> which some people like to do compared to the a little cumbersome:
>
> require 'pathname'
> require 'fileutils'
> require 'English'
> require 'abbrev'
>
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.
Jesse M. (Guest)
on 2007-06-11 00:35
(Received via mailing list)
On Sunday 10 June 2007 15:24, Marc H. wrote:
>
> 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?
>

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
Robert H. (Guest)
on 2007-06-11 21:20
"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 M.. This is one of the
reason ruby rocks.
Robert H. (Guest)
on 2007-06-11 21:28
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
'' :)
Aureliano C. (Guest)
on 2007-06-11 21:34
(Received via mailing list)
require *%w( yaml pp )
should work.
Ben B. (Guest)
on 2007-06-11 21:40
(Received via mailing list)
On Tue, Jun 12, 2007, Aureliano C. wrote:
> require *%w( yaml pp )
> should work.

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
Rob B. (Guest)
on 2007-06-11 21:56
(Received via mailing list)
On Jun 11, 2007, at 1:28 PM, Marc H. wrote:

>
> One reason why I do not so much like require 'something' is because of
> the
> '' :)
>
> --
> Posted via http://www.ruby-forum.com/.

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 B.    http://agileconsultingllc.com
removed_email_address@domain.invalid
Robert K. (Guest)
on 2007-06-11 21:57
(Received via mailing list)
On 11.06.2007 19:28, Marc H. wrote:
> 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

You need require *%w(...) with the redefinition that Jesse gave.  Note
the asterix.

> One reason why I do not so much like require 'something' is because of
> the
> '' :)

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
Eric H. (Guest)
on 2007-06-12 12:30
(Received via mailing list)
On Jun 11, 2007, at 10:20, Marc H. wrote:
> "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.

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.
Robert H. (Guest)
on 2007-08-01 05:12
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?
Robert K. (Guest)
on 2007-08-02 02:02
(Received via mailing list)
On 01.08.2007 03:12, Marc H. wrote:
>   Why does require allow only one argument?
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
Ruben M. (Guest)
on 2007-08-02 05:05
(Received via mailing list)
> > The question should have simply been:
>
> >   Why does require allow only one argument?
>
> 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.

Perhaps the use for aditional parameters in next versions.
This topic is locked and can not be replied to.