The Dir::glob method takes an argument and provides an array of all directories and files nested in the argument. (From there, you can capture the index of the array using [0] .) The argument may contain a pattern that must match, along with the flags. The argument (pattern, flags) can be parameters similar to (but not exactly) regular expressions.
From the documents, including a couple of templates / flags that may interest you:
Please note that this pattern is not a regular expression; it is closer to the shell shell. See File.fnmatch for the value of the flags parameter. Note that case sensitivity is case-sensitive (therefore File::FNM_CASEFOLD ignored), as is the order in which the results are returned.
* - matches any file. May be limited by other values ββin glob. Equivalent to / .* /x in regexp.[set] - matches any character in the set. It behaves exactly like character sets in Regexp , including the negative value set ( [^az] ).
The abbreviation Dir::glob() is equal to Dir[] , although I prefer the long form. As you saw above, using parentheses means a special pattern / flag for the argument. Here are a few examples (from the docs) that may better explain this:
Dir["config.?"] #=> ["config.h"] Dir.glob("config.?") #=> ["config.h"] Dir.glob("*.[az][az]") #=> ["main.rb"] Dir.glob("*") #=> ["config.h", "main.rb"]
You can override the [] method for Dir , but I will not show how many (and I) do not recommend using the Ruby core classes and modules for monkeys. However, you can create a method in your own class. See the following:
class User # Class method => User.new[arg] def self.[](arg) end # Instance method => @user[arg] def [](arg) end end
source share