I have no idea which performs better, a grep with a look-around or
calling a block (Proc object) on every element in Dir.new().entries.
I'd be interested in performance metrics there.
BTW, Dir[pattern] is almost the same thing. It's a bit cumbersome,
though. It works a little more like find, than ls. E.g.
irb(main):002:0> Dir.enum
=> ["dir_ls.rb", "doc"]
irb(main):003:0> Dir['*']
=> ["dir_ls.rb", "doc"]
irb(main):004:0> Dir['doc/*']
=> ["doc/_index.html", "doc/class_list.html", "doc/css",
"doc/Dir.html", "doc/file_list.html", "doc/index.html", "doc/js",
"doc/method_list.html", "doc/top-level-namespace.html"]
irb(main):005:0> Dir.enum('doc')
=> ["_index.html", "class_list.html", "css", "Dir.html",
"file_list.html", "index.html", "js", "method_list.html",
"top-level-namespace.html"]
Then to do a recursion:
irb(main):022:0> Dir['doc/**/*']
=> ["doc/_index.html", "doc/class_list.html", "doc/css",
"doc/css/common.css", "doc/css/full_list.css", "doc/css/style.css",
"doc/Dir.html", "doc/file_list.html", "doc/index.html", "doc/js",
"doc/js/app.js", "doc/js/full_list.js", "doc/js/jquery.js",
"doc/method_list.html", "doc/top-level-namespace.html"]
irb(main):023:0> Dir.enum_r('doc')
=> ["doc/_index.html", "doc/class_list.html", "doc/css",
["doc/css/common.css", "doc/css/full_list.css", "doc/css/style.css"],
"doc/Dir.html", "doc/file_list.html", "doc/index.html", "doc/js",
["doc/js/app.js", "doc/js/full_list.js", "doc/js/jquery.js"],
"doc/method_list.html", "doc/top-level-namespace.html"]
Dir.enum_r gives you a tree structure, whereas Dir['**/*'] gives you a
flat array. Depends upon ypur needs I guess. Dir.ls and dir.ls_r
could/should be rewritten using Dir[pattern], with some work to
tranform the arg into the correct pattern. Using Dir.new() does not
take a glob. Dir.glob(pattern) does and takes a block.
First incomplete attempt:
class Dir
def
self.ls(pattern='*', &block)
files = glob(pattern, &block)
puts files.join(" ") unless block_given?
end
end
BTW, tried the similar rush example and could not get it to work, am I
missing anything? ( doc['**'] => undefined method or variable
error.)
As far as ===, this has always been confusing to me. You have ==, ===,
.eql? and .equal? As best as I know, === is the case/when comparison.
As a boolean, it holds true whenever that would be true in a case
statement's 'when' expression. For instance: 1939 == 1900..1999 is
false, but 1939 === 1900..1999 is true. Because case 1939; when
1900..1999 would execute that when's then clause, since it is in that
range.
The mechanics of the operator are that it is a method defined in all
base Ruby objects. You can define it in your own objects if you want
them to be used in case expressions (or in the standalone 'a === b'
instnnce).
Regards,
Ed