DABlog - Home tag:dablog.rubypal.com,2009:mephisto/ Mephisto Noh-Varr 2009-04-21T23:16:10Z dblack tag:dablog.rubypal.com,2009-04-21:413 2009-04-21T23:15:00Z 2009-04-21T23:16:10Z "The Well-Grounded Rubyist" now available in PDF! <p>It’s been a busy few days, with the release of not <a href="http://dablog.rubypal.com/2009/4/21/envycasts-featuring-david-a-black-ruby-1-9-what-you-need-to-know">my Ruby 1.9 Envycasts</a> but also the <span class="caps">PDF</span> version of my new book <a href="http://manning.com/black2">The Well-Grounded Rubyist</a>.</p> <p><span class="caps">TWGR</span> is an expanded, updated, Ruby-only reworking of my 2006 book “Ruby for Rails”. It targets Ruby 1.9.1, and includes a great deal of new material (enough that it took me almost a year longer than I thought it would to write :-) The book is entirely about the Ruby language, not Rails. Lots of readers of <span class="caps">R4R</span> encouraged me to write a “just-Ruby” book, and here it is!</p> <p>I’m looking forward to the release of the paper version on May 1, too. Not sure yet whether there are Kindle and/or Sony e-reader versions coming, but I’ll keep you posted.</p> dblack tag:dablog.rubypal.com,2009-03-21:395 2009-03-21T01:22:00Z 2009-03-21T01:23:32Z Is this an early use of the slang "cool"? <p>Here’s a passage from <i>The Man in Lower Ten</i> by Mary Roberts Rinehart, published in 1906. I’ve included some context but the main thing I’m interested in is the appearance of the word “cool” in the second paragraph.</p> <blockquote> “Nonsense,” he said. “Bring yourself. The lady that keeps my boarding-house is calling to me to insist. You remember Dorothy, don’t you, Dorothy Browne? She says unless you have lost your figure you can wear my clothes all right. All you need here is a bathing suit for daytime and a dinner coat for evening.” <br /> <br /> “It sounds cool,” I temporized. “If you are sure I won’t put you out—very well, Sam, since you and your wife are good enough. I have a couple of days free. Give my love to Dorothy until I can do it myself.” </blockquote> <p>I can’t see what “cool” means in the second paragraph, other than “cool” in the slang sense that we use it. My understanding is that “cool” in that sense started, or at least came into common usage, during or after World War II. In any case, 1906 seems insanely early for it.</p> <p>But what else could it mean in the quotation above? The wardrobe described in the first paragraph doesn’t suggest a particularly cool climate. Is there some other nuance of the word I’m not getting?</p> <p>I shall leave comments open on this one, at least until the spam gets intolerable.</p> dblack tag:dablog.rubypal.com,2009-03-14:392 2009-03-14T14:21:00Z 2009-03-14T14:22:23Z Did I mention Ruby training in Atlanta, April 1-3? <p>The answer is…yes! I did mention it. But I’ll mention it again.</p> <h2>Want to learn Ruby, and learn it right?</h2> <p>Come to Atlanta for three days and learn Ruby from:</p> <ul> <li>me (author of <a href="http://www.manning.com/black"><em>Ruby for Rails</em></a>, <a href="http://www.manning.com/black2"><em>The Well-Grounded Rubyist</em></a>, and other stuff; long-time Ruby programmer; one of the most experienced Ruby trainers on the planet)</li> <li>Jeremy McAnally (“mrneighborly”, author of <a href="http://www.manning.com/mcanally"><em>Ruby in Practice</em></a>, creator of the Ruby Hoedown (annual conference))</li> <li>Rick Olson (“technoweenie”, member of the Rails core team; plugin writer extraordinaire)</li> </ul> <h2>You gotta better way to learn Ruby?</h2> <p>I doubt it. Just read that list of instructors again… and you get training materials, a book (“Ruby in Practice”), and lunches.</p> <p>There’s registration info <a href="http://www.entp.com/training/atlanta09">here</a>, and you can <a href="mailto:dblack@rubypal.com">contact me directly</a> with any questions.</p> <p>Hope to see you there!</p> <p>P.S. If you’re a Ruby expert but have friends or co-workers or employees who could use an accelerated intro/intermediate course, send them along!</p> dblack tag:dablog.rubypal.com,2009-02-16:385 2009-02-16T14:50:00Z 2009-02-16T14:52:25Z Ruby training in Atlanta, April 1-3! <p>Want to learn Ruby, or improve what you already know? Come to Atlanta!</p> <p><a href="http://www.rubypal.com">Ruby Power and Light</a> and <a href="http://www.entp.com"><span class="caps">ENTP</span></a> are teaming up to present a three-day Ruby course in Atlanta. You can get more info, and register, <a href="http://www.entp.com/training/atlanta09">here</a>.</p> <p>Training will be by me and Ruby developer/author Jeremy McAnally (“mrneighborly”). And Rick Olson (“technoweenie”) will be there too, helping with the training and sagely dispensing Ruby wisdom and advice. (Seriously!) It will be at the Georgia Tech Hotel & Conference Center.</p> <p>Please <a href="mailto:dblack@rubypal.com">email me</a> if you have any questions. Otherwise, see you there!</p> dblack tag:dablog.rubypal.com,2009-02-03:381 2009-02-03T01:38:00Z 2009-02-03T01:38:44Z Why athletes thanking God for victories is stupid <p>I hate it when athletes thank God when they win. My reasons for hating it have nothing to do with my own atheism. I hate it because it’s narcissistic and because it’s theologically infantile.</p> <p>If you win a game and then thank God, <em>and do not thank God when you lose</em>, you are going on record as believing that God wanted you to win, and that a victory by your opponent would have represented a thwarting of God’s plan.</p> <p>But how do you know? Isn’t it possible that losing is what God has planned for you, and that it will do you good? Maybe losing will strengthen your character. Maybe your opponent needs the win (or the prize money) more than you do, and God somehow managed to figure that out in spite of being dazzled by your greatness. Maybe you should be thanking God for protecting you from the sin of pride by not letting you win a spiritually meaningless, entirely earthly contest.</p> <p>But I’ve never seen an athlete drop to his or her knees and thank God after a loss. Why not? Because the ones who thank God when they win have a dinky, anthropomorphic conception of God. Their God is “the man upstairs,” the Santa Claus figure, the parent who may or may not give them the birthday present they want. And to hell with the other kids. Me, Me, Me.</p> <p>So what gives? Where does this all come from? Whose big idea was it to thank God only for bringing about what they themselves wanted to happen anyway?</p> <p>Let’s go back to ancient times. Things were different with respect to thanking gods, <em>because there were lots of gods</em> and the gods took sides in the contest. It made sense for the Greeks to thank Athena for the victory over the Trojans because Athena was, at some Olympian level, duking it out with Ares and Aphrodite. The Greeks’ powerful friends prevailed over the Trojans’ powerful friends. And the Greeks understood that someone had actually made an effort on their behalf, faced uncertainty, and prevailed. So they thanked her.</p> <p>Dear athlete: Do you think that God faces uncertainty when you play a tennis match?</p> <p>Do you think that God has to make an effort on your behalf to make sure you win?</p> <p>Do you think that God’s enemy is rooting for your opponent?</p> <p>And if you don’t think all that, what exactly are you thanking God for when you win? I mean <em>exactly</em>. Not just vaguely that you’re happy, and happiness feels good, so it must come from God. That’s theological babytalk.</p> <p>The best thing that can be said about thanking God for an athletic victory and not for a loss is that it’s an ignorant corruption of what was a perfectly reasonable pagan practice. If you’re a monotheist and thank God for a win, you’re making a statement about your own inherent worth, and what you believe is God’s opinion of that worth, in comparison to the inherent worth of your opponent. You’re asserting that your victory is of the Lord to an extent that a victory by your opponent would not have been. And you’re implying unmistakeably that your opponent is in league with God’s enemy.</p> <p>In other words, thanking God for an athletic victory is stupid, uninformed, thoughtless, self-absorbed, and about as far from anything religious or spiritual as you can get. I understand the whole thing about religion not being the same as rational thought. But this isn’t even the same as religious thought. It’s just vanity.</p> dblack tag:dablog.rubypal.com,2009-01-23:371 2009-01-23T22:31:00Z 2009-01-23T22:32:07Z RailsConf registration (and a hiatus year for RailsConf Europe) <p>Registration is now open for RailsConf 2009 (May 4-7). You can get more details, and register, at <a href="http://www.railsconf.com">the RailsConf 2009 website</a>.</p> <p>RailsConf is taking place in Las Vegas, one of my favorite cities. Yes, I know what a weird and ironic place it is. But for whatever reason, I’ve always found it extremely enjoyable. May is a good time to go&mdash;hopefully not to hot to step outside!</p> <p>There’s a lot going on at RailsConf this year, highlighted by its timing in the wake of the Rails/Merb merger decision. There will be lots of merger news and highlights, along with the usual great lineup of talks and, above all, the chance to meet and get to know other Rails developers as well as Rails core team members, authors, bloggers, and pretty much the whole gang!</p> <h2>A hiatus year for RailsConf Europe</h2> <p>Ruby Central and O’Reilly have decided to take a hiatus from producing RailsConf Europe this year, for the simple reason that it didn’t bring in enough revenue last year to justify doing it again, particularly given the tight economy and the need to err on the side of caution. RailsConf Europe has always been a really great event, and people who go to it really love it, but we need a year of retrenchment while we figure out how to get everyone else to realize how great it is! Plans for 2010 are not certain yet; we’re taking it one year at a time.</p> <p>Meanwhile, the Ruby and Rails communities continue to produce an astonishing number of high-quality, uniquely branded and flavored events. I’m not even going to try to list them all here. Do a search, though, and you may very well find one near you.</p> dblack tag:dablog.rubypal.com,2009-01-16:362 2009-01-16T14:20:00Z 2009-01-16T14:20:36Z Son of 10 things to be aware of in Ruby 1.9! <p>I’m happy to see that my recent <a href="http://dablog.rubypal.com/2009/1/14/10-things-to-be-aware-of-in-moving-to-ruby-1-9">10 things to be aware of in moving to Ruby 19</a> article has proven helpful to lots of people. This article is a follow-up.</p> <p>The goal of the article was to point out 1.9 features and changes that might cause your existing code not to run correctly, or not to run at all. I went a bit soft, though: two of the original ten (hashes being ordered and the changes in method-argument syntax) weren’t really things that might break your 1.8 code.</p> <p>So I feel I owe the world two more code-breaking 1.9 features! And they’re here, along with a bonus one.</p> <h2>But first, some links</h2> <p>The denizens of ruby-talk have provided lots of helpful ideas and feedback. James Edward Gray II and others mentioned <span class="caps">M17N</span>, a topic on which I defer to the more expert among us, especially James who has written a multi-part <a href="http://blog.grayproductions.net/articles/understanding_m17n"><span class="caps">M17N</span> guide</a>. He’s going to be expanding it to include 1.9 encoding, so keep an eye on it.</p> <p>Brian Candler suggested that people might be interested in the presentation by me and Dave Thomas at RubyConf 2008 on <a href="http://rubyconf2008.confreaks.com/ruby-19-what-to-expect.html">Ruby 1.9: What to Expect</a>. We cover some pitfalls but also some new, non-pitfall features you might want to know about.</p> <p>If you’re interested in Ruby 1.9 generally, you might be interested in my forthcoming book <a href="http://www.manning.com/black2">The Well-Grounded Rubyist</a>, which is a fully revised, revamped, “Ruby only” second incarnation of my 2006 book <a href="http://www.manning.com/black">Ruby for Rails</a>.</p> <p>Apologies to anyone I’ve failed to credit, and thanks to all for the feedback.</p> <p>And with that, here are the pitfalls! (Speaking of pitfalls, I think I’ve remembered all the &lt;pre&gt; tags this time….)</p> <h2>String indexing behavior has changed</h2> <p>(Thanks to Michael Fellinger and Robert Dober)</p> <p>In Ruby 1.8, indexing strings with <code>[]</code>, as in <code>"string"[3]</code>, gives you an <span class="caps">ASCII</span> code:</p> <pre> &gt;&gt; "string"[3] =&gt; 105 </pre> <p>In order to get a one-character-long substring, you have to provide a length:</p> <pre> &gt;&gt; "string"[3,1] =&gt; "i" </pre> <p>In Ruby 1.9, the indexing operation gives you a character.</p> <pre> &gt;&gt; "string"[3] =&gt; "i" </pre> <p>Also, kind of along the same lines, the ?-notation now gives a character rather than a code. In 1.8:</p> <pre> &gt;&gt; ?a =&gt; 97 </pre> <p>and in 1.9:</p> <pre> &gt;&gt; ?a =&gt; "a" </pre> <h2>if-conditions can no longer end with a colon</h2> <p>In 1.8 you can do this:</p> <pre> if x: puts "Yes!" end </pre> <p>In 1.9, you can’t use that colon any more. The same is true of when clauses in case statements. This will not parse in 1.9:</p> <pre> case x when 1: "yes!" end </pre> <h2>Bonus thing! No more default to_a</h2> <p>In 1.9 you cannot assume that every object has a <code>to_a</code> method. You’ve probably seen warnings about this in 1.8, and the day of reckoning has now arrived.</p> <pre> &gt;&gt; "abc".to_a NoMethodError: undefined method `to_a' for "abc":String </pre> <p>You can use the Array method to turn anything into an array. If it’s an array already, it returns the object itself (not a copy). If it’s anything else, it tries to run <code>to_ary</code> and <code>to_a</code> on it (in that order), and if those aren’t available, it just wraps it in an array.</p> <p>Array isn’t new, but we’re likely to be using it a lot more now that there’s no default <code>to_a</code> operation.</p> <p>Have fun!</p> dblack tag:dablog.rubypal.com,2009-01-14:359 2009-01-14T19:21:00Z 2009-02-15T03:51:21Z 10 things to be aware of in moving to Ruby 1.9 <p><em>Update: There’s a sequel to this post, called <a href="http://dablog.rubypal.com/2009/1/16/son-of-10-things-to-be-aware-of-in-ruby-1-9">Son of 10 things…</a> </em></p> <p>I’ve been writing a lot about Ruby 1.9 (my book <a href="http://www.manning.com/black2">The Well-Grounded Rubyist</a> is due out in a couple of months), and I thought I’d share my personal list of things you need to be careful of as you go from 1.8 to 1.9. This is not a list of changes; it’s a list of changes that you really need to know about to get your 1.8 code to work in 1.9, things that have a relatively high likelihood of biting you if you don’t know about them.</p> <h3>Strings are no longer enumerable</h3> <p>You can’t do <code>string.each</code> and friends any more. This has an impact, for example, on the Rack interface, where there has in the past been a requirement that the third item in the returned array respond to <code>each</code>.</p> <h3>Block argument semantics</h3> <p>This is a big change, and a big topic. The salient point is that when you do this:</p> <pre> array.each {|x| ... } </pre> <p>the block parameter list is handled like a method parameter list. In 1.8, blocks use assignment semantics, so that <code>@ is like @x=</code>. That’s why in 1.8 you can do:</p> <pre> array.each {|@x| ... } </pre> <p>(assign to an instance variable) or even:</p> <pre> array.each {|self.attr| ... } </pre> <p>(call the <code>attr=</code> method on <code>self</code>). You can’t do those things in 1.9; the parameters are bound to the arguments using method-argument semantics, not assignment semantics.</p> <h3>Block variables scope</h3> <p>Block parameters are local to the block.</p> <pre> x = 1 [2,3].each {|x| } </pre> <p>In 1.8, <code>x</code> would now be 3 (outside the block). In 1.9 the two <code>x</code>’s are not the same variable, so the original <code>x</code> is still 1.</p> <p>However, a variable that (a) already exists, and (b) is not a block parameter, is not local to the block.</p> <pre> x = 1 [2,3].each {|y| x = y } </pre> <code>x</code> is now 3. If you want or need to shield your existing variables from being used inside the block, declare variables as block local by putting them after a semi-colon in the parameter list: <pre> x = 1 [2,3].each {|y;x| x = y } </pre> <p><code>x</code> is still 1.</p> <h3>Method argument semantics</h3> <p>Method arguments do some new things too. In particular, you can now put required arguments after the optional argument glob parameter:</p> <pre> def my_meth(a,*b,c) </pre> <p>There aren’t too many situations where you’d want to do this (though there are one or two).</p> <h3>The * operator has changed semantics</h3> Compare 1.8: <pre> &gt;&gt; a = [1,2] =&gt; [1, 2] &gt;&gt; *b = a =&gt; [[1, 2]] &gt;&gt; b =&gt; [[1, 2]] </pre> <p>and 1.9:</p> <pre> &gt;&gt; a = [1,2] =&gt; [1, 2] &gt;&gt; *b = a =&gt; [1, 2] &gt;&gt; b =&gt; [1, 2] </pre> <p>I’ve always interpreted the <code>*</code> operator in the following way:</p> <pre><code>The expression *x represents the contents of the array x, as a list.</code></pre> <p>In 1.8, <code>*b = [1,2]</code> means that <code>[1,2]</code> is the contents of the array <code>b</code>, which means that <code>b</code> is <code>[[1,2]]</code>. The 1.9 semantics don’t seem to behave that way. I’m not sure what the new general rule for <code>*</code> is, or whether maybe I was wrong that there was such a rule that governed all cases (though I can’t think of an exception).</p> <h3>Hashes are ordered</h3> <p>This isn’t likely to bite you but it’s something to be aware of, both in your own code and in looking at the code of others. Hashes are ordered by insertion order. Reassigning to a key does not change the insertion placement of that key.</p> <h3>method and friends return symbols</h3> <p>Expressions like <code>obj.methods</code> and <code>klass.instance_methods</code> return symbols instead of strings in 1.9. That means that you might have to do <code>to_s</code> operations on them, if you need them as strings. However…</p> <h3>Symbols are string-like</h3> <p>... symbols have become very string-like. You can match them against regular expressions, run methods like <code>#upcase</code> and <code>#swapcase</code> on them, and ask them their <code>size</code> (i.e., their size in characters). I’m not sure what the purpose of this is. I’d just as soon have symbols not be any more string-like than they absolutely have to be.</p> <h3>Gems are automatically in the load path</h3> <p>When you start Ruby (or irb), your load path (<code>$:</code>) will include the necessary directories for all the gems on your system. That means you can just require things, without having to require rubygems first. You can manipulate the load path per gem version with the gem method.</p> <h3>Lots of enumerable methods return enumerators</h3> <p>Called without a block, most enumerable methods now return an enumerator. It’s fairly unusual to use the return value of blockless calls to <code>map</code>, <code>select</code>, and others, but it’s worth knowing that now you cannot assume that, for example, <code>Array#each</code> will always return its receiver.</p> <p>You can use this feature to chain enumerators, though the circumstances in which chaining enumerators really buys you anything are pretty few. I don’t know of a case where you would do this:</p> <pre> array.map.other_method { ... } </pre> <p>with the exception of <code>map.with_index</code>. The <code>map</code> call is essentially a pass-through filter here. (This was not true in early versions of 1.9, where you could attach knowledge of a block to a chained enumerator, but that behavior was removed.)</p> <p>Incidentally, you win the prize (which is endless glory :-) if you can account for the difference between these two snippets:</p> <pre> &gt;&gt; {1 =&gt; 2}.select {|x,y| x } =&gt; {1=&gt;2} &gt;&gt; {1 =&gt; 2}.select.select {|x,y| x } =&gt; [[1, 2]] </pre> <p>It’s all about enumerators….</p> <p>If you’re careful about these changes, and keep an eye out for others, you should be able to continue to have fun with Ruby in version 1.9 and beyond!</p> dblack tag:dablog.rubypal.com,2008-12-29:337 2008-12-29T15:13:00Z 2008-12-29T15:14:38Z Cool wishlist management at WishSight! <p>Announcing the opening of <a href="http://www.wishsight.com">WishSight</a>!</p> <p>WishSight is for managing wishlists and gift-giving. It lets you see who’s given (or promised) what to whom, and it lets gift-givers for particular people communicate with each other, via a comment-board, so that they don’t duplicate gifts.</p> <p>It’s based on a Christmas-list application I wrote in 2005 that my family and friends have been using every year since then. It’s completely merchant-unaffiliated. You can post links for the gifts you want, and they can be links to any merchant.</p> <p>WishSight helps you cut down on gift duplication, and increases the chances that people will get things they actually want, without the gift-givers having to do a round-robin of email or phone calls to pin down who’s buying what. And chances are they don’t all know each other anyway&mdash;which doesn’t matter on WishSight, because you all communicate by leaving comments directly on your mutual friend’s wishlist.</p> <p>All you have to do is:</p> <ul> <li>sign up</li> <li>list the email addresses of people who you want to be able to see your wishlist</li> <li>get those people to sign up and “whitelist” your email address</li> <li>list your wishes</li> <li>stake “claims” on other people’s wishes</li> </ul> <p>There’s no stealth: the email addresses are only used internally to determine who’s allowed to see whose wishlist. Also, you can list email addresses even if the people haven’t signed up yet. Once they do sign up, they will automatically have permission to see your wishlist and claim your wishes. No two-sided “handshakes” required; you just whitelist people.</p> <p>Have fun, and let me know if any questions or problems!</p> dblack tag:dablog.rubypal.com,2008-12-21:320 2008-12-21T15:59:00Z 2008-12-21T16:23:03Z On the menu this season: Muslims and gays <p>Don’t get me wrong. I’m not saying that other forms of hate and prejudice are extinct, or even on the wane. But it feels like the stars anti-Muslim sentiment and homophobia are in the ascendancy.</p> <p>It’s very much about statements that don’t sound aggressive or hateful, on the surface, but that would never be made if hate didn’t lurk just below. I’m thinking, for example, of a report I heard on the radio of some attack or other, involving “three Muslims of middle-eastern descent.” I might have the phrasing of the “middle-eastern descent” part wrong (though it was that or close to it). In any case, the salient bit, for me, was “three Muslims.”</p> <p>When was the last time you heard a crime described as having been committed by “three Christians”? How about “A Jew broke into a convenience store…”? So what’s up with “three Muslims”?</p> <p>What’s up, of course, is hate. I don’t think the radio announcer or the newswriter hates Muslims. But they do operate under a compulsion to mention explicitly that Muslims are Muslims, and ultimately that’s so that the listenership can be put on alert to hate them. Does the phrase “three Muslims” have explanatory power? Did these people do whatever they did <i>because</i> they are Muslims? No. There’s no reason to mention their religion except out of habit of mentioning the fact that Muslims are Muslims.</p> <p>Back when I was a university professor (1992-2005; in this case somewhere around 2003, I think), the school newspaper had a kind of “person-in-the-street” feature, where they’d ask a few people around campus a question and print selected answers. One week, the question was something about Iraq. One of the people quoted in the feature said something along the lines of, “Bomb them all off the face of the earth.” Or “Blow them all up”&mdash;words to that effect.</p> <p>My response was to call the editor-in-chief of the newspaper into my office and have a little chat with him. I was under no institutional imperative to do so&mdash;I was not involved with the paper directly&mdash;but it seemed to me that I had an opportunity to teach him perhaps the most important lesson of his college career. “If the question of the week had been about how to improve the cafeteria food,” I asked him, “and someone had said, ‘Line the whole cafeteria staff against the wall and shoot them dead,’ would you have printed it?”</p> <p>Of course he would not have, and said that he would not have. “The fact that what we would not say about the cafeteria workers, we <i>would</i> say about the entire population of a Muslim country,” I explained, “is the dehumanization process at work.” I do believe he understood and took my point on board.</p> <p>So we mention that people are Muslims, and we lower the bar when it comes to suggesting (or, if you like, joking about) their violent deaths. And it’s all very dangerous and should be sending up serious alarms.</p> <p>Labeling the gay as gay is an even more popular pastime. The world has settled for a breathtakingly stunted view of what homosexuality entails, and how it manifests itself. It manifests itself, by the way, as itself, not as an obsession with the song “YMCA” or an expertise in designer footware. Hey, more power to you if you have that expertise. But the set of all men who do intersects in a miniscule subset with the set of all men whose primary sexual orientation is toward men. Ditto for all the stereotypes.</p> <p>Of course, the world can’t deal with the idea that homosexuality manifests itself only as itself, because if that’s true, it means you can’t tell who’s gay; and that, like being unable to tell who’s Jewish, is unacceptable. The workaround is to <i>pretend</i> that you can tell who’s gay, resorting to babytalk about your “gaydar” when the stereotypes, as they must, fail you.</p> <p>And then, following a fairly tight train of thought, there’s hatred of gays.</p> <p>First of all, let me explain that I include, as hatred, the “love the sinner, hate the sin” horseshit espoused by the Catholic church. It is, to be sure, a kinder, gentler hatred than the burning-at-the-stake kind. The idea is that you’re enlightened enough to acknowledge that some people just are gay. But you also understand that, as gays, they must never indulge in the kinds of sexual activities they feel interested in. So you, as the compassionate believer, offer to contribute to their happiness by giving them support and encouragement as they fight to maintain their chastity.</p> <p>How noble.</p> <p>The church, of course, has two thousand years of experience disguising hate as love. But this one is particularly devious and malign. Let’s cut to the chase: the only reason that one adult human being would try to stop another adult human being, on a lifelong basis, from attaining romantic and/or erotic satisfaction is that he or she (human one) <i>hates</i> him or her (human two). No amount of theological stroking can change that. It’s hate.</p> <p>Not news, of course, that the Pope and friends hate gays. But interesting to see how slimy and prurient they can get, in the process. Anyway, let’s move on.</p> <p>Actually we can borrow a concept from the church: “invincible ignorance.” When I read the stuff about homosexuality being a choice (note that it’s not that sexual preference is a choice, just homosexuality&mdash;which makes it kind of weird to describe it as a choice), my reaction is that if you put twenty articulate, knowledgeable people in a room for twenty years with the person who’s taking the “choice” position, that person would emerge still saying that homosexuality is a choice. There’s no point of entry for explanation, and no point of contact with reality.</p> <p>It’s pathetic, but I still count it as hate. At least it leads to hate. Or from hate, perhaps. Or maybe these people are actually choosing to be vicious, and could stop themselves if they really wanted to. It’s hard to know. They’re not saying.</p> <p>With gay marriage on the news radar these days, more and more of this kind of discourse is showing up: the choice thing, but also the “gays recruit people” thing (which is actually backwards; have these people ever watched television commercials?) and, most disturbingly of all, the “gays prey on children” thing. And each of these things embodies two problems: first, that people believe it; and second, that it’s acceptable to say it publicly.</p> <p>Which hateful statements are acceptable and which aren’t is a kind of lump under the carpet that moves around but never goes away. Unfortunately, the underlying hate never goes away either&mdash;and ultimately, no matter which targeted people or groups we’re talking about, it’s the underlying hate that matters. But who gets to say what, and when, and with what consequences (or lack thereof) is, in itself, something that I think it’s worth keeping fairly close tabs on.</p> dblack tag:dablog.rubypal.com,2008-11-30:282 2008-11-30T13:05:00Z 2008-11-30T13:26:09Z Probative Programming: the physical unification of code and tests <p>I’m encouraged by a couple of recent conversations to go public with this possible wacky idea. It has to do with code and testing.</p> <p>I’ll start with the idea, and then say something about why I’m thinking along these lines.</p> <p>The idea is for a programming system designed in such a way that the code and its tests are physically together, in one file. Furthermore, that file is not executable. You have to run it through a dedicated filter utility to generate the actual code file(s) from it.</p> <p>So it’s a bit like, and indeed inspired by, Knuth’s Literate Programming, where the code and its documentation are fused together in a single file which contains both but is, itself, neither. You can’t execute that file; you have to generate the real code files from it.</p> <p>Adapting the master-file idea to testing, as I envision it, would also entail the following constraint: that the system <em>would refuse to generate the code files</em> unless the code involved already had tests, and those tests passed. In other words, the whole system would militate against using untested code in production, by physically obstructing the creation of executable code files for untested stretches of code.</p> <p>It seems to me that this would make for a much more sensible and efficient flow of energy than what we’ve got now. What we’ve got now are separate files, and therefore the <em>possibility</em> of running untested code. As long as that possibility exists, people will run untested code. Reordering things so that the creation of the executable code comes <em>after</em> the successful test run would, potentially, realign the energy of the whole process in a very productive way.</p> <p>As things stand now, the energy is flowing in a wrong and wasteful way. The evidence for this is sociological, at least as much as it is technical. Thorough testing involves keeping the code and the tests in contact with each other through willpower and force, like holding like ends of two magnets together. Therefore, people who test consistently end up with bragging rights, which they often exercise. I hasten to add that I’m not talking about the really accomplished, masterful engineers of the great testing frameworks we’ve got available to us. Those people are above bragging. But there’s a sub-population that isn’t.</p> <p>I’m really tired of seeing the test police needling people about not having written tests. It’s not that people shouldn’t write tests. Like I said, it’s about the energy flowing the wrong way. The whole culture of test machismo is, start to finish, a waste of energy and, above all, doesn’t work. You can’t get the whole world to write tests by trying to shame people into it, one person at a time. As long as the technical conditions allow for untested code, untested code there will be.</p> <p>So we’ve got untested code, alongside a culture of testier-than-thou assertiveness. Neither is good.</p> <p>And then there’s the programming should be fun thing. Programming should be fun. Testing should be a big part of programming. Therefore, testing should be fun. However, it’s acquired a sort of “do it because it’s good for you” aura, like using a treadmill or eating your vegetables. Again, this take on testing is wasteful and irrelevant&mdash;but it arises directly from the physical possibility of running untested code, and will not go away as long as that possibility exists.</p> <p>I’ve made some very sketchy, preliminary attempts to see what a Probative Programming file might look like, for a Ruby program. It’s a daunting task, and one I may or may not ever succeed at. But I’m convinced that something along these lines is both possible and desirable.</p> <p>Finally, if there are existing systems that do what I’m describing, or anything substantially similar to it, I’d be interested in hearing about them.</p> dblack tag:dablog.rubypal.com,2008-11-24:277 2008-11-24T12:18:00Z 2008-11-24T12:19:26Z RESTful Rails for the restless <h2>QuickStarts-R-Us</h2> <p>As one of the most active Rails trainers on the circuit, I come up a lot against the challenge of introducing RESTful Rails to relative newcomers. It’s a challenge because the <span class="caps">REST</span> support in Rails is very high-level and, even for the diligent, basically impossible to understand deeply without a knowledge of the subsystems&mdash;in particular, the routing system&mdash;on which it is built.</p> <p>I believe it’s possible, nonetheless, to understand up front how the RESTful support in Rails fits into the subsystems that support it; and I believe that it’s beneficial to gain such an understanding. My purpose is thus to provide a “QuickStart” introduction, not to the practice of writing RESTful Rails applications but to the way the <span class="caps">REST</span> support in Rails fits into what’s around and beneath it. If you want to do RESTful Rails but either find it too magical or don’t quite understand how it relates to the framework overall (does it add? supersede? enhance?), then this article may be of interest to you.</p> <p>You may wonder why I’m not making use of the Rails scaffolding. That is, as they say, “a whole nother” story. Short answer: the scaffolding gives you a quick start, but also a quick end. It explains nothing and leaves you with a lot of work to do to reverse the ill effects of having a lot of “one-size-fits-none” code lying around your application directory.</p> <p>So no scaffolding. Also, no <span class="caps">REST</span> theory&mdash;but by all means have a look at the theory once you get into the practice. It’s just not my focus here.</p> <p>In what follows, I’ve tried to be concise&mdash;minimalist, almost. I’d advise not skimming over anything, even if you think you already know it. I’m chosing the path carefully. If you don’t trust me as a guide, that’s another matter entirely :-) If you do, welcome.</p> <h2>What a (non-RESTful) Rails application does</h2> <p>The job of a Rails application is to provide responses to requests. Responses are generated by controller actions, which are (in Ruby terms) instance methods of controller classes.</p> <p>When your application receives a request, the first order of business is to figure out which action to execute. The subsystem that does this is the routing system. It’s the routing system’s job, for every request, to determine two things: </p> <pre><code>1. controller 2. action</code></pre> <p>If it cannot determine those two things, it has failed, and you get a routing error. If it can, the routing has succeeded. End of story. (You might get a “No such action” error, but that’s not the routing system’s problem. The routing system has done its job if it comes up with an action, whether the action exists or not.)</p> <p>The main information that the routing system uses to determine which controller and action you want for a given request is the request <span class="caps">URL</span>. By definition, every <span class="caps">URL</span> that’s meaningful to your application can be resolved to a controller/action pair. If the <span class="caps">URL</span> contains information beyond that which is needed to determine a controller and action, that information gets stored in the <tt>params</tt> hash, to which the controller action has access. (That’s how you get <tt>params[:id]</tt>, for example.)</p> <p>The routing system uses a rule-based approach to resolving URLs into controller/action pairs. The rules are stored in routes.rb. A rule might say, for example (paraphrased here in English), “A <span class="caps">URL</span> with (1) a string, (2) a slash, (3) a string, (4) a slash, and (5) an integer means: execute action (3) of controller (1) with <tt>params[:id]</tt> set to (5)” (and indeed the default routing rule says exactly that). Rules can be specific, to the point of silliness. It’s perfectly possible to program the routing system so that “/blah” means: “the show action of the students controller with <tt>params[:id]</tt> set to 1010.” There’s almost certainly no point in such a mapping, but the point is that you can program the routing system in a fine-grained way.</p> <p>In the non-RESTful case, the <span class="caps">URL</span> is all that the routing system needs to do its job of performing a rule-based determination of a controller and an action.</p> <p>In the RESTful case, it isn’t.</p> <h2>Enter the verbs</h2> <p>This is the crux of RESTful routing in Rails. Everything else flows directly from this, so make sure you understand it.</p> <p>Instead of routing based solely on rule-driven mapping of each <span class="caps">URL</span> to a controller/action pair, RESTful Rails adds another decision gate to the chain: the <span class="caps">HTTP</span> request method of the incoming request. That method will be one of <span class="caps">GET</span>, POST, <span class="caps">PUT</span>, or <span class="caps">DELETE</span>. It’s the combined information&mdash;URL plus request method&mdash;that the RESTful routing uses to determine the controller and the action.</p> <p>That means that for every incoming request, the correct controller/action pair is determined not per <span class="caps">URL</span>, but per <span class="caps">URL</span> per request method. That, in turn, means that a given <span class="caps">URL</span>, such as this:</p> <pre> http://blah.blah/houses/14 </pre> <p>might map to two or more different controller/action pairs. It all depends on the <span class="caps">HTTP</span> request method.</p> <p>In theory, any one <span class="caps">URL</span> can be routed to as many as four controller/action pairs, because any one <span class="caps">URL</span> can be used in a <span class="caps">GET</span>, <span class="caps">PUT</span>, POST, or <span class="caps">DELETE</span> request. In practice there aren’t that many permutations, because some combinations of request method and <span class="caps">URL</span> semantics are not meaningful. But the principle is what matters: a single <span class="caps">URL</span> no longer has an unambiguous meaning, but must be interpreted in conjunction with the request method.</p> <p>Furthermore, these conjoined interpretations are hard-coded to a pre-determined set of seven actions: <tt>index</tt>, <tt>show</tt>, <tt>delete</tt>, <tt>edit</tt>, <tt>update</tt>, <tt>new</tt>, and <tt>create</tt>. (You can add custom ones, but those are the canonical ones.) For example, the “houses” <span class="caps">URL</span> above, if requested as a <span class="caps">GET</span>, automatically routes to the <tt>show</tt> action of the houses controller, with <tt>params[:id]</tt> set to 14. If submitted with a <span class="caps">PUT</span>, it goes to the <tt>update</tt> action. <span class="caps">A URL</span> with no id field (/houses) goes either to <tt>index</tt> or to <tt>create</tt>, depending on the request method. And so forth.</p> <p>That, as I say, is the crux of the matter: routing based on <span class="caps">URL</span> plus request method. Keep this in mind as you get into the details and bells and whistles of RESTful Rails.</p> <p>Interpreting requests, though, is only half of the job of the routing system. The other half is the generation of strings.</p> <h2>RESTful <span class="caps">URL</span> generation</h2> <p>When you write this in your view:</p> <pre> &lt;%= link_to "Click here for help", :controller =&gt; "users", :action =&gt; "help" %&gt; </pre> <p>your view ends up containing this:</p> <pre> &lt;a href="/users/help"&gt;Click here for help&lt;/a&gt; </pre> <p>It’s the routing system that does the job of processing the <tt>link_to</tt> arguments and figuring out what the <span class="caps">URL</span> (or, in this case, the relative path) in your tag should consist of.</p> <p>The same thing happens with RESTful routing, except that you never have to spell out the controller and action. Instead, you call yet more helper methods. Compare this:</p> <pre> &lt;%= link_to "User profile for #{user.name}", :controller =&gt; "users", :action =&gt; "show", :id =&gt; user.id %&gt; </pre> <p>with this:</p> <pre> &lt;%= link_to "User profile for #{user.name}", user_path(user) %&gt; </pre> <p>You don’t have to define the method <tt>user_path</tt>. It comes into being automatically, when you write:</p> <pre> map.resources :users </pre> <p>in routes.rb. And it has a simple job: return the right string, in this case the string ”/users/14” (assuming that user.id is 14).</p> <p>For every resource you route, you get a fistful of such methods: <tt>user_path(user)</tt>, <tt>users_path</tt>, <tt>new_user_path</tt>, and <tt>edit_user_path</tt> (plus all of these with <tt>_url</tt> instead of <tt>_path</tt>). These methods do nothing but generate strings. They have no knowledge of request methods or <span class="caps">REST</span>. In fact, they’re just examples of named routes&mdash;methods that generate the right strings for specific routing rules&mdash;and you can use named routes in routes.rb even without <span class="caps">REST</span>. The only <span class="caps">REST</span>-related special treatment is that <tt>map.resources</tt> automatically writes a bunch of these methods for you. You can think of <tt>map.resources</tt> as, primarily, a macro that writes named route methods, much as <tt>attr_accessor</tt> automatically writes getter and setter methods.</p> <p>The specifics of what the various RESTful named route methods do is for future study. The point here is to see the roadmap. You do <tt>map.resources :users</tt>, and from that point on, you can use methods in your views to create <span class="caps">URL</span> strings, rather than having to spoonfeed the information about which controller, action, and id are involved.</p> <p>But that still leaves the question of the request method. How does ”/users/14” know which action to trigger when clicked?</p> <h2>Specifying request methods</h2> <p>When you write view code that generates path strings (with <tt>link_to</tt>, <tt>form_for</tt>, <tt>link_to_remote</tt>, etc.), you want the right string, obviously, but you also need the link, when clicked, to use a particular <span class="caps">HTTP</span> request method for the request. Otherwise the RESTful routing system won’t have enough information to make sense of the <span class="caps">URL</span>.</p> <p>The helper methods that generate hyperlinks all have sensible <span class="caps">HTTP</span> request method defaults (which you can override if needed). <tt>link_to</tt> generates a link that will submit a <span class="caps">GET</span> request. <tt>form_for</tt> generates a <span class="caps">POST</span> form tag (method=“post”), unless you tell it to use <span class="caps">PUT</span> (which is conventional for update operations, as opposed to new record creation operations), and so forth.</p> <p>Again, the named route methods don’t have request method intelligence. The enclosing hyperlink-writing methods (<tt>link_to</tt> and friends) do. They just used the named route methods as lower-level helpers for the specific purpose of generating the right strings.</p> <h2>Invisible ink</h2> <p>One of the challenges of using RESTful routing in Rails is that you end up with not very much information available to you visually. When you write a RESTful form in your view, let’s say for an update:</p> <pre> &lt;% form_for :house, :url =&gt; house_path(@house.id), :html =&gt; { :method =&gt; :put } do |f| %&gt; &lt;% end %&gt; </pre> <p>you never see the word “update” in routes.rb, nor in the <span class="caps">URL</span>, nor in the view templates, nor in the <span class="caps">HTML</span> source of your rendered views. You just have to know that a <tt>thing_path</tt>-style named route, coupled with a request method override to <span class="caps">PUT</span> (override of the default <span class="caps">POST</span> for <tt>form_for</tt>, that is), will result in a form that, when submitted, will send a <span class="caps">PUT</span> request to the <tt>update</tt> action of the houses controller. And you have to trust that the routing system will succeed in so routing it.</p> <p>RESTful routing pushes most of the routing intelligence&mdash;which, as you now know, means the determination of a controller/action pair from an incoming request&mdash;under the surface. You have to learn how the <span class="caps">REST</span>-ified routing system thinks. The early phases of learning RESTful routing tend to involve memorizing the combinations of named routes and request methods, and which action they point to. The good news is that there’s a finite number of them, and they make sense. If it seems like routing soup, hang in there and look closely at the logic. It will come clear.</p> <h2>The rest…</h2> <p>That’s the basics. There’s a lot more to it, including (but not limited to) more “magic” shortcuts. But if you get the basic ideas you’ll be in good shape. </p> <ul> <li>The basic routing system resolves a <span class="caps">URL</span> to a controller/action pair.</li> <li>RESTful routing resolves a <span class="caps">URL</span>/request-method combination to a controller/action pair. </li> <li><tt>map.resources :things</tt> generates a bunch of named routes (<tt>things_path</tt>, etc.) for you automatically.</li> <li>You don’t see as much visual evidence of the routing logic with RESTful routing as with non-RESTful routing, so you have to learn exactly what it’s thinking, especially the seven hard-coded action names.</li> </ul> <p>Now go forth and <span class="caps">REST</span>. Oh, one more thing. Here’s a chart I once made, showing how the named routes map through the request methods to the seven canonical actions. The chart uses the <tt>_url</tt> methods (which give you the whole thing, including http://), but the <tt>_path</tt> versions would exist too.</p> <p><img src="http://www.wobblini.net/images/routes.png" alt="RESTful routing chart" /></p> dblack tag:dablog.rubypal.com,2008-10-03:233 2008-10-03T21:14:00Z 2008-10-03T21:15:36Z Why I am suspicious of the bailout bill <p>The bailout bill has just passed. I know very little about economics, little enough that I don’t feel entitled to a strong opinion one way or the other on whether the bill should have passed. But I am suspicious of it.</p> <p>I’m suspicious of it, for one thing, because of the fear-mongering that has surrounded it; it’s very reminiscent of the ongoing “Terrorists will come and kill your family if the executive branch doesn’t get a blank check for waging undeclared war” campaign, and things in that vein.</p> <p>But I’m even more suspicious of the bill because of all the rhetoric about how it will help “Main Street” as well as “Wall Street”. I don’t know whether it will or not, but what troubles me is the fact that this kind of rhetoric makes it sound like Congress and the Bush administration are desperate to help Main Street. The fact is that, in general, they’re not.</p> <p>Every microsecond of every day in the history of this country there have been uncountable opportunities for the government to help citizens with financial problems, difficulty paying for a home, lack of job opportunities, inability to get credit, and all the rest of it. The thrust of the behavior of the government for most of the history of the country has been not to bother helping such people to any significant degree.</p> <p>Now, all of a sudden, helping Main Street leaps to the front of the congressional and executive agenda. I’m disinclined to buy it. If the common weal were really a government priority, we would have known by now. I find it immensely suspicious that the greatest outpouring of social concern, at least as measured in money, comes tethered to a Wall Street bailout.</p> <p>If Main Street is going to benefit from the delivery of a de facto blank check to Wall Street, surely it would not benefit any <i>less</i> from having money delivered to it directly. But you don’t hear any talk of, say, the government purchasing houses for the victims of fiscal mismanagement. I suppose it would have taken too long to draft a bill that did that; and as we know, the earth would have left its axis if the bill had not been passed this week….</p> dblack tag:dablog.rubypal.com,2008-09-13:219 2008-09-13T10:00:00Z 2008-09-13T10:05:23Z Tracks a-go-go at RubyConf 2008! <p><a href="http://www.rubycentral.org">Ruby Central</a> is gearing up for <a href="http://2008.rubyconf.org">RubyConf 2008</a>, which has a fantastic program and which you can still <a href="https://www.regonline.com/rubyconf2008">register for</a> (at time of writing, anyway!).</p> <p>People have noticed, naturally, that we’ve gone over entirely to a multi-track format (except for keynotes and a couple of other special slots). And they’re surprised; we used to be one-track, and then last year we were multi-track but with a good dose of plenary sessions.</p> <p>So I thought I’d say something about the multi-trackedness of RubyConf 2008, for anyone who’s interested.</p> <p>The bottom line is that we’ve scheduled multiple tracks because we got so many really, really good proposals. Of course we can’t accept all of them; we can’t be <em>that</em> multi-track. There will always be a cutoff, and where the cutoff comes always involve a judgment call. This time around the judgment was that the number of talks we’d have to exclude, in order to dilute the multi-trackedness significantly, was too great.</p> <p>In fact, we started drafting a schedule without explicitly discussing the multi-track issue; it mostly emerged from what we jotted down, and then it continued to make sense to us as we started analyzing the track issue more closely.</p> <p>People have asked whether it’s about the size of the event. It is, in a couple of ways&mdash;subtle ways, perhaps, but important.</p> <p>For one thing, we know that not every speaker is comfortable getting up in front of 500 people. Lots are, but it’s still a lot to ask. Breakout sessions make for situations in which more speakers are likely to be comfortable.</p> <p>Of course, if there are only fifteen speakers, we could easily find people who don’t mind a big audience. But what about that “only fifteen speakers” thing?</p> <p>In a conference with 400-500 people present, it’s definitely more fun if, say, twelve percent of the people prowling the halls and sitting next to you at lunch are speakers, instead of two or three percent. Having fifteen speakers at an event with over 400 people isn’t the same, for anyone, as having fifteen speakers at an event with sixty people. If the ratio is too lop-sided, it gets too much into the “us and them” thing. We’ve never been into that.</p> <p>Another reason we’re OK with moving toward a multi-track format is the proliferation and success of the Ruby regional conferences, many of which are one-track. Everyone should attend, at some point, a one-track conference. It’s really cool the way everyone at such a conference shares the same experience. My first conference was a one-track academic film conference in 1985, and it was great. And the wonderful flowering of the Ruby regional conference culture means that, even if it isn’t at RubyConf, many Rubyists will get a chance to have that experience.</p> <p>We started our regional conference grant program in 2006 in the hope that “regional” wasn’t going to mean “provincial”&mdash;that regional conferences could be top-notch events&mdash;and that hope has been fulfilled beyond what we could possibly have wished for. (And certainly way beyond what we can take credit for. The regional organizers have been amazing!) These high-quality small events can address many needs and desires, including the desire for the experience of a one-track format.</p> <p>In sum, the RubyConf format for 2008 is a format for its time, its year, its configuration of the Ruby world. We’re nothing but excited about it and hope you’ll come and share the fun!</p> dblack tag:dablog.rubypal.com,2008-09-06:216 2008-09-06T08:07:00Z 2008-09-06T08:08:47Z Back from RailsConf Europe 2008 <p>I got home yesterday from <a href="http://en.oreilly.com/railseurope2008/public/content/home">RailsConf Europe 2008</a> in Berlin, and am very happy to say that the event was a major success.</p> <p>It was particularly gratifying to hear from many attendees that they found the program content more advanced and more instructive than last year. It’s always hard to fine-tune the level of talks across a big program like this, and I’m really glad to have evidence that people overall felt it had gone in the right direction.</p> <p>Highlights included keynote addresses by David Heinemeier Hansson and Jeremy Kemper, as well as a Rails core team panel discussion with David, Jeremy, and Michael Koziarski. <span class="caps">DHH</span> led us through some very interesting thoughts on the notion of “legacy” code, and how that concept plays out with respect to one’s own development and growth as a programmer. Jeremy talked about performance, and masterfully expanded the horizon beyond the shop-worn “Does Rails scale?” stuff to some very specific and powerful techniques for evaluating and adjusting performance.</p> <p>We also held a “Symposimi” (the name is based on a misspelling in the program; it should have been “Symposium” but came out “Symposimi,” and I decided that sounded really cool!) on the subject of Ruby versions and implementations&mdash;who’s using what, what’s targeting what, the pros and cons of moving to 1.8.7 and/or 1.9. A symposimi is a town-meeting-like gathering of people who want to ask and answer questions about a topic. It’s more audience-based than a symposium, and less hierarchical.</p> <p>The symposimi was fun for me because I got to do some live code demos, which I usually don’t at the conferences I’m an organizer of!</p> <p>Lots of people asked about next year. We don’t know yet where RailsConf Europe will be in 2009. Probably not Berlin, just because we’d like to move it around. If you have suggestions (and a rationale other than that you happen to live there :-) by all means let me know.</p> <p>Now that <span class="caps">RCE2008</span> is over, I’m looking forward to RubyConf. Stay tuned for announcements of the program and registration!</p>