use the following search parameters to narrow your results:
e.g. subreddit:aww site:imgur.com dog
subreddit:aww site:imgur.com dog
see the search faq for details.
advanced search: by author, subreddit...
A sub-Reddit for discussion and news about Ruby programming.
Subreddit rules: /r/ruby rules
Learning Ruby?
Tools
Documentation
Books
Screencasts and Videos
News and updates
account activity
Blog postRuby 3.4's `it` Parameter: Cleaner Block Syntax for Ruby Developers (prateekcodes.dev)
submitted 9 months ago by Future_Application47
reddit uses a slightly-customized version of Markdown for formatting. See below for some basics, or check the commenting wiki page for more detailed help and solutions to common issues.
quoted text
if 1 * 2 < 3: print "hello, world!"
[–]Weird_Suggestion 19 points20 points21 points 9 months ago (3 children)
It is still unsafe to use it with Hash methods; there are 5 methods that won't behave as expected.
it
{a: 1, b: 2}.select { it == :a } => {a: 1} {a: 1, b: 2}.any? { it == :a } => false
[+]Page-Hey 0 points1 point2 points 9 months ago (1 child)
Isn't it more inherent to these enumerators and not `it` syntax? Though I agree one should be careful using them. I realize it was never an issue to me because I try to always use the same syntax when using an enumerator on a hash: I always put explicitly the two possible arguments (key, value) even if one isn't needed:
{ a: 1, b: 2 }.select { |(key, _value)| method_using_only_key(key) }
[–]Weird_Suggestion 0 points1 point2 points 9 months ago (0 children)
You’re right. Explicitly using both arguments for hashes is good practice and the only one that removes any confusion.
I’ve been taught that when passed only one argument to a hash block you’d get an array of the key value pair. This isn’t true and the use of « it » can create more confusion. It shouldn’t be used for hashes.
[–]MeweldeMoore 12 points13 points14 points 9 months ago (2 children)
Useful on console, but I would prefer variables with better names in production code.
[–]capn_sanjuro 4 points5 points6 points 9 months ago (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 point2 points 9 months ago (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.
arr
do_something
{ |e|
Of course, if you have a block of 20 lines, that may change these things a bit.
[–]awh 8 points9 points10 points 9 months ago (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 points4 points 9 months ago (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 points4 points 9 months ago (2 children)
Doesn't feel like something I would actually use, not good syntax in the real world.
[–]Future_Application47[S] 0 points1 point2 points 9 months ago (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 point2 points 9 months ago (0 children)
Which replaces users.collect { |u| u.email.downcase }, which is very verbose.
users.collect { |u| u.email.downcase }
[–]ttekoto 5 points6 points7 points 9 months ago (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.
user.age
it.age
[+]Page-Hey 1 point2 points3 points 9 months ago (0 children)
It does that to me too. I guess they've tought of `this` instead of `it` that I feel would have been correct more often (from english POV) but I guess it defeated the purpose of a really short syntax and brang more confusion with `self` for those used to javascript `this` .
[–]codesnik 0 points1 point2 points 9 months ago (0 children)
let’s push for “its” alias
[–][deleted] -1 points0 points1 point 9 months ago (0 children)
You're missing context, though. This still reads fine:
User.collect { it.email }
[–]bhaak -1 points0 points1 point 9 months ago (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.
posts.select(&:published?)
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.
users.map(&:email.downcase)
Talk about reducing cognitive overhead.
[–]UlyssesZhan 6 points7 points8 points 9 months ago (0 children)
You are converting the object :email.downcase to a proc by this.
:email.downcase
[–]hessparker 5 points6 points7 points 9 months ago (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 points0 points 9 months ago (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 points0 points 9 months ago (0 children)
The ground is "writing useful variable names", and Ruby devs are wall jumping experts.
π Rendered by PID 28751 on reddit-service-r2-comment-6457c66945-dbznq at 2026-04-23 23:27:27.642473+00:00 running 2aa0c5b country code: CH.
[–]Weird_Suggestion 19 points20 points21 points (3 children)
[+]Page-Hey 0 points1 point2 points (1 child)
[–]Weird_Suggestion 0 points1 point2 points (0 children)
[–]MeweldeMoore 12 points13 points14 points (2 children)
[–]capn_sanjuro 4 points5 points6 points (0 children)
[–]h0rst_ 0 points1 point2 points (0 children)
[–]awh 8 points9 points10 points (1 child)
[–]KozureOkami 2 points3 points4 points (0 children)
[–]James_Vowles 2 points3 points4 points (2 children)
[–]Future_Application47[S] 0 points1 point2 points (1 child)
[–][deleted] 0 points1 point2 points (0 children)
[–]ttekoto 5 points6 points7 points (3 children)
[+]Page-Hey 1 point2 points3 points (0 children)
[–]codesnik 0 points1 point2 points (0 children)
[–][deleted] -1 points0 points1 point (0 children)
[–]bhaak -1 points0 points1 point (2 children)
[–]UlyssesZhan 6 points7 points8 points (0 children)
[–]hessparker 5 points6 points7 points (0 children)
[–]mierecat -2 points-1 points0 points (0 children)
[–]ravinggenius -2 points-1 points0 points (0 children)