Live data from Hacker News

Ruby delights built into the language

technology.doximity.com

21–30 of 89 posts

Re: Ruby delights built into the language

#21
post #16

This 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 . 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

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.

You 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.

Re: Ruby delights built into the language

#22

Earlier quoted context omitted.

I mean, no one's making you use this functionality, why should it be removed?

Once 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.…

This is a very good point. Things done wrong in the standard library are often done wrong permanently and forever. Particularly given the current demands from some people for perfect backwards compatibility.

Looking at the stdlib for a language like Rust it tends to feel like people who had 20+ years of experience in other languages went and thought deeply about the problem, with an understanding that accepting the design might be permanent.

Re: Ruby delights built into the language

#23
post #16

This 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 . 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

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.

First, 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.

Re: Ruby delights built into the language

#24

This 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 . 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

Hmm that's a good show of Ruby's great built-ins for Enumerable, but can't any language with a findIndex and a Set achieve the same thing?

I.e. in JS:

  myString.split('').findIndex((c, i, arr) => new Set(arr.slice(i-4, i)).size == 4)

Re: Ruby delights built into the language

#25
post #16

This 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 . 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

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.

Code 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)

Re: Ruby delights built into the language

#26
post #16

This 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 . 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

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.

As you get used to it, it becomes highly readable. The 'tap' method to see what the elements look like at an intermediary step is also really helpful. I usually build these up left to right as well though and won't add on the next processing step unless I'm very certain there period ones are correct

Re: Ruby delights built into the language

#27

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.

I love method_missing - one simple method unlocks so much power and eliminates so much boiler plate.

Re: Ruby delights built into the language

#28

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.

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

#29
post #25
post #16

Earlier 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.

Code 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)

Truly, if this was not meant to be pasted in a REPL, it'd be in a class, and probably constitute of several descriptively named methods.

Re: Ruby delights built into the language

#30
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.
Post reply on HN