Ruby 'require' with wildcard

Is there a way to upload files that match only a specific line? For example, suppose I want to upload files matching 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 it won’t work. What is the best way to do this?

+6
source share
3 answers

You can do the same with a loop:

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

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

+12
source

I tried using this to create a test suite for Minitest without using Rspec. The accepted answer did not work for me, but it happened:

 require "minitest/autorun" Dir.glob("*_test.rb").each do |file| require_relative file end 
+1
source

I use this:

 Dir.entries( File.dirname( __FILE__ ) ). grep( /test.*\.rb/ ) { | file | require_relative file } 

Unfortunately, Ruby does not allow strings before the dot, so the perfect form does not work:

 Dir.entries( File.dirname( __FILE__ ) ) .grep( /test.*\.rb/ ) { | file | require_relative file } 
0
source

Source: https://habr.com/ru/post/903958/


All Articles