{"id":353,"date":"2007-08-14T14:31:21","date_gmt":"2007-08-14T18:31:21","guid":{"rendered":"http:\/\/www.dr-chuck.com\/wordpress\/?p=353"},"modified":"2011-12-17T12:23:53","modified_gmt":"2011-12-17T16:23:53","slug":"ruby-first-almost-disappointment","status":"publish","type":"post","link":"https:\/\/www.dr-chuck.com\/csev-blog\/2007\/08\/ruby-first-almost-disappointment\/","title":{"rendered":"Ruby &#8211; First (almost) disappointment"},"content":{"rendered":"<p>When you are learning a new language, when something is different often the first reaction is that it is bad &#8211; after all we fear change.<br \/>\nAs I was learning about Ruby objects, I was learning that all members variables are private and that the only way to get at the variables was through setters and getters.  I got used to setters and getters when I went from C++ to Java &#8211; it has always bugged me that I had to add 10 lines of code and 8 lines of comments just to access a dang member variable the &#8220;cool Java&#8221; way.<br \/>\nWith one of the tenents of Ruby is to dispense with unnecessary cruft &#8211; &#8220;convention versus configuration&#8221;  I was surprised when all the books immediately introduce setters and getters right away.  I would have thought that there would be a simple way to do this &#8211; code like this bothered me:<br \/>\nclass Thermo<br \/>\ndef set_temp(x)  # a setter<br \/>\n@temp=x<br \/>\nend<br \/>\ndef get_temp  # a getter<br \/>\n@temp<br \/>\nend<br \/>\nend<br \/>\nAnd they described this in books as if this was a *good thing*.   To me it was crap and too much &#8220;me-tooing&#8221; Java patterns &#8211; it was the absolute antithesis of &#8220;convention not configuration&#8221; &#8211; it was overblown syntax &#8211; something I hoped to get away from in Ruby &#8211; but here in Chapter 3 &#8211; was creeping cruft &#8211; when would it end?  And with this crufty syntax needed for *every* member variable &#8211; as far as I was concerned &#8211; Ruby was already a failure at delivering on &#8220;convention not overblown wasteful, repetitive, and redundant syntax&#8221;.  Grr. I was only a few weeks into the new language.<br \/>\nSo I started reading my five Ruby books and looking in the indexs and searching for things like &#8220;ruby sucks&#8221; and &#8220;ruby setters and getters suck&#8221; in Google and trying to learn if there were others as peeved about this as me.  I found some peeved folks &#8211; and even some folks peeved about this issue.   But somewhere along the line I got a clue.<br \/>\nActually I had a clue all along &#8211; ActiveRecord does *not* use the setter\/getter pattern faux-Java style &#8211; and since ActiveRecord is written in Ruby &#8211; it must be doing something much niftier than faux-Java setters and getters.  You just refer to<br \/>\nthermo.id<br \/>\nNot<br \/>\nthermo.get_id   thermo.set_id(1234)<br \/>\nTo get the Id of an active record object &#8211; I liked that &#8211; how do they do that?  So the hunt was on.<br \/>\n<I>By the way there needs to be a newbie list where people like me can ask dumb questions like this and get a boot to the head to quickly learn this stuff.  Like dummies@ruby.org or some such. <\/i><br \/>\nSo I was sure there was a better idea &#8211; there was hope.  I will bypass all the pages that I waded through until I found this one.<br \/>\nhttp:\/\/www.ruby-doc.org\/docs\/UsersGuide\/rg\/accessors.html<br \/>\nIt shows two patterns of setters that make a LOT of sense &#8211; far more sense than the drivel above.  I will switch to his code here:<br \/>\nclass Fruit<br \/>\ndef kind=(k)<br \/>\n@kind = k<br \/>\nend<br \/>\ndef kind<br \/>\n@kind<br \/>\nend<br \/>\nend<br \/>\nf2 = Fruit.new<br \/>\nf2.kind = &#8220;apple&#8221;<br \/>\nf2.kind<br \/>\nNow that is a sweet syntax &#8211; particularly when accessing and setting the member variable.  Kind of like C++ again &#8211; operator overloading &#8211; a nice clean syntax that allows me to do things &#8220;in context&#8221; &#8211; working with the compiler as my pal rather than pounding it with syntax as in Java (sorry &#8211; still a little bitter about the loss of operator overloading when I left C++ five years ago).<br \/>\nOf course &#8211; Ruby does this one better below.<br \/>\nThis *is* &#8220;convention over configuration&#8221; &#8211; or at least &#8220;don&#8217;t repeat yourself&#8221;.  &#8220;Don&#8217;t repeat yourself&#8221; is a very important concept in programming as it keeps us from making transcription mistakes.  So the super-sweet Ruby-regains-absolute-advantage syntax is as follows:<br \/>\nclass Fruit<br \/>\nattr_accessor :kind<br \/>\nend<br \/>\nf2 = Fruit.new<br \/>\nf2.kind = &#8220;apple&#8221;<br \/>\nf2.kind<br \/>\nAh &#8211; I am back happy with Ruby again &#8211; no wasted moves &#8211; all graceful &#8211; like a ballet dancer &#8211; I can express what I want in a concise syntax that clearly communicates my intent to the future reader of the code.   And if I want to add a little &#8220;special stuff&#8221; to my setter &#8211; simply drop back to the attr_reader and do my own setter as in the previous example.<br \/>\nWhew!   I was kind of grouchy with Ruby for a few hours &#8211; but I am feeling better now &#8211; and in addition the Ruby approach is am improvement beyond C++ and Java IMHO &#8211; simple syntax for simple situations with the option to go deeper using more syntax only when there is a real need.<br \/>\nAlso I got a little back on the operator overloading front&#8230;   Cha-Ching.<br \/>\nLet me know if there is a mailing list to help me get through these mental blocks more easily.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>When you are learning a new language, when something is different often the first reaction is that it is bad &#8211; after all we fear change. As I was learning about Ruby objects, I was learning that all members variables are private and that the only way to get at the variables was through setters [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-353","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/www.dr-chuck.com\/csev-blog\/wp-json\/wp\/v2\/posts\/353","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.dr-chuck.com\/csev-blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.dr-chuck.com\/csev-blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.dr-chuck.com\/csev-blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.dr-chuck.com\/csev-blog\/wp-json\/wp\/v2\/comments?post=353"}],"version-history":[{"count":1,"href":"https:\/\/www.dr-chuck.com\/csev-blog\/wp-json\/wp\/v2\/posts\/353\/revisions"}],"predecessor-version":[{"id":2464,"href":"https:\/\/www.dr-chuck.com\/csev-blog\/wp-json\/wp\/v2\/posts\/353\/revisions\/2464"}],"wp:attachment":[{"href":"https:\/\/www.dr-chuck.com\/csev-blog\/wp-json\/wp\/v2\/media?parent=353"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.dr-chuck.com\/csev-blog\/wp-json\/wp\/v2\/categories?post=353"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.dr-chuck.com\/csev-blog\/wp-json\/wp\/v2\/tags?post=353"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}