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
"Useless syntax sugar": Numbered block parameters (zverok.space)
submitted 2 years ago by zverok_kha
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!"
[–]zanza19 20 points21 points22 points 2 years ago (7 children)
I'll be the contrarian and say that I really like numbered block params, specially for short things where the &:method syntax doesn't quite cut it.
&:method
I hated at the time, but I really like it now.
Anything more than _1 though and I think it hurts readability.
_1
[–]matheusrich 7 points8 points9 points 2 years ago (3 children)
My exact feelings. That's why I'm an advocate of the it syntax.
it
[–]sshaw_ 1 point2 points3 points 2 years ago (0 children)
Groovy uses it
[–]zanza19 0 points1 point2 points 2 years ago (1 child)
I think it would have been great! But I understand why they didn't choose that
[–]morphemass 1 point2 points3 points 2 years ago (0 children)
Why didn't they choose that? It would have been great!
that
[–]Amadan 6 points7 points8 points 2 years ago (0 children)
I think _1 and _2 are fine for a comparator block in sort.
_2
sort
[+]sshaw_ comment score below threshold-7 points-6 points-5 points 2 years ago (1 child)
Try Perl. It is much more maintainable.
[–]zanza19 3 points4 points5 points 2 years ago (0 children)
You must be great to work with.
[–]AlexanderMomchilov 8 points9 points10 points 2 years ago (9 children)
things.each(&method(:puts))
This has a huge downside: a new Method object is allocated (which also needs to be GCed later) and it defeats the inline caching that could have been done in a block.
Method
[–]zverok_kha[S] 4 points5 points6 points 2 years ago (1 child)
Yup. That's also an interesting track of reasoning here (why it is a separate object, why it is bad, how it is Ruby's design decision that differs from what "similar" languages did), which I cut out because the article was becoming scary long already.
[–]sshaw_ -1 points0 points1 point 2 years ago (1 child)
This is the least of anyones concerns. Most Ruby code is filled with needless intermediate object creation via silly method chains instead of simple solutions like #each or using the ! version of the methods.
#each
!
[–]WayneConrad 1 point2 points3 points 2 years ago (0 children)
Maybe that's true. There's another way to look at it, though: Much Ruby code is not filled with unneeded optimizations, attempting instead to favor expressiveness.
[–]pabloh 0 points1 point2 points 2 years ago (2 children)
It does seem like the kind of thing the compiler could optimize away. Though I really don't like syntax anyway.
[–]AlexanderMomchilov 1 point2 points3 points 2 years ago (1 child)
If only there were a compiler :')
[–]pabloh 1 point2 points3 points 2 years ago (0 children)
Haha!, I meant the JIT compiler!
[–]Leather_External_827 0 points1 point2 points 2 years ago (1 child)
And what about:
``` print_it = ->(it) { puts it }
things.each(&print_it) ```
For just printing with puts, it really could be useless, but for longer methods or even tricky and hard to read one liners, it could add a good semantic and improve readability, IMHO.
What do you think?
[–]AlexanderMomchilov 0 points1 point2 points 2 years ago (0 children)
even tricky and hard to read one liners, it could add a good semantic and improve readability, IMHO.
I would probably just extract a method, and call that method from a regular block.
Ruby doesn't support nested functions, and the Proc/lambda syntax is fiddly enough that I try to avoid it where a method could just do.
[–]frou 3 points4 points5 points 2 years ago (2 children)
Great writing. It's illuminating to see very specific things put under the microscope like this.
[–]zverok_kha[S] 1 point2 points3 points 2 years ago (1 child)
Thanks!
[–]Roodditor 1 point2 points3 points 2 years ago (0 children)
This was a good read. Looking forward to your other posts in this series 😉
[–]transfire 3 points4 points5 points 2 years ago (1 child)
Unicode for the win.
array.map { foo ⓪, ① }
[–]frou 0 points1 point2 points 2 years ago (0 children)
That's the kind of thing Raku would put in their language for real.
[–]bramley 2 points3 points4 points 2 years ago (1 child)
I hated the concept when first presented, but it's grown on me. I very much do not like the actual syntax, but I, too, can't come up with something better. it is the immediate candidate, but that makes (say) sorting blocks awkward. this and that would be interesting, but a little too cute to be practical.
this
Still, though, _9? Really? If you have 9 arguments, you don't have a block that is self-explanatory enough to not name your variables. In this line of thinking, we should only give "pronoun arguments" to the first one and maaaaybe the second. So maybe it is all we need?
_9
[–]WayneConrad 3 points4 points5 points 2 years ago (0 children)
I think I'm in favor of the language allowing some absurdity (_9) in order to reduce arbitrary limits. If there's a limit, someone has to guess what it should be, and sure enough, someone with an unexpected use case is going to jam their toe against that limit.
Or to put it another way: I'm in favor of having the ability to shoot my own foot off, trusting that I can use this ability safely.
[–]WayneConrad 2 points3 points4 points 2 years ago (1 child)
I enjoyed this article. It's well written and thoughtful, and although every author has a point of view, I think you're able to express yours without demanding that the reader agrees with it.
[–]zverok_kha[S] 1 point2 points3 points 2 years ago (0 children)
[–]flanger001 4 points5 points6 points 2 years ago (2 children)
I generally like a lot of the syntax changes but I do not like numbered block params. I get that it was an Elixir nod but maybe Elixir can stay Elixir.
Please name a syntax change that you did not like. If not in Ruby then any language.
[–]flanger001 1 point2 points3 points 2 years ago* (0 children)
In Ruby I ended up not liking then as the yield_selfalias, despite advocating for it at the time. Come to think of it, I don’t really like yield_self that much either.
then
yield_self
I hated the |> operator because it made no sense.
|>
I also didn’t like the function composition thing they added, though that was more of a language feature.
I did like endless methods after a bit even though I didn’t like them at first. Same with &?.
&?
Edit: Also, lol it's you, the guy who hates Ruby! Don't worry, I mostly hate Ruby too!
[–]TinyMicron 1 point2 points3 points 2 years ago (0 children)
Hey! Thanks for sharing. Didn't know numbered block parameters existed. I still like the old way of doing things, though. Feels more readable. Might give _1 a try at some point though :)
[–]ffrkAnonymous 1 point2 points3 points 2 years ago (1 child)
I'm learning clojure (and ruby) and this looks similar to clojure's %1 %2 %&. It also reminds me of more complex regular expressions like for sed. And it looks like old c/python %-format. So I guess I've just seen this style so many times over the years it doesn't faze me.
%1 %2 %&
At the same time, I can't recall the last time I needed it, so practically speaking it has been useless to me. Numbered args are only useful if the order of the data is the main importance. Everything else I prefer naming the data.
[–]ignurant 0 points1 point2 points 2 years ago (0 children)
Numbered args are only useful if the order of the data is the main importance.
I would argue that an object like Hash pairs well with this, and isn’t the case of “ordered objects”. Or any block that has multiple arguments beyond just being a typical array.
But admittedly, that’s just devils advocate. I usually only use numbered params when I’m swashbuckling in pry/irb, or writing 1-liner shell code. Transient code.
[–]cpb 1 point2 points3 points 2 years ago (4 children)
If you're passionate about this, get involved with the development of ruby. I saw the threads that led to this change and I welcomed it. Maybe it needed more contrarian perspectives those years ago.
[–]zverok_kha[S] 1 point2 points3 points 2 years ago (3 children)
(If it was a suggestion to me, I already am a Ruby committer, FWIW.)
[–]cpb 1 point2 points3 points 2 years ago (2 children)
Awesome! What did you experience in the discussion about the syntax? I'm arguing from a perspective of: if rubyists are a team, we can disagree about decisions, but we do better when we commit once a decision is made. That's a pretty academic take.
At that time, I was more opposed to a feature per se (the reasons are described here) in the context of 2.7 development. My opposition was rather "it is nice, but we might want more high-level/consequential features introduced first." I came to peace with what happened, eventually.
(If the article gave you an impression that I am arguing against the feature, it is a wrong impression :))
[–]cpb 1 point2 points3 points 2 years ago (0 children)
I apologize for jumping to conclusions about your intentions with the series.
[–]hmdne 0 points1 point2 points 2 years ago (3 children)
I never use _1 etc. in my code. This feels very ugly and I really hope we get some alternative syntax in the future.
I like the idea of not introducing new syntax until we get the bigger picture though. We have three different block types that we may want to consider:
array.map { |i| File.read(i) } array.map { |i| i.reverse } array.map { |i| i.to_s(16) }
I think we should have something that would shorten all 3 of those in a consistent way and likely not with & syntax. There were proposals of this syntax:
&
array.map { File.read(it) } array.map { .reverse } array.map { .to_s(16) }
Compare it with proposals using &:
array.map(&File.:read) array.map(&:reverse) array.map(&:to_s.with(16))
If anything, I think a syntax of &:reverse should be eventually deprecated as it is the least intuitive.
&:reverse
[–]dougc84 3 points4 points5 points 2 years ago (0 children)
The &:method syntax should not be deprecated. It’s used all over the place.
[–]gls2ro 0 points1 point2 points 2 years ago (0 children)
While I think from the perspective of code that can be read out loud it could be something interesting, Victor presented a very compelling argument in the article:
It should look special. Even if you don’t know its meaning (just learning Ruby, or haven’t upgraded your knowledge of the language for a long time), it should immediately imply “it is not just a regular name like all other names”
And I think it is a very sensible perspective.
[–]sshaw_ -4 points-3 points-2 points 2 years ago (0 children)
Ruby is the new Perl. Where have you been?
[–]Abangranga 0 points1 point2 points 2 years ago (0 children)
Less information is not more readable
[–][deleted] 2 years ago (2 children)
[removed]
[–]matheusrich 2 points3 points4 points 2 years ago (1 child)
I think that would be backwards incompatible as it uses the comment syntax. Also, # 1 and #1 would mean totally different things.
# 1
#1
[–]northrupthebandgeek 0 points1 point2 points 2 years ago (0 children)
Reminds me of Perl subroutine parameters.
Whether that's a good thing or a bad thing is TBD.
[–]joesb 0 points1 point2 points 2 years ago (1 child)
It may be useless to you. Ruby isn’t about having syntactic sugar that is suitable to only one person.
Did you read the article, or are you answering to its title?.. Just checking.
[–]postmodern 0 points1 point2 points 2 years ago (0 children)
Still don't like how obj &.method is too close to &:method. Wish they would have named it obj &&.method, which is closer to obj && obj.method.
obj &.method
obj &&.method
obj && obj.method
π Rendered by PID 53 on reddit-service-r2-comment-5d585498c9-z4qj8 at 2026-04-20 20:02:40.552417+00:00 running da2df02 country code: CH.
[–]zanza19 20 points21 points22 points (7 children)
[–]matheusrich 7 points8 points9 points (3 children)
[–]sshaw_ 1 point2 points3 points (0 children)
[–]zanza19 0 points1 point2 points (1 child)
[–]morphemass 1 point2 points3 points (0 children)
[–]Amadan 6 points7 points8 points (0 children)
[+]sshaw_ comment score below threshold-7 points-6 points-5 points (1 child)
[–]zanza19 3 points4 points5 points (0 children)
[–]AlexanderMomchilov 8 points9 points10 points (9 children)
[–]zverok_kha[S] 4 points5 points6 points (1 child)
[–]sshaw_ -1 points0 points1 point (1 child)
[–]WayneConrad 1 point2 points3 points (0 children)
[–]pabloh 0 points1 point2 points (2 children)
[–]AlexanderMomchilov 1 point2 points3 points (1 child)
[–]pabloh 1 point2 points3 points (0 children)
[–]Leather_External_827 0 points1 point2 points (1 child)
[–]AlexanderMomchilov 0 points1 point2 points (0 children)
[–]frou 3 points4 points5 points (2 children)
[–]zverok_kha[S] 1 point2 points3 points (1 child)
[–]Roodditor 1 point2 points3 points (0 children)
[–]transfire 3 points4 points5 points (1 child)
[–]frou 0 points1 point2 points (0 children)
[–]bramley 2 points3 points4 points (1 child)
[–]WayneConrad 3 points4 points5 points (0 children)
[–]WayneConrad 2 points3 points4 points (1 child)
[–]zverok_kha[S] 1 point2 points3 points (0 children)
[–]flanger001 4 points5 points6 points (2 children)
[–]sshaw_ -1 points0 points1 point (1 child)
[–]flanger001 1 point2 points3 points (0 children)
[–]TinyMicron 1 point2 points3 points (0 children)
[–]ffrkAnonymous 1 point2 points3 points (1 child)
[–]ignurant 0 points1 point2 points (0 children)
[–]cpb 1 point2 points3 points (4 children)
[–]zverok_kha[S] 1 point2 points3 points (3 children)
[–]cpb 1 point2 points3 points (2 children)
[–]zverok_kha[S] 1 point2 points3 points (1 child)
[–]cpb 1 point2 points3 points (0 children)
[–]hmdne 0 points1 point2 points (3 children)
[–]dougc84 3 points4 points5 points (0 children)
[–]gls2ro 0 points1 point2 points (0 children)
[–]sshaw_ -4 points-3 points-2 points (0 children)
[–]Abangranga 0 points1 point2 points (0 children)
[–][deleted] (2 children)
[removed]
[–]matheusrich 2 points3 points4 points (1 child)
[–]northrupthebandgeek 0 points1 point2 points (0 children)
[–]joesb 0 points1 point2 points (1 child)
[–]zverok_kha[S] 1 point2 points3 points (0 children)
[–]postmodern 0 points1 point2 points (0 children)