<story><title>Ruby delights built into the language</title><url>https://technology.doximity.com/articles/ruby-delights-built-into-the-language</url></story><parent_chain><item><author>tra3</author><text>I struggle with code like this (also, complicated piped shell statements). How do you debug&amp;#x2F;reason about this? I know this specific example may be an exercise, but still.</text></item><item><author>phoronixrly</author><text>This article is kind of niche... My favourite Ruby delights are among others, methods provided by the Enumerable mixin &lt;a href=&quot;https:&amp;#x2F;&amp;#x2F;rubyapi.org&amp;#x2F;3.1&amp;#x2F;o&amp;#x2F;enumerable&quot; rel=&quot;nofollow&quot;&gt;https:&amp;#x2F;&amp;#x2F;rubyapi.org&amp;#x2F;3.1&amp;#x2F;o&amp;#x2F;enumerable&lt;/a&gt;. There&amp;#x27;s a pile of functionality there that helps with tedious tasks. E.g. here&amp;#x27;s a solution to this year&amp;#x27;s AOC challenge #6 using Enumerable&amp;#x27;s each_cons method:&lt;p&gt;&lt;pre&gt;&lt;code&gt;    File.read(&amp;#x27;input6.txt&amp;#x27;).chars.each_cons(4).find_index { _1.uniq == _1 } + 4&lt;/code&gt;&lt;/pre&gt;</text></item></parent_chain><comment><author>hayst4ck</author><text>Ruby is a surprisingly simple language. &lt;i&gt;Metaprogramming in ruby&lt;/i&gt; is a pretty good book that makes it clear what is happening under the hood.&lt;p&gt;The yield is probably the most complicated part of it, but it is extremely useful for hiding complexity. Once you understand yield, there is very little magic to what ruby is doing.&lt;p&gt;I prefer python to ruby, but the concept is the same:&lt;p&gt;&lt;a href=&quot;https:&amp;#x2F;&amp;#x2F;realpython.com&amp;#x2F;introduction-to-python-generators&amp;#x2F;&quot; rel=&quot;nofollow&quot;&gt;https:&amp;#x2F;&amp;#x2F;realpython.com&amp;#x2F;introduction-to-python-generators&amp;#x2F;&lt;/a&gt;&lt;p&gt;As for understanding the command line&amp;#x2F;reasoning about it, they are usually generated iteratively.&lt;p&gt;&lt;pre&gt;&lt;code&gt;  Look at a file to see what you have to work with:
    $ cat $file | head -n 5
  Decide commas aren&amp;#x27;t useful and remove them
    $ cat $file | sed &amp;#x27;s&amp;#x2F;,&amp;#x2F;&amp;#x2F;g&amp;#x27; | head -n 5
  Decide you want a tab between every 4 characters on any given line
    $ cat $file | sed &amp;#x27;s&amp;#x2F;,&amp;#x2F;&amp;#x2F;g&amp;#x27; | sed &amp;#x27;s&amp;#x2F;\(....\)&amp;#x2F;\1\t&amp;#x2F;g&amp;#x27; | head -n 5
&lt;/code&gt;&lt;/pre&gt;
Each step of the way you see the output, and every additional pipe modifies the last seen output.&lt;p&gt;Everything else is just being aware of what tools you can use (sed&amp;#x2F;awk&amp;#x2F;grep&amp;#x2F;xargs&amp;#x2F;etc) and the limits of the data you work with.&lt;p&gt;GP may have done something like popping the ruby repl, &lt;i&gt;irb&lt;/i&gt;, and then:&lt;p&gt;&lt;pre&gt;&lt;code&gt;  File.read(&amp;#x27;input6.txt&amp;#x27;).chars
  File.read(&amp;#x27;input6.txt&amp;#x27;).chars.each_cons(4).to_a
  File.read(&amp;#x27;input6.txt&amp;#x27;).chars.each_cons(4).find_index { _1.uniq == _1 } + 4&lt;/code&gt;&lt;/pre&gt;</text></comment>
<story><title>Ruby delights built into the language</title><url>https://technology.doximity.com/articles/ruby-delights-built-into-the-language</url></story><parent_chain><item><author>tra3</author><text>I struggle with code like this (also, complicated piped shell statements). How do you debug&amp;#x2F;reason about this? I know this specific example may be an exercise, but still.</text></item><item><author>phoronixrly</author><text>This article is kind of niche... My favourite Ruby delights are among others, methods provided by the Enumerable mixin &lt;a href=&quot;https:&amp;#x2F;&amp;#x2F;rubyapi.org&amp;#x2F;3.1&amp;#x2F;o&amp;#x2F;enumerable&quot; rel=&quot;nofollow&quot;&gt;https:&amp;#x2F;&amp;#x2F;rubyapi.org&amp;#x2F;3.1&amp;#x2F;o&amp;#x2F;enumerable&lt;/a&gt;. There&amp;#x27;s a pile of functionality there that helps with tedious tasks. E.g. here&amp;#x27;s a solution to this year&amp;#x27;s AOC challenge #6 using Enumerable&amp;#x27;s each_cons method:&lt;p&gt;&lt;pre&gt;&lt;code&gt;    File.read(&amp;#x27;input6.txt&amp;#x27;).chars.each_cons(4).find_index { _1.uniq == _1 } + 4&lt;/code&gt;&lt;/pre&gt;</text></item></parent_chain><comment><author>n0tth3dro1ds</author><text>&amp;gt; How do you debug&amp;#x2F;reason about this?&lt;p&gt;Debug? You don’t. You have to break your sexy chained-functional-style 1-liner into a “boring” multi-line loop to actually set meaningful breakpoints and work through any problems that may arise. Which is why I dislike this style of code — once something goes wrong, it’s WAY more cumbersome to debug and almost always makes you “unroll” it into its boring, “classic” form. And, of course, once you do that, you’re now debugging something DIFFERENT than what’s shipping in production! And you have to be extra careful to ensure that all of the logic has been kept the same, lest you ship a patch that doesn’t actually fix the bug! This is my prototypical “what programming is NEVER about” example: programming is never about how pretty the code is. Programming is about shipping features, and then being able to diagnose and fix problems with what you shipped.</text></comment>