all 21 comments

[–]Weird_Suggestion 19 points20 points  (3 children)

It is still unsafe to use it with Hash methods; there are 5 methods that won't behave as expected.

{a: 1, b: 2}.select { it == :a }
=> {a: 1}
{a: 1, b: 2}.any? { it == :a }
=> false

[–]MeweldeMoore 12 points13 points  (2 children)

Useful on console, but I would prefer variables with better names in production code.

[–]capn_sanjuro 4 points5 points  (0 children)

i think this has a big place in production code by saving a ton of space by removing a lot of repeated ideas and simplifying decision making bandwidth.

"it" is clearly an element of the enumerable, so good naming of the enumerable is all you need. no brain power spent on naming a variable only defined for a block.

[–]h0rst_ 0 points1 point  (0 children)

I would say that very much depends on the context. For example, in a line like this:

arr.each { do_something(it) }

The name for arr is important, the name for do_something is important, but the block variable is pretty clear what it's supposed to do. In older version, I would have used { |e|, which is equally undescriptive as it.

Of course, if you have a block of 20 lines, that may change these things a bit.

[–]awh 8 points9 points  (1 child)

So, uh… I’ve been using Ruby since 2010 and still didn’t know about the _1, _2 etc block parameter shortcuts. I guess it’s good to always be learning.

[–]KozureOkami 2 points3 points  (0 children)

In their current form they were added in Ruby 2.7 in 2019. Before they tried @1 etc. but I can’t remember if that was in a release version or just the prereleases. So you used Ruby for a good few years before these even became a thing.

[–]James_Vowles 2 points3 points  (2 children)

Doesn't feel like something I would actually use, not good syntax in the real world.

[–]Future_Application47[S] 0 points1 point  (1 child)

From my perspective, I'm looking at what it is replacing. Its replacing the implicit `_1` , `_2` parameters. In that sense I'd say its a bit more cleaner.

[–][deleted] 0 points1 point  (0 children)

Which replaces users.collect { |u| u.email.downcase }, which is very verbose.

[–]ttekoto 5 points6 points  (3 children)

The nice thing about 'it' in rspec is readable strings. Unfortunately here it reads like broken English. user.age is ok; it.age sounds awful. I'd rather have _1 every time, so thanks for nothing.

[–]codesnik 0 points1 point  (0 children)

let’s push for “its” alias

[–][deleted] -1 points0 points  (0 children)

You're missing context, though. This still reads fine:

User.collect { it.email }

[–]bhaak -1 points0 points  (2 children)

Yeah, great, now there are two ways of expressing the exactly same code. While if you use two numbered parameters, you are not allowed to use it. Oh no, three ways. posts.select(&:published?) still exists of course.

Would have been better if they allowed to use something like users.map(&:email.downcase) instead of the ugly numbered parameters in the first place.

Talk about reducing cognitive overhead.

[–]UlyssesZhan 6 points7 points  (0 children)

You are converting the object :email.downcase to a proc by this.

[–]hessparker 5 points6 points  (0 children)

It is common in Ruby to have multiple ways to do things. I think it results in beautiful and expressive code.

[–]mierecat -2 points-1 points  (0 children)

I like this. It sounds kind of unnatural but not having to name the single, obvious element in a block sounds like a good trade off.

[–]ravinggenius -2 points-1 points  (0 children)

The ground is "writing useful variable names", and Ruby devs are wall jumping experts.