Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ruby 'require' with wildcard expression

Tags:

ruby

Is there a way to load files that only match a specific string? For example, suppose I want to load files that matches account1.rb account2.rb and so on. I want to be able to do something like

require File.expand_path("../account*.rb", __FILE__)

but of course this does not work. What is the best way to do this?

like image 659
denniss Avatar asked Dec 16 '11 22:12

denniss


2 Answers

You can do the same thing with a loop:

Dir.glob(File.expand_path("../account*.rb", __FILE__)).each do |file|
  require file
end

The expand_path method only resolves paths. It does not expand wildcards.

like image 168
tadman Avatar answered Sep 22 '22 20:09

tadman


I tried to use this to create a test-suite for Minitest without using Rspec. The accepted answer didn't work for me, but this did:

require "minitest/autorun"

Dir.glob("*_test.rb").each do |file|
    require_relative file
end
like image 36
leondepeon Avatar answered Sep 21 '22 20:09

leondepeon