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.
Ruby delights built into the language
21–30 of 89 posts
Re: Ruby delights built into the language
#22Earlier 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.…
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
#23This 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.
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
#24This 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.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
#25This 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.
Re: Ruby delights built into the language
#26This 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.
Re: Ruby delights built into the language
#27This 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.
Re: Ruby delights built into the language
#28This 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
#29Earlier 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)
Re: Ruby delights built into the language
#30This:
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.