<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: darcylol</title>
    <description>The latest articles on DEV Community by darcylol (@darcylol).</description>
    <link>https://dev.to/darcylol</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F3031002%2Fe55eec49-c2d6-42f5-8559-9413cb5b3003.png</url>
      <title>DEV Community: darcylol</title>
      <link>https://dev.to/darcylol</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/darcylol"/>
    <language>en</language>
    <item>
      <title>Many ways to express one thing in Ruby (_n syntax, _ prefix, &amp; ampersand)</title>
      <dc:creator>darcylol</dc:creator>
      <pubDate>Tue, 08 Apr 2025 21:55:50 +0000</pubDate>
      <link>https://dev.to/darcylol/many-ways-to-express-one-thing-in-ruby-n-syntax-prefix-ampersand-2h0j</link>
      <guid>https://dev.to/darcylol/many-ways-to-express-one-thing-in-ruby-n-syntax-prefix-ampersand-2h0j</guid>
      <description>&lt;p&gt;Today, coding in Ruby reminds me of the days when I was learning French. I wasn’t bad at expressing myself, but I struggled with vocabulary. One day, I needed to draft a professional email in French. After spending hours checking my grammar and searching for the right words, I hoped my language teacher would give me the green light to send it out.&lt;/p&gt;

&lt;p&gt;While she acknowledged that I had conveyed the meaning correctly, she also pointed out that I was repeating myself. She then introduced me to different ways to say the same thing, just to avoid using the same word.&lt;/p&gt;

&lt;p&gt;Below is an example of expressing the same thing in many ways in Ruby. This is an array of arrays, where each inner array contains a date and a volume value. Our goal is to calculate the total volume. &lt;br&gt;
&lt;code&gt;[["2025-05", 1400], ["2025-04", 4200], ["2025-03", 3100]]&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Here are a few ways to do that in Ruby:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;Desctructure manually by assigning variables to each element&lt;br&gt;
&lt;code&gt;total_volume = data.sum { |date, volume| volume }&lt;/code&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Desctructure manually and only assign variable that you are going to use. '_' in this context is considered as a thrown-away or not-used element&lt;br&gt;
&lt;code&gt;total_volume = data.sum { | _, volume | volume }&lt;/code&gt;&lt;br&gt;
You can use chain map and sum.&lt;br&gt;
&lt;code&gt;total_volume = data.map { | _, volume | volume }.sum&lt;/code&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Desctructure and make use of numbered parameter. Ruby reads the patter of each inner array (e.g ["2025-05", 1400]), first element("2025-05") as _1, second element (1400) as _2. &lt;br&gt;
&lt;code&gt;total_volume = data.sum { _2 }&lt;/code&gt;&lt;br&gt;
reference: &lt;a href="https://ruby-doc.org/core-2.7.0/Proc.html#class-Proc-label-Numbered+parameters" rel="noopener noreferrer"&gt;https://ruby-doc.org/core-2.7.0/Proc.html#class-Proc-label-Numbered+parameters&lt;/a&gt;&lt;br&gt;
It felt odd, but saving time and space to name your variable in an iteration. Why not? &lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Here is the part when things get "Frenchy"(aka inconsistent and full of exceptions). Let's say now the data I need to deal with changes to&lt;br&gt;
&lt;code&gt;new_data = [[1400, "2025-05"], [4200, "2025-04"], [3100, "2025-03"]]&lt;/code&gt;. &lt;br&gt;
Now the volume is the first element of each inner array, and we want to sum all those volumes.&lt;br&gt;
From what we learned earlier, it might feel natural to write:&lt;br&gt;
&lt;code&gt;total_volume = new_data.sum { _1 }&lt;/code&gt;&lt;br&gt;
But nope! ❌ Since there is only 1 parameter, the destructuring magic disappears and _1 just gives you the whole pair, not the first item in the pair([1400, "2025-05"]). &lt;br&gt;
Hence if you are only passing _1 in the block, you have to index into the array explicitly. Like this:&lt;br&gt;
&lt;code&gt;total_volume = data.sum { _1[0]}&lt;/code&gt;&lt;br&gt;
To go back to the example at the beginning of the article, you can write below to achieve the same result&lt;br&gt;
&lt;code&gt;total_volume = data.sum { _1.last}&lt;/code&gt;&lt;br&gt;
Confusing! Isn't it? &lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Use &amp;amp; ampersand operator to represent the iterating element. Note: This approach calls a method on the element, so use : instead of .&lt;br&gt;
&lt;code&gt;total_volume = data.sum(&amp;amp;:last)&lt;/code&gt;&lt;br&gt;
&lt;code&gt;total_volume = data.map(&amp;amp;:last).sum&lt;/code&gt;&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Ruby often feels like a language built on the motto:&lt;br&gt;
“There should be many -- and sometimes too many ways to do it, implicit or explicitly.”&lt;/p&gt;

&lt;p&gt;(Yes I am trying to contradict the Zen of Python: "there should be one-- and preferably only one --obvious way to do it")&lt;/p&gt;

&lt;p&gt;I like shortcuts. I like flexibility. But I’m not always convinced a language needs so many different ways to say the same thing. Ideally, I’d rather use a language to express many different ideas.&lt;/p&gt;

&lt;p&gt;So, what about you?&lt;/p&gt;

&lt;p&gt;What’s your favourite (or least favourite) Ruby shorthand? And if you code in another language, does it offer similar shortcuts?&lt;/p&gt;

&lt;p&gt;Let’s chat 👇&lt;/p&gt;

</description>
      <category>ruby</category>
      <category>webdev</category>
      <category>rails</category>
    </item>
  </channel>
</rss>
