4 ms·
I appreciate the Ruby love in this post, but it's worth pointing out that the author is clearly new to the language. I say that because he writes Ruby as if he'
by area51org 4y ago
I appreciate the Ruby love in this post, but it's worth pointing out that the author is clearly new to the language. I say that because he writes Ruby as if he's writing some other language, and not idiomatic Ruby.
This:
dirp = Dir.open(".")
for f in dirp
case f
when /\.rb\z/
print f, "\n"
else
# do not print
end
end
dirp.close
Could be made much more Ruby-native (and simpler!) as:
Dir.open('.').each do |f|
puts f if f.match?(/\.rb\z/)
end
My point isn't to play the "I know the language better than you, so nyah nyah" game, just to suggest that if the author likes Ruby as much as he says, he should learn it better.
- neilpointer 4y agoYou couldn't be more wrong, and a simple Google search of the author was all you had to do lol. It's pretty clear here (given his background) that the author is writing in a style to make the code examples more accessible to a wider audience.
- deleted 4y ago[deleted]
- tmm84 4y agoThat snippet looks like Python/Lua/C with the for, case and close being used. Ruby is one of those languages that like Lisp/Scheme, Smalltalk or Haskell requires you to know what is going on as well as what is available to come up with a clean/simple solution. I think finding good Ruby resources that are up to date are few but I believe most of the stuff out there still works in recent versions.
- artificialLimbs 4y ago>> I think finding good Ruby resources that are up to date are few... I just finished a class that was taught with Ruby. It seems like an absolutely delightful language, but I could not find docs pertaining to several projects that I had conceptualized as my 'final project'. Microsoft Graph API has no docs available for Ruby.
- antod 4y ago> That snippet looks like Python/Lua/C with the for, case and close being used Not really Python like at all - apart from the one line with for. Python doesn't have a case or when statement, or syntax level regex, doesn't open or close directories, and for files it would idiomatically would use the context manager for file access rather than closing it. But it also doesn't look like any Ruby I've seen or written either :)
- jrochkind1 4y agoSo the weirdest part is that the OP is literally just a cut and paste of a bunch of scripts found in the ruby distribution "samples" folder (that I had no idea even existed). That pretty not great ruby wasn't written by the author -- it's actually included in a "sample" file with the ruby distro? https://github.com/ruby/ruby/blob/ruby_3_1/sample/dir.rb https://github.com/ruby/ruby/blob/ruby_3_1/sample/dir.rb A file whose commit history shows... it's part of the very first commit recorded in git history, in 1998 by matz, the original author of ruby. I have no idea what's going on in that "sample" folder, very weird. The OP is simply regurgitating the weird "sample" folder. The "author" of the OP didn't write any of this code. According to git history... matz did?!?
- ysavir 4y ago> A file whose commit history shows... it's part of the very first commit recorded in git history, in 1998 by matz, the original author of ruby. So not necessarily written by Matz, but part of the body of work that eventually saw a `git init`, and early enough that much of the modern standard Ruby formatting was still emerging. Doesn't seem all that surprising to me in the context of when it was written.
- brasic 4y agoThis was written by matz some time between 1993 and 1994. It appeared in the very first preview release of ruby, version 0.49. The original tarball has the file modification timestamps: $ curl -s https://cache.ruby-lang.org/pub/ruby/1.0/ruby-0.49.tar.gz | tar -tvz | grep dir.rb -rw-r--r-- 0 matz root 127 Jun 3 1994 ruby/sample/dir.rb
- deleted 4y ago[deleted]
- Lammy 4y agoFun retrocomputing diversion: Initial Ruby development was done on Sony NEWS, the same type of workstation that hosted the earliest PlayStation development boards: https://en.wikipedia.org/wiki/Sony_NEWS https://en.wikipedia.org/wiki/Sony_NEWS
- brasic 4y agoYou're quite right -- the author of that code *was* very new to the language when he wrote this, but then again, so were we all. [0] [0] https://github.com/ruby/ruby/commit/3db12e8b236ac8f88db8eb4690d10e4a3b8dbcd4#diff-a9e391df69e6624bd355a2b429d8d85ae7bc6993addf4b7223adedf09ba762c6 https://github.com/ruby/ruby/commit/3db12e8b236ac8f88db8eb46...
- quechimba 4y agoOr even: puts Dir["*.rb"]
- kgeist 4y agoI'm not familiar with Ruby, so I have a question: the first snippet has the .close method called while the second doesn't. Does the second example leak resources, or it's automatically closed after a GC in a finalizer, maybe? Does .each close the directory? Or, maybe, .close is a no-op in newer versions?
- semiquaver 4y agoYou’re quite right. OP’s rewrite contains a bug: it leaks one file descriptor per invocation. The correct version passes a block to `open` directly so that it’s automatically closed when the block is done executing. Maybe golfing down to be “idiomatic” isn’t always best :) Dir.open('.') do |dirp| dirp.each do |f| puts f if f.match?(/\.rb\z/) end end https://ruby-doc.org/core-3.1.0/Dir.html#method-c-open https://ruby-doc.org/core-3.1.0/Dir.html#method-c-open There are a few ruby stdlib classes like Tempfile that use the finalizer trick you mention to free resources on GC but Dir isn’t one of them. Here’s it’s implementation: https://github.com/ruby/ruby/blob/1a24442193fe437e761e941d1ae0923d45fbb92f/dir.c https://github.com/ruby/ruby/blob/1a24442193fe437e761e941d1a...
- voraciousdev 4y agoProbably worth mentioning that each of those code snippets links out to a core sample in the ruby/ruby repository. There is a source link directly above the code you posted. https://github.com/ruby/ruby/blob/ruby_3_1/sample/dir.rb https://github.com/ruby/ruby/blob/ruby_3_1/sample/dir.rb