Earlier quoted context omitted.
Personally I find the Ruby easier to read. Just follow the functions left to right, and the method names are more literate (chars() vs split('') for instance). I’m also not sure at first what slicing on an array with a negative start index will do, whereas I’m sure each_cons does just what it purports to do.
Fair enough. In js slicing with a negative index would grab from the end of the array, but if the end index is positive it'll return an empty string so it's safe in this case I think the tradeoff is how much of the API you need to memorize. In JS if you know .filter, .map, and .reduce you're good for 95% of use-cases (technically .reduce can be used to implement every other method, but it can get messy). I also work…
Ruby delights built into the language
41–50 of 89 posts
Re: Ruby delights built into the language
#42Earlier quoted context omitted.
I love method_missing - one simple method unlocks so much power and eliminates so much boiler plate.
… and ruins the life of future maintainers, there’s a reason few languages tried to borrow features from ruby’s metaprogramming (saying this as a rubyist)
Re: Ruby delights built into the language
#43I 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…
Re: Ruby delights built into the language
#44I 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…
Re: Ruby delights built into the language
#45Seems to be just a random collection of random ruby snippets, for clicks? Surprised to see it voted up.
Oh wait... they are snippets from the ruby distribution "sample" folder? I didn't even know there was a ruby distribution "sample" folder.
Looking at the git history, it looks like while a few of the "samples" have been touched in the past few years -- others haven't been touched in 10+ years, and I think maybe all of them originate 10+ years ago... maybe all of them originate from ruby original release? I don't think most people even know about this "sample" folder.
So I guess OP is giving us a tour of it, ok. It's... not that interesting.
https://github.com/ruby/ruby/blob/ruby_3_1/sample/
Some of the samples show you how to use parts of the stdlib, which I guess can be useful. Others, like that second example "biorhythm", are just kind of inexplicable random scripts. (What even is a "biorhythm"?). None of them seem to have any comments (perhaps because they were written by Japanese speakers decades ago?)
These literally decades-old snippets of unclear purpose are probably not actually great examples of how to write current ruby. They may be interesting historically, I wonder how they even got here.
Re: Ruby delights built into the language
#46I 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…
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
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?!?
Re: Ruby delights built into the language
#47I 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…
That 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.
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.
Re: Ruby delights built into the language
#48Earlier quoted context omitted.
I struggle with code like this (also, complicated piped shell statements). How do you debug/reason about this? I know this specific example may be an exercise, but still.
Ruby is a surprisingly simple language. Metaprogramming in ruby is a pretty good book that makes it clear what is happening under the hood. The yield is probably the most complicated part of it, but it is extremely useful for hiding complexity. Once you understand yield, there is very little magic to what ruby is doing. I prefer python to ruby, but the concept is the same: https://realpython.com/introduction-to-pytho…
Re: Ruby delights built into the language
#49Earlier quoted context omitted.
… and ruins the life of future maintainers, there’s a reason few languages tried to borrow features from ruby’s metaprogramming (saying this as a rubyist)
Metaprogramming is powerful for making really nice, easy-to-use _libraries_. It shouldn't ever be used for business logic. Ever. Unless you're going to go all in and make a DSL for your business, but I don't know of anyone who actually does that.
The smart programmers want to write libraries. This is actually a problem for businesses, because they often don't need libraries.
Re: Ruby delights built into the language
#50I 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…
So 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 A file whose commit history shows... it's part of the very first…
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.