{"id":2672,"date":"2010-01-12T22:31:01","date_gmt":"2010-01-13T02:31:01","guid":{"rendered":"http:\/\/blogs.n1zyy.com\/n1zyy\/?p=2672"},"modified":"2010-01-12T22:31:01","modified_gmt":"2010-01-13T02:31:01","slug":"nitpicking-computers","status":"publish","type":"post","link":"https:\/\/blogs.n1zyy.com\/n1zyy\/2010\/01\/12\/nitpicking-computers\/","title":{"rendered":"Nitpicking Computers"},"content":{"rendered":"<p>One thing I like about Ruby is that it&#8217;s <em>usually<\/em> less nitpicky than some other languages I&#8217;ve used. Sometimes tiny little differences with computers are really important, and people programming should really have a mastery of those things. But sometimes the computer is just being petty.<\/p>\n<p>Here&#8217;s one that ticks me off every time, though. Let&#8217;s say I do something really simple, like look up all the rows matching a simple criteria. I then call .each on the result, to iterate over them.<\/p>\n<p>But there are actually <strong>two<\/strong> pet peeves in one here:<\/p>\n<ul>\n    <li>If you get back no results, you often end up with the variable being nil. And calling &#8220;nil.each&#8221; blows apart. Sometimes this makes sense and you really need to handle nil results specially. But very often, I couldn&#8217;t care less. I said to look something up and, <em>for every one you found<\/em>, do something with it. That works if you found 0 results, too: you do nothing. If I asked you to go through your refrigerator and, for every blue apple you found, put it on the table, and there were no blue apples in your refrigerator, I&#8217;d expect you to shrug your shoulders, put no blue apples on the counter, and briskly walk away thinking I was kind of weird. I wouldn&#8217;t expect you to keel over and die because there were no blue apples.<\/li>\n    <li>If you get <em>one<\/em> result back, it&#8217;s not always an array. Sometimes it is, and .find seems to give you an array like a good citizen. But I <em>very<\/em> often have to write dumb code that checks to see if what&#8217;s coming in is an array and, if not, puts it inside a new one-item array. This is dumb. Really dumb. (And you <em>used<\/em> to be able to blindly cast anything to an array, and Array.to_a was smart enough to not end up with an array inside an array. But that&#8217;s been deprecated.)<\/li>\n<\/ul>\n<p>I&#8217;m sure the purists will argue that there&#8217;s some magical way that&#8217;s The Right Way, or explain why adding a no-op method to nil is bad and why Base.each would be unthinkably bad. But &#8220;take a group of things and iterate over each thing&#8221; is one of the most basic things you can do in any language, and it&#8217;s fantastically dumb for me to have to handle &#8220;special cases&#8221; of there being 0 or 1 things.<\/p>","protected":false},"excerpt":{"rendered":"<p>One thing I like about Ruby is that it&#8217;s usually less nitpicky than some other languages I&#8217;ve used. Sometimes tiny little differences with computers are really important, and people programming should really have a mastery of those things. But sometimes &hellip; <a href=\"https:\/\/blogs.n1zyy.com\/n1zyy\/2010\/01\/12\/nitpicking-computers\/\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-2672","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/blogs.n1zyy.com\/n1zyy\/wp-json\/wp\/v2\/posts\/2672","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blogs.n1zyy.com\/n1zyy\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blogs.n1zyy.com\/n1zyy\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blogs.n1zyy.com\/n1zyy\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blogs.n1zyy.com\/n1zyy\/wp-json\/wp\/v2\/comments?post=2672"}],"version-history":[{"count":0,"href":"https:\/\/blogs.n1zyy.com\/n1zyy\/wp-json\/wp\/v2\/posts\/2672\/revisions"}],"wp:attachment":[{"href":"https:\/\/blogs.n1zyy.com\/n1zyy\/wp-json\/wp\/v2\/media?parent=2672"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blogs.n1zyy.com\/n1zyy\/wp-json\/wp\/v2\/categories?post=2672"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blogs.n1zyy.com\/n1zyy\/wp-json\/wp\/v2\/tags?post=2672"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}