Behavior when wild card (**) is specified in ruby


It's not a big story, but I'm a little worried, so it's a memo.

The'**' wildcard is useful, isn't it? However, when I passed it to options with ruby, the behavior was strange.

Who will deploy it?

It is a shell. So fish will receive'** /', but bash won't. So what about ruby?

Two correct ways

ruby hage.rb */hoge.

When you write, fish will expand it in advance.

p ARGV[0]
> ruby bin/calc_site_ks.rb **/d_ein*.o*
["double_new_model/vol_098/d_ein_098.o22192", "double_new_model/vol_100/d_ein_100_0_24.o22179", "double_new_model/vol_100/d_ein_100_24_44.o22180", "double_new_model/vol_102/d_ein_102.o22194", "double_new_model/vol_104/d_ein_104.o22195"]

ruby hage.rb '*/hoge.'

If you write, it will not expand.

> ruby bin/calc_site_ks.rb '**/d_ein*.o*'

So, after that, expand it with Dir.glob (ARGV [0]).

In normal command usage, it is not enclosed in quotes and is expanded as it is. In that case, the order of options is important. Be careful if you use optparse or thor, but you may need to be a little careful with the command you usually use.


Start ruby also specifies the specifications. Says,

The string specified in argument is set as the initial value of the built-in constant Object :: ARGV. In environments where the standard shell does not expand wildcards (Win32), the Ruby interpreter expands the wildcards and sets it to Object :: ARGV. In this case you can use *',?', []', ** /'as wildcards. In the Win32 environment, if you want to suppress wildcard expansion, enclose the argument in single quotes (').

Recommended Posts

Behavior when wild card (**) is specified in ruby
Docker does not work when DOCKER_HOST is specified in WSL2
Behavior when each is executed in the reverse order Range
Encoding when getting in Windows + Ruby
Is there no type in Ruby?
Be careful when omitting return in Ruby
[Technical memo] What is "include" in Ruby?
[Ruby] Your Ruby version is 2.6.3, but your Gemfile specified 2.5.8
Command when dependency is broken in Ubuntu 20.04
Your Ruby version is 2.4.6, but your Gemfile specified 2.6.4
Do not return when memoizing in Ruby
[Ruby] How to prevent errors when nil is included in the operation
[Ruby] Behavior of evaluation of conditional expression in while
The ruby version is managed in the .rbenv / version file
When the project is not displayed in eclipse
Class in Ruby
Heavy in Ruby! ??
BUNDLE_APP_CONFIG which is troublesome when developing Ruby with DevContainer
Data acquisition method memo when there is HashMap in HashMap
Correspondence when Ruby version does not switch in rbenv
When Spring Batch is executed continuously in Oracle, ORA-08177
[Swift] Setting the behavior when the Share button is pressed
[Ruby] Thinking when there is no receiver in front of the method (it looks like)