Cleanest/One-liner way to require all files in directory in Ruby?
Posted
by viatropos
on Stack Overflow
See other posts from Stack Overflow
or by viatropos
Published on 2010-03-21T19:09:59Z
Indexed on
2010/03/21
19:11 UTC
Read the original article
Hit count: 249
When creating gems, I often have a directory structure like this:
|--lib
|-- helpers.rb
`-- helpers
|-- helper_a.rb
`-- helper_b.rb
Inside the helpers.rb
, I'm just require
-ing the files in the helpers
directory. But I have to do things like this:
$:.push(File.dirname(__FILE__) + '/helpers')
require 'helper_a'
require 'helper_b'
Is there a way to make that one line so I never have to add to it? I just came up with this real quick:
dir = File.join(File.dirname(__FILE__), "helpers")
Dir.entries(dir)[2..-1].each { |file| require "#{dir}/#{file[0..-4]}" }
But it's two lines and ugly. What's a slick one liner to solve this problem?
© Stack Overflow or respective owner