Hacker News new | past | comments | ask | show | jobs | submit login

Totally agree! Other tricks I rely on:

a) put a `binding.irb` (or `binding.pry`) in any rescue block you may have in your script - it'll allow you to jump in and see what went wrong in an interactive way. (You'll need a `require 'irb'` in your script too, ofc)

b) I always use `Pathname` instead of `File` - it's part of the standard library, is a drop in replacement for `File` (and `Dir`) and generally has a much more natural API.

c) Often backticks are all you need, but when you need something a little stronger (e.g. when handling filenames with spaces in them, or potentially hostile user input, etc), Ruby has a plethora of tools in its stdlib to handle any scenario. First step would be `system` which escapes inputs for you (but doesn't return stdout).

d) Threads in Ruby are super easy, but using `Parallel` (not part of the stdlib) can make it even easier! A contrived example: `Parallel.map(url_list) { |url| Benchmark.measure { system('wget', url) }.real }.sum` to download a bunch of files in parallel and get the total time.

MacOS has Ruby 2.6 installed by default which is perfectly serviceable, but it's EOL and there are plenty of features in 3+ that make the jump more than worthwhile.




> You'll need a `require 'irb'` in your script too, ofc

irb is a part of Ruby core, so this isn’t true. (It may have been at one point? I’m not sure.)

I love binding.irb. I use it all the time.


Huh, TIL! Requiring `irb` has been unnecessary since 2.5 and I never noticed.


Me too (binding.pry). I think of it as REPL based development.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: