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.
4828d528e2e46f7c8160c336eb332836?d=identicon&s=25 Robert Heiler (shevegen)
on 2007-06-10 21: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?
3afd3e5e05dc9310c89aa5762cc8dd1d?d=identicon&s=25 Tim Hunter (Guest)
on 2007-06-10 22:17
(Received via mailing list)
Marc Heiler 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.
B63d3390f6285b1af0f1662687911a95?d=identicon&s=25 Jesse Merriman (Guest)
on 2007-06-10 22:35
(Received via mailing list)
On Sunday 10 June 2007 15:24, Marc Heiler 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
4828d528e2e46f7c8160c336eb332836?d=identicon&s=25 Robert Heiler (shevegen)
on 2007-06-11 19: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 Merriman. This is one of the
reason ruby rocks.
4828d528e2e46f7c8160c336eb332836?d=identicon&s=25 Robert Heiler (shevegen)
on 2007-06-11 19: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
'' :)
69498abbf47d4967f8640317a58377dd?d=identicon&s=25 Aureliano Calvo (Guest)
on 2007-06-11 19:34
(Received via mailing list)
require *%w( yaml pp )
should work.
90a73d9875462aaa9fab2feffafbffe7?d=identicon&s=25 Ben Bleything (Guest)
on 2007-06-11 19:40
(Received via mailing list)
On Tue, Jun 12, 2007, Aureliano Calvo 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
Ef3aa7f7e577ea8cd620462724ddf73b?d=identicon&s=25 Rob Biedenharn (Guest)
on 2007-06-11 19:56
(Received via mailing list)
On Jun 11, 2007, at 1:28 PM, Marc Heiler 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 Biedenharn    http://agileconsultingllc.com
Rob@AgileConsultingLLC.com
E0d864d9677f3c1482a20152b7cac0e2?d=identicon&s=25 Robert Klemme (Guest)
on 2007-06-11 19:57
(Received via mailing list)
On 11.06.2007 19:28, Marc Heiler 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
58479f76374a3ba3c69b9804163f39f4?d=identicon&s=25 Eric Hodel (Guest)
on 2007-06-12 10:30
(Received via mailing list)
On Jun 11, 2007, at 10:20, Marc Heiler 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.
4828d528e2e46f7c8160c336eb332836?d=identicon&s=25 Robert Heiler (shevegen)
on 2007-08-01 03: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?
E0d864d9677f3c1482a20152b7cac0e2?d=identicon&s=25 Robert Klemme (Guest)
on 2007-08-02 00:02
(Received via mailing list)
On 01.08.2007 03:12, Marc Heiler 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
F2c730957ef0aec5616091b30fe88075?d=identicon&s=25 Ruben Medellin (chubas)
on 2007-08-02 03: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.