How [] works on a class in Ruby

I see that I can get a list of files in a directory using

Dir["*"] 

How should I read this syntax exactly? As I know, you can use [] to retrieve a value from an array or hash.

How does [] work on the call?

+5
source share
4 answers

From Ruby Docs , Dir["*"] equivalent to Dir.glob(["*"]) . (As indicated, this is syntactic sugar )

Dir not a call, it is a class, and class Dir objects are directory streams that you access as an array.

In your particular case, Dir["*"] will return an array file names found from the template passed as Dir[patternString] . "*" because the pattern will match zero or more characters, in other words, it will match all and, thus, will return an array all the file names in this directory.

For your second question, you can simply define it like any other method as follows:

 class YourClass def self.[](v) #your code here end end 
+4
source

[] is just a method, for example #to_s, #object_id. and etc.

You can define it on any object:

 class CoolClass def [](v) puts "hello #{v}" end end CoolClass.new["John"] # => "hello John" 

In your case, it is defined as a singleton method, thus:

 class Dir def self.[](v) ... end end 
+5
source

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 
+2
source

Dir is an object, like any other object (it’s just an instance of the Class class), and [] is a method similar to any other method (it just has a funny name, and special syntactic amenities that allow it to call the use of other syntax in addition to normal).

So you define it just like any other method:

 class MyClass def self.[](*) end end 
+1
source

All Articles