[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Requiring more than one file?

Marc Heiler

6/10/2007 7:25:00 PM

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?

--
Posted via http://www.ruby-....

12 Answers

Tim Hunter

6/10/2007 8:16:00 PM

0

Marc Heiler wrote:
> 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.

--
RMagick OS X Installer [http://rubyforge.org/project...]
RMagick Hints & Tips [http://rubyforge.org/forum/forum.php?for...]
RMagick Installation FAQ [http://rmagick.rubyforge.org/instal...]


Jesse Merriman

6/10/2007 8:34:00 PM

0

On Sunday 10 June 2007 15:24, Marc Heiler wrote:
> 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


--
Jesse Merriman
jessemerriman@warpmail.net
http://www.jessemer...

Marc Heiler

6/11/2007 5:20:00 PM

0

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

--
Posted via http://www.ruby-....

Marc Heiler

6/11/2007 5:28:00 PM

0

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

--
Posted via http://www.ruby-....

aurelianito

6/11/2007 5:33:00 PM

0

require *%w( yaml pp )
should work.

On 6/11/07, Marc Heiler <shevegen@linuxmail.org> 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
>
>
> One reason why I do not so much like require 'something' is because of
> the
> '' :)
>
> --
> Posted via http://www.ruby-....
>
>

Ben Bleything

6/11/2007 5:40:00 PM

0

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

Robert Klemme

6/11/2007 5:52:00 PM

0

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

Rob Biedenharn

6/11/2007 5:55:00 PM

0


On Jun 11, 2007, at 1:28 PM, 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
>
>
> One reason why I do not so much like require 'something' is because of
> the
> '' :)
>
> --
> Posted via http://www.ruby-....

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://agileconsult...
Rob@AgileConsultingLLC.com



Eric Hodel

6/12/2007 8:29:00 AM

0

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.

--
Poor workers blame their tools. Good workers build better tools. The
best
workers get their tools to do the work for them. -- Syndicate Wars



Marc Heiler

8/1/2007 1:13:00 AM

0

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?

--
Posted via http://www.ruby-....