Talk Like A Duck : You Can't Judge a Book... Some Mental Traps in Learning Ruby /2007/10/17/you-cant-judge-a-book-some-mental-traps-in-learning-ruby?format=rss en-us 40 In Ruby, it's not the dog, it's the tricks! Comment on You Can't Judge a Book... Some Mental Traps in Learning Ruby by labria <p>I do use the Person[] in the rails console, through .irbrc config, but I surely would not use it in the app itself. When someone looks through my code, he expects Name[] to be an constant array, defined somewhere else (I would!). I wouldn&#8217;t like to confuse people to save a bit of typing. Name.find() is 4 symbols longer, but much more readable.</p> Wed, 17 Oct 2007 18:17:51 -0400 urn:uuid:3c31e446-c5bb-4641-b338-3cc0aa3d2dfe http://talklikeaduck.denhaven2.com/2007/10/17/you-cant-judge-a-book-some-mental-traps-in-learning-ruby#comment-1432 Comment on You Can't Judge a Book... Some Mental Traps in Learning Ruby by Jeremy <p>The object that it&#8217;s too similar to array is sort of silly. You could just as easily have a Hash that has a numeric key&#8230;</p> Thu, 18 Oct 2007 15:56:29 -0400 urn:uuid:184a379d-b379-4e59-9b98-fac7ef306f8f http://talklikeaduck.denhaven2.com/2007/10/17/you-cant-judge-a-book-some-mental-traps-in-learning-ruby#comment-1461 Comment on You Can't Judge a Book... Some Mental Traps in Learning Ruby by Bob <p>&#8220;just because 99% of the time when you see something &#8220;indexed&#8221; by an integer, that thing doesn&#8217;t have to be an Array.&#8221;</p> <p>And just because 99% of the time turning the ignition switch in your car starts it, doesn&#8217;t mean you can&#8217;t have it turn on the radio every once in awhile.</p> <p>There is something to be said for clarity and consistency in an interface. Just because you can do it, doesn&#8217;t mean that you should.</p> Thu, 18 Oct 2007 17:26:11 -0400 urn:uuid:5e0c9993-a72e-481f-8f0b-6afb352cdf79 http://talklikeaduck.denhaven2.com/2007/10/17/you-cant-judge-a-book-some-mental-traps-in-learning-ruby#comment-1467 Comment on You Can't Judge a Book... Some Mental Traps in Learning Ruby by Pat Maddox <p>Another brief example:</p> <p>foo = lambda {|a| a.succ}</p> <p>foo[&#8220;c&#8221;] => &#8220;d&#8221;</p> Fri, 19 Oct 2007 04:39:44 -0400 urn:uuid:88fa68d3-d0dd-465f-b582-6a3381b2545c http://talklikeaduck.denhaven2.com/2007/10/17/you-cant-judge-a-book-some-mental-traps-in-learning-ruby#comment-1487 Comment on You Can't Judge a Book... Some Mental Traps in Learning Ruby by Piers Cawley <p>Personally make @Model#[]@ be something that would make @Model[params[:model_id]]@ return either the specified model or throw a RecordNotfound exception if no such model exists. </p> <p>Most of the time that&#8217;s going to be the same as find, but in cases like @/songs/my-sharona@, not so much.</p> <p>But then, I loathe URLs that expose primary keys.</p> Fri, 19 Oct 2007 06:30:06 -0400 urn:uuid:5cc06a7c-bed9-4817-9bc8-32d572535df3 http://talklikeaduck.denhaven2.com/2007/10/17/you-cant-judge-a-book-some-mental-traps-in-learning-ruby#comment-1490 Comment on You Can't Judge a Book... Some Mental Traps in Learning Ruby by Ryan Bates <p>I think you lose much of the convenience of the square brackets in a real app because you&#8217;re rarely accessing the id direcly. Instead it&#8217;s more like:</p> <p><code><pre>Person[params[:id]]</pre></code></p> <p>Nested square brackets, ick. I prefer the original way:</p> <p><code><pre>Person.find(params[:id])</pre></code></p> <p>Also, making it easier to search by id directly like this might encourage it more. So you&#8217;d see more code like this:</p> <p><code><pre> Person[project.person_id] </pre></code></p> <p>Instead of this:</p> <p><code><pre> project.person </pre></code></p> <p>Just a thought.</p> Fri, 19 Oct 2007 11:19:07 -0400 urn:uuid:0f9bc090-9293-4703-a3ae-26953c2e5406 http://talklikeaduck.denhaven2.com/2007/10/17/you-cant-judge-a-book-some-mental-traps-in-learning-ruby#comment-1493 Comment on You Can't Judge a Book... Some Mental Traps in Learning Ruby by jmoiron <p>I think that there are things that are useful as a convenience (like $() from prototype) that, because of their ubiquity and their vastly improved readability, can bend the rules a bit.</p> <p>I strongly believe that abuse of the indexed/associative accessors rarely fall into this category.</p> <p>I think it&#8217;s important for an API in particular not to reveal half-done abstractions for normal use. As a shortcut for an interactive session; by all means. But if you are implementing an index accessor for use inside program text, then your object better mimic an array to the fullest extent possible; it better have a length, iteration, etc.</p> <p>If it doesn&#8217;t, then making your object <em>look</em> like an array is misleading, and you introduce more complexity by re-using that syntax than was originally present: your own example proves it. </p> <pre> Person.find(1) </pre> <p>What&#8217;s a person?</p> <pre> Person[1] </pre> <p>Now what&#8217;s a person?</p> <p>A reader of your program (or the writers using your API) will have to think &#8220;Oh, is @a[10]@ a list or an ActiveRecord object?&#8221; every time that indexed accessor is used, and it isn&#8217;t always obvious (because it isn&#8217;t really always important). If I am implementing methods like these, I usually stop and think: &#8220;Am I going to be able to treat this like the type it <em>looks</em> like without having to think about it&#8217;s implementation?&#8221; If the answer is no, I don&#8217;t do it. </p> <p>Writing an interface to functionality that doesn&#8217;t require you to think about the implementation of that interface when you use it is <em>real</em> expressiveness. Anything else is just &#8220;sugar abuse&#8221;.</p> Sat, 20 Oct 2007 02:03:59 -0400 urn:uuid:7be2b335-3875-4bf3-b630-f480e3e45ca7 http://talklikeaduck.denhaven2.com/2007/10/17/you-cant-judge-a-book-some-mental-traps-in-learning-ruby#comment-1517