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.<p>This:<p><pre><code> dirp = Dir.open(".")
for f in dirp
case f
when /\.rb\z/
print f, "\n"
else
# do not print
end
end
dirp.close
</code></pre>
Could be made much more Ruby-native (and simpler!) as:<p><pre><code> Dir.open('.').each do |f|
puts f if f.match?(/\.rb\z/)
end
</code></pre>
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.
I don't really understand what this post is.<p>Seems to be just a random collection of random ruby snippets, for clicks? Surprised to see it voted up.<p>Oh wait... they are snippets from the ruby distribution "sample" folder? I didn't even know there <i>was</i> a ruby distribution "sample" folder.<p>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 <i>originate</i> 10+ years ago... maybe all of them originate from ruby original release? I don't think most people even know about this "sample" folder.<p>So I guess OP is giving us a tour of it, ok. It's... not that interesting.<p><a href="https://github.com/ruby/ruby/blob/ruby_3_1/sample/">https://github.com/ruby/ruby/blob/ruby_3_1/sample/</a><p>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?)<p>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.
This article is kind of niche... My favourite Ruby delights are among others, methods provided by the Enumerable mixin <a href="https://rubyapi.org/3.1/o/enumerable" rel="nofollow">https://rubyapi.org/3.1/o/enumerable</a>. There's a pile of functionality there that helps with tedious tasks. E.g. here's a solution to this year's AOC challenge #6 using Enumerable's each_cons method:<p><pre><code> File.read('input6.txt').chars.each_cons(4).find_index { _1.uniq == _1 } + 4</code></pre>
> There are so many Ruby features built in to the language itself. In the very first episode of this series, we dove deep into the internals of one such gem bundled with Ruby's standard library: IRB. Keeping on theme, I'd like to take some time to explore some other areas of Ruby's source.<p>From that introduction I though this would be about different parts of the standard library, but it's about files from the example folder. In how far are these built in?
Hey everyone! OP and author here.<p>I would like to thank everyone for such a lively discussion! It has been a joy reading through and feeling the sentiment ebb and flow in that traditional HN fashion<p>For those curious and inquisitive minds out there, this article was intended to provoke emotion, fuel your creative soul, and pull you in to the Ruby source code. The precursor to this article was one I wrote on how IRB works and many of it's features. I had accidentally stumbled upon this "samples" directory and like many here wondered _why_ it is there and _what purpose_ was it serving. While many of the examples are a bit dated, they all make use of the standard library in various ways, and many make use of Ruby in ways _I_ hadn't seen before. Even if you don't care for the examples themselves, I hope to leave you with ample reading material to explore those unfamiliar crevices of Ruby.<p>As many have shouted, there are quite a few pain points in the Ruby language documentation, best practices, discoverability, and onboarding of new comers. This is an incredible time to be a part of this community and make this language as enjoyable to learn as it is to write.
most of the posts here are criticisms of the OPs efforts. they seem misplaced. Ruby lacks accessible, consistent, example-based explorations that cover the breadth of the language in practice-- as a programming language, rather than just a dsl for OO and REST apps.<p>I'm fairly new to Ruby and to programming in general. I've found so far that Ruby is sorely underdocumented. Coming from JS, where we have MDN, the docs for Ruby are obtuse and don't provide examples.<p>If you do google search you get wordy, outdated, non-idiomatic code examples that often feel incomplete or are even flat out wrong. There are a lot of gotcha's and hangups in the language* that you can sometimes find in books-- but not consistently and never in blog posts.<p>My learning process so far has been to look at the docs, which often are EMPTY (see Symbol#to_proc)*. Then I'll read through 5-6 blog posts from 2016, not find the answer, then do searches through multiple (expensive) books that are almost always nothing succinct (not reference style). I eventually have to ask on a chatroom or some other asynch forum, in which case that knowledge has no foward discoverability and the experts in the language find themselves in a samsara of questions/answers.<p>I <i>could</i> (and often) do github-wide searches of usages of a specific method or idiom, but that is tedious, inconsistent, and overly dependent on one of many features on a 3rd-party service.<p>TL;DR: what i'd like to see as a newcomer to the language is a canonized and community-driven knowledge base, ala wiki.<p>*an example of a hangup: some types are immutable and others not. array.each on an array of intergers will not behave the same as an array of strings.. so for ints you have to use map for certain transformations<p>*<i>the doc entry for Symbol#to_proc is exactly one line and doesn't even use the method! it uses the &: operator.</i>
This almost reads more like a list of cruft which should be removed from the stdlib (obviously not benchmark.rb though). For delegation it is probably better to use forwardable and be explicit about delegated methods--delegate.rb smells like it uses method_missing magic.
As a newb, what editor to use for Ruby? It's one of those languages that VS Code does not cover properly and I can't really find good setup for nvim or emacs. RubyMine works wonders but I'm poor.<p>Also, why is Ruby "editor responsiveness" (not sure what to call it) so much worse than e.g. Python?
How do I run these? For example:<p>```<p>ruby sample/biorhythm.rb<p>ruby: No such file or directory -- sample/biorhythm.rb (LoadError)<p>```<p>(I know I could simply copy these files off GitHub, but does the IRB on my computer already know about these biorhythm/calendar files somehow). Am I missing something?