Requiring more than one file?

M

Marc Heiler

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?
 
T

Tim Hunter

Marc said:
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'
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.
 
J

Jesse Merriman

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?

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
 
M

Marc Heiler

"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.
 
M

Marc Heiler

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
'' :)
 
B

Ben Bleything

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
 
R

Robert Klemme

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
 
R

Rob Biedenharn

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
'' :)

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
(e-mail address removed)
 
E

Eric Hodel

"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.
 
M

Marc Heiler

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?
 
R

Robert Klemme

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?

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
 
R

Rubén Medellín

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.
 

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. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

[ANN] JRuby 1.0.2 Released 0
[ANN] JRuby 1.6.0.RC2 released 0
ANN main-4.4.0 0
[ANN] Facets 1.8 4
[ANN] JRuby 1.1RC2 Released 1
[ANN] main-3.0.1 0
[ANN] JRuby 1.1 RC 1 Released 20
[ANN] main-4.0.0 (for avdi) 0

Members online

No members online now.

Forum statistics

Threads
473,743
Messages
2,569,478
Members
44,898
Latest member
BlairH7607

Latest Threads

Top