Hacker News new | ask | show | jobs
by torben-friis 531 days ago
I happen to be reading the latest edition of the pickaxe book, as I'll soon be using Ruby at work (first experience) and this 'it' update sounds so much better than the _1 described in the book.

I remember thinking it clashed a bit with the idea of trying to make the code read like natural language.

3 comments

_1 is also a bit of a footgun. It becomes an array of all block params IF the block has an arity > 1 AND no other block param (e.g. _2) is referenced anywhere within the block.

It's an unusually half-baked feature by Ruby's standards. I think there was some hesitation about the name "it" initially, because "it" is an essential method name in rspec, and is used within blocks there.

Oh damn. I have been using `_1`, `_2`, etc. extensively for years... I didn't know about that footgun.

I'd like know if `it` is merely an alias for `_1`, or if protects from this "arity issue" too.

I pity anyone needing to debug a fiddly Rspec problem in the next few years.
IIRC, the decision was that it in a block is only ever called without arguments, e.g. map { it * 2 }, and it in Rspec is only ever called with arguments, e.g. it "describes a test" do ... end. So it ended up being a non-issue.
I can totally see some add on gem for rspec breaking that assumption.
FYI, one of my favorite Ruby idioms is to capture command output with `open(...){_1.read}`

  pp open("|ls -lh /usr/bin/ls"){_1.read}
  "-rwxr-xr-x 1 root root 135K Aug 30 04:57 /usr/bin/ls\n"
or to quickly print tabular data

  open("|column -t -s \\t", "w"){_1 << tsv_data}
Am I missing something - it's 2am here, so possibly - or could you not just use backticks for the first one?
Oh that's embarrassing -- you're right, the first example doesn't make sense. Kinda pointless actually haha

I use it for code like this:

  raw = IO.popen(%W[gzip -d -c -- #{path}]){_1.read}
  raw = IO.popen(%W[gzip -d -c], in: pipe){_1.read}
  raw = IO.popen(%W[git status --porcelain -- #{path}]){_1.read}
Useful because it eliminates /bin/sh footguns (e.g. `md5sum hello&world` forking or `~john` expanding), plus you get kernel.spawn() options. `open("| ...)` only made sense for the second example.

Anyway, I find "_1" more eye-pleasing than:

  IO.popen(%W[gzip -d -c #{path}]){|io| io.read}
Did you know you were going to use Ruby before Applying? Just wondering.
Yup. I don't mind switching languages, I've done it a few times and I enjoy learning new things.

The market for ruby seems to have good salaries and job satisfaction despite being smallish, so it didn't seem like a bad area to get some experience in.

Why, is there any issue with the choice I'm not aware of?

>Why, is there any issue with the choice I'm not aware of?

No, not at all. I was just interested since there is a sudden influx of people joining Ruby ( I guess mostly Rails ) companies without previous Ruby background. As I have notice this across HN, Reddit, Twitter and elsewhere. And yes Ruby market tends to be on the slightly higher end because they mostly hire people with years of programming experiences and lack of junior position. And there used to be complains about not being able to find people with Ruby / Rails experiences etc. So it is a good market shift I guess.

Ah well, as soon as I even glanced at Ruby my social media feeds were filled with tech influencers.

There is a niche there of very popular channels producing Ruby tutorials, seemingly aimed at the junior js-bootcamp dev crowd. They also focus on Neovim, tmux and other terminal based tools.

It is a completely anecdotal observation, but my guess is that it might explain part of the trend.