11 ms·
Ruby delights built into the language
- skitter 4y ago> 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. 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?
- fknorangesite 4y ago> I though this would be about different parts of the standard library, but it's about files from the example folder. All of the examples given use only the standard lib, afaict.
- horsawlarway 4y agoYet there's almost no real discussion of how to use the standard library here, and a lot of talking about example code that's mostly useless (seriously - why is biorhythm pseudoscience #2, and how is displaying a calendar on the CLI going to help me...?) I agree with the top post - I have a very different idea about what "built-in" means than the author of this article.
- Lammy 4y ago> seriously - why is biorhythm pseudoscience #2 It's an interactive tutorial on using the `Date` and `OptionParser` classes and printing stuff to stdout. > and how is displaying a calendar on the CLI going to help me It's an interactive tutorial on using the `Date` and `GetoptLong` classes and printing stuff to stdout :)
- horsawlarway 4y agoAnd that's fine. But that topic in the article is not Date/OptionParser/GetOptLong - it's "This week I'm sharp-as-a-tack, and slow and sad as a sack. Here's to next week!" and I don't see any value add above linking to examples (not built-ins). Ex - why not explore OptionParser (the actual built-in!) - and compare to ARGV, or contrast with gets.chomp. Or explain how similar the concept is to something like Yargs from the node side. Also, as a personal aside - I don't really see a bunch of custom formatted printf statements with no attempt at explanation a very good example at all.
- fknorangesite 4y ago> how is displaying a calendar on the CLI going to help me How is displaying "Hello world" on the CLI going to help me?
- halfmatthalfcat 4y ago
- o_1 4y ago
- sodapopcan 4y agoSomeone submitted it and people voted on it.
- halfmatthalfcat 4y ago
- sodapopcan 4y agoRuby is still relevant and people are still learning it. There's some good stuff in there (though I agree I don't understand the "built in" claim) and stuff I didn't know after getting back into it after being away from it for a couple of years.
- vlunkr 4y agoFrom the guidelines: Please don't post shallow dismissals, especially of other people's work There are tons of examples, with links to the code, I think it's fairly informative.
- drusepth 4y agoPeople that write ruby usually do so because they like ruby (as opposed to just doing it for a job, etc). Therefore, people who write ruby are probably going to upvote submissions about the cool quirks of ruby -- and there are a lot of people who write ruby.
- phoronixrly 4y agoThis article is kind of niche... My favourite Ruby delights are among others, methods provided by the Enumerable mixin https://rubyapi.org/3.1/o/enumerable https://rubyapi.org/3.1/o/enumerable. 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: File.read('input6.txt').chars.each_cons(4).find_index { _1.uniq == _1 } + 4
- tra3 4y agoI 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.
- yxhuvud 4y agoYou read it from left to right and figure out what the result of each step (marked by .) is before you tackle the next part of it.
- phoronixrly 4y agoFirst, it's an exercise, you don't get to use much of this functionality day to day, at least if you're a lowly webdev :(. Second, the solution is optimized for speed of writing (you get more points in AOC the quicker you submit the solution), not for readability. I try to get the chain of calls in my Ruby REPL as soon as the challenge drops. Third, if you so wish to debug this chain of calls, you can insert a breakpoint and get a REPL anywhere in it with Object#tap: foo.bar(3).tap { |chain| binding.irb }.baz { |larodi| larodi + 5 } And lastly, it becomes second nature to read and write these.
- culi 4y agoCode like this should definitely not be written in production code bases and is really just a fun exercise. I think everyone would agree that if this had been written in 2 or 3 lines it'd be much more readable (and therefore maintainable)
- 4y ago
- lamontcg 4y agoThis 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.
- phoronixrly 4y agoI mean, no one's making you use this functionality, why should it be removed?
- booleandilemma 4y agoNot the parent, but my usual response to this is always "well someone's going to use it in the codebase, which means I'm going to have to end up using it too".
- lamontcg 4y agoYeah, code maintenance isn't actually free.
- jrockway 4y agoOnce things are in the standard library, they're basically impossible to remove, unless you want to break everyone's code and prevent them from updating. If you're making a new programming language today, the standard library is a place where you should think very carefully about everything you add. Every programmer learning the language will have to be familiar with the standard library, so keeping it small is good. But too small, and programmers will always have to reach for third-party packages without maintenance or stability guarantees. For example, you don't want to have to choose among 5 different libraries to write and run unit tests; the language should define that and it should meet everyone's needs (or be extensible with a small third-party library). I think the Rubys and the Perls of the world probably choose too much to add to the standard library, and that's where the comment you're replying to comes from. But, while it's easy to overdo the standard library, it's also bad if you underdo the standard library. Tough and sometimes under-considered aspect of language design.
- medellin 4y ago
- chromatin 4y agoInteresting; I don't think physicians, the target market, like them much either. It's like a lame facebook/linkedin for healthcare professionals.
- area51org 4y agoI 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.
- jrochkind1 4y agoI don't really understand what this post is. Seems 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/ 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.
- barrenko 4y agoAs 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. Also, why is Ruby "editor responsiveness" (not sure what to call it) so much worse than e.g. Python?
- isomorphic 4y agoIf you're looking for IDE-level language assistance, I can't help you, but since you mentioned nvim: I use regular vim with CoC / Conquer of Completion (vim plugin; LSP server, may not strictly be necessary for nvim), Solargraph (Ruby Gem; language server), and Rubocop (also a Gem) for linting. I previously/still use ALE (vim plugin; Asynchronous Lint Engine) because I haven't gotten CoC+Solargraph to play nice with Rubocop, probably due to something silly. https://github.com/neoclide/coc.nvim https://github.com/neoclide/coc.nvim https://solargraph.org/ https://solargraph.org/ https://rubocop.org/ https://rubocop.org/ https://github.com/dense-analysis/ale https://github.com/dense-analysis/ale My impression with all of this running under MacVim... it's plenty responsive. It can take a while for Solargraph to index everything on startup if you're working in a big project; once it loads, it's snappy. (There's probably a way to cache that startup scan.)
- winrid 4y agoYou can pay for RubyMine once and use that version forever (or until you are not poor :) )
- zanellato19 4y agoVS Code with Ruby extensions is what most people use from my experience. I have no idea what you mean by editor responsiveness.
- barrenko 4y ago"Dotting" into some property or method for ruby works noticeably slower than for any other language I've ever tried, same for autocomplete.
- H12 4y ago
- nomilk 4y agoHow do I run these? For example: ``` ruby sample/biorhythm.rb ruby: No such file or directory -- sample/biorhythm.rb (LoadError) ``` (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?
- codenamev 4y agoI will update the article, but all of the code snippets were run from the root of the ruby source code. So before you run them: git clone https://github.com/ruby/ruby.git https://github.com/ruby/ruby.git && cd ruby
- annadiru 4y agomost 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. 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. 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. 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. I could (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. TL;DR: what i'd like to see as a newcomer to the language is a canonized and community-driven knowledge base, ala wiki. *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 *the doc entry for Symbol#to_proc is exactly one line and doesn't even use the method! it uses the &: operator.
- Toutouxc 4y ago> array.each on an array of intergers will not behave the same as an array of strings What do you mean by this?
- annadiru 4y agoThe discussion is in the chat history of the ruby channel in matrix chat. (lol) Alas, logs aren't easily accessible in the element android app (which is what i've got).
- codenamev 4y agoHey everyone! OP and author here. 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 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. 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.