Ruby is faster to write and more concise by a long shot—certainly if we're talking back-end web services. I prefer Go to Ruby generally but it's weird that Go developers tend not to even see how much yak shaving is involved with it. Or really any downside with it.
Try writing this in Go:
records = User.limit(10)
records.select { |user| user.is_admin? }.map{|user| "#{user.id} : #{user.name}"} records := Stream.Limit(User, 10)
records.Select(func(user User) bool {return user.IsAdmin()}).Map{func(user User) string { return fmt.Sprintf("%d %s", user.id, user.name) }
Yes a bit more verbose, that is the price to pay for static typing without full type inference as in the ML language family.https://go.googlesource.com/proposal/+/master/design/go2draf...
records.collect({case u if u.isAdmin => s”${u.id} ${u.name}”}).take(10) guess the language
That line of code could be Scala, or something else.
In any case, the point is that there are compiled languages that offer similar productivity like Ruby, with AOT/JIT compilers out of the box, and great IDE experiences.
In Go, the intuitive way would be one loop with an if inside it. So it would be easy to spot this performance issue if you did it the ruby way with two loops.
https://go.dev/play/p/ixDKuosxPNp
I bet I wrote my for loop as quickly as you wrote your map. The character count isn't that much higher.
The more important thing is the ruby version is chainable and more readable—and that matters far more often.
> more readable
Without having more context, User in your code is possibly an ActiveRecord class, so to read the #select, you have to consider that it could be radically different and heavier than Enumerable#select (eg. it may execute a DB query as a side effect). So I like your example still, because it’s representative, and certainly expressive, but not readable per se. IMO this sin here is simply that ActiveRecord shouldn’t have overloaded select.
Using an ORM was a poor choice on my part. I agree the magic there does harm readability since it makes it a lot harder to understand what it really is doing.
In fact given that only a subset of users will be admin users for the second loop n is going to < 10 anyway.
I believe it was Rob Pike that actually said something along the lines of not bothering to optimise until you know that n is large[2].