Write Software, Well

January 19, 2023

Method Shorthand in Ruby

While reading the docs, I learned that Ruby (3.0 and up) provides a shorthand syntax for one-line methods, similar to computed properties in C#.

Ruby Method Shorthand

Personally, I don't plan to use them in my code, except in tests. I prefer the standard syntax. The shorthand also looks confusing with the setter methods that already have a = in the name. For me, the saved keystrokes aren't worth the mental overhead.

I also shared the above code on Reddit and LinkedIn, and there was a lot of interesting discussion with nuanced comments.

What do you think? Too much syntactic sugar, or a nice way to express short properties? Let me know in the comments.

Subscribe to get future posts via email (or grab the RSS feed)

Comments

No comments yet. Be the first to leave one.

Sign in to leave a comment.