Ruby idioms

Posted on July 06, 2007

Exceptions, begin/rescue/else/ensure/
Did you know that your begin/rescue/else/ensure code can have an else?

Rails to_xml

Given:

puts ContactPhoneNumber.find(:all).to_xml

Rails is going to create xml that looks like:

<?xml version="1.0" encoding="UTF-8"?>
<contact-phone-numbers type="array">
  <contact-phone-number>
    <created-at type="datetime">2007-11-09T15:46:30-08:00</created-at>
    <customer-id type="integer" nil="true"></customer-id>
    <id type="integer">1</id>
    <phone-number>12065558888</phone-number>
    <updated-at type="datetime">2007-11-09T15:46:30-08:00</updated-at>
    <verified type="boolean">false</verified>
  </contact-phone-number>
</contact-phone-numbers>

** (two asterisks) in globs

Posted on May 25, 2007

Never realized before that two asterisks had special meaning in a file glob.

They mean “give me all directories recursively, including this directory.”

See the Dir documentation for the full details. And notice that it’s not documented everywhere you might expect; Pathname’s documentation doesn’t talk about it, for example.