small medium large xlarge

Generic-user-small
21 Sep 2011, 17:18
T.J. Zeeman (2 posts)

While working on the atom.builder part of generating a feed, the next snippet confused me a bit:

      entry.author do |author|
        entry.name order.name
        entry.email order.email
      end

Given the label author of the first line I expected this to be used to invoke the name and email methods, but instead they were invoked on the entry itself. Strangely enough this code does seem to be working.

I’ve also tried the code I expected:

      entry.author do |author|
        author.name order.name
        author.email order.email
      end

and this works as well.

I’m a bit confused what is supposed to be the correct piece now. The first (taken from the book)? If so, then why does it have that seemingly unused author part in it? The second? If so, then why does the first one work as well?
Samr_small_pragsmall
21 Sep 2011, 20:02
Sam Ruby (634 posts)

Good catch. |author| is clearly unnecessary, and should be removed from the book.

As you are curious, I encourage you to explore builder. entry is a Builder::XmlMarkup object. If you follow the preceding link, you will find the following text:

the markup block now gets the markup object sent as an argument, allowing you to use a shorter alias within the block.

In this case, author isn’t shorter than entry, but it becomes an alias for the markup object anyway. :-)

You must be logged in to comment