Sample Chapter from "Ruby Best Practices"

== The short story

Hi folks, I bugged O’Reilly to let me release a sample chapter of my
book, and they let me.

So you can now directly download a PDF copy of my “Mastering the
Dynamic Toolkit” chapter without having to go through any hoops:

== For a little background on RBP:

This book is aimed mostly at people with a decent technical grasp of
Ruby but not a ton of practical experience. The goal is to show how
to attack Ruby problems in the way that a seasoned Rubyist might.
Though it might not be suitable for raw beginners, intermediate Ruby
programmers should be able to learn a lot from the book, and strong
Ruby programmers should enjoy it. I’ve put together a great
internal review team to help me make sure I reach this goal, and we
also have Rough Cuts set up for general feedback.

== For a little background on this chapter

In the PDF I linked above, you’ll find all sorts of meta-programming
and DSL goodness, if not by name. I try to emphasize how much these
‘voodoo’ like features can be treated like ordinary Ruby code if you
take some care to do things well.

I chose this chapter to release because it’s my favorite. Most of the
other chapters in the book follow the same general format, though.
That means if you like this one, you’ll probably enjoy the others as
welll.

== Please help me improve the book!

I still have a few weeks to make revisions, so whether you plan on
buying the book or not, feel free to offer some feedback. I’ve also
made an agreement with O’Reilly to allow me to release the book under
a creative commons license 9 months after publication.

If after reading the chapter you decide you want to see the whole
thing and offer some suggestion before it goes to print, please buy a
rough cut:

http://rubybestpractices.com

If you want to know exactly what’s in the book, you can check out this
blog post:

http://blog.majesticseacreature.com/archives/2009.02/whats_in_rbp.html

Anyway, I’ll spam the list only twice more, when the book reaches
content completeness (in a few weeks), and when it goes to print
(hopefully in June). I hope you enjoy it!

-greg

Gregory B. wrote:

== The short story

Hi folks, I bugged O’Reilly to let me release a sample chapter of my
book, and they let me.

So you can now directly download a PDF copy of my “Mastering the
Dynamic Toolkit” chapter without having to go through any hoops:

http://cachefly.oreilly.com/catalogs/Mastering_the_Dynamic_Toolkit.pdf

Jump on this. It’s great stuff.


James B.

www.jamesbritt.com - Playing with Better Toys
www.ruby-doc.org - Ruby Help & Documentation
www.rubystuff.com - The Ruby Store for Ruby Stuff

On Wed, Feb 25, 2009 at 10:33 PM, James B. [email protected]
wrote:

http://cachefly.oreilly.com/catalogs/Mastering_the_Dynamic_Toolkit.pdf

Jump on this. It’s great stuff.

Seconded, I’ve seen several of the chapters and talked with Gregory
about this and it’s quality.

Ruby books haven’t been doing well of late (sales are down 51%
over last year), so it’s good to see a good book that the community
can get behind. Maybe this will help reverse the trend.

On Wed, Feb 25, 2009 at 8:24 PM, Gregory B.
[email protected] wrote:

== The short story

Hi folks, I bugged O’Reilly to let me release a sample chapter of my
book, and they let me.

So you can now directly download a PDF copy of my “Mastering the
Dynamic Toolkit” chapter without having to go through any hoops:

http://cachefly.oreilly.com/catalogs/Mastering_the_Dynamic_Toolkit.pdf

I forgot to mention that for the twitter inclined, you can find book
announcements at:
http://twitter.com/rubypractices

Now I’ll really stop with the self-promotion :slight_smile:

-greg

On Feb 26, 2009, at 11:05 AM, pat eyler wrote:

Dynamic Toolkit" chapter without having to go through any hoops:

http://cachefly.oreilly.com/catalogs/Mastering_the_Dynamic_Toolkit.pdf

Jump on this. It’s great stuff.

Seconded, I’ve seen several of the chapters and talked with Gregory
about this and it’s quality.

Agreed. It’s been a great project to be an early reviewer on.

James Edward G. II

2009/2/26 Rados³aw Bu³at [email protected]:

My guess is that there are too many books that describe only ruby (as
a language) and rails (I’m pretty sure that there are more books about
rails than ruby itself… - it’s paradox :)). Gregg’s books should be
shiny gem in ruby books land.

IMHO, “Design Patterns in Ruby” by Russ Olsen is another shiny gem.

Cheers,
Antonio

On Feb 26, 2009, at 12:00 PM, Antonio C. wrote:

2009/2/26 Rados³aw Bu³at [email protected]:

My guess is that there are too many books that describe only ruby (as
a language) and rails (I’m pretty sure that there are more books
about
rails than ruby itself… - it’s paradox :)). Gregg’s books should be
shiny gem in ruby books land.

IMHO, “Design Patterns in Ruby” by Russ Olsen is another shiny gem.

I agree:

http://blog.grayproductions.net/articles/design_patterns_in_ruby

James Edward G. II

2009/2/26 James G. [email protected]:

I agree:

http://blog.grayproductions.net/articles/design_patterns_in_ruby

I’ve really been wanting to read that book. It sounds awesome.

-greg

On Thu, Feb 26, 2009 at 6:05 PM, pat eyler [email protected] wrote:

Ruby books haven’t been doing well of late (sales are down 51%
over last year), so it’s good to see a good book that the community
can get behind. Maybe this will help reverse the trend.

Mu guess is that there are too many books that describe only ruby (as
a language) and rails (I’m pretty sure that there are more books about
rails than ruby itself… - it’s paradox :)). Gregg’s books should be
shiny gem in ruby books land.


Pozdrawiam

Rados³aw Bu³at
http://radarek.jogger.pl - mój blog

2009/2/26 Gregory B. [email protected]:

IMHO, “Design Patterns in Ruby” by Russ Olsen is another shiny gem.

I agree:

http://blog.grayproductions.net/articles/design_patterns_in_ruby

I’ve really been wanting to read that book. It sounds awesome.

It is pretty awesome. If you want a short taste, here’s the interview I
did with Russ:

Gregory B. wrote:

This book is aimed mostly at people with a decent technical grasp of
Ruby but not a ton of practical experience. The goal is to show how
to attack Ruby problems in the way that a seasoned Rubyist might.

I have just about 0 technical grasp of ruby, but so far I can understand
everything in your example chapter. I did have to look up a few things
in pickaxe2.

== Please help me improve the book!

I still have a few weeks to make revisions, so whether you plan on
buying the book or not, feel free to offer some feedback.

Ok, here goes.

  1. In this sentence:

“Builder works by capturing calls to missing methods, but if a method is
actually defined, it’d have trouble doing its magic.”

The contraction “it’d” is awkward and rarely used in conversation or in
writing. Substitute “it would” instead.

  1. This sentence:

“For example: if XmlMarkup were just a subclass of Object, with no
methods removed, you wouldn’t be able produce this XML, due to the
naming conflict.”

The phrase ‘this XML’ had me looking up–instead of down. It would be
clearer to me if the sentence was changed to this:

“For example, if XmlMarkup were just a subclass of Object with no
methods removed, you wouldn’t be able produce the following XML due
to a naming conflict.”

  1. Error here:

“The technique used here is simply to initialize a hidden_methods hash
within the class, and then assign then method name as a key to the
associated UnboundMethod object.”

That seems to be a common error in the text. You should do a search for
‘then’ to find similar errors.

Also, the “a @hidden_methods” phrase is awkward. An alternative:

“The technique used here is simply to initialize a method named
@hidden_methods with a hash…”

I think the phrase ‘within the class’ is redundant, so that could be
eliminated to make the sentence approximately the same length.

  1. I found this confusing:

A.new
NoMethodError: undefined method `inspect’ for #<A:0x42ac34>

Maybe IRB bug!! <-----***

You use “Maybe IRB bug!!” in several places, and it doesn’t add anything
explanatory to the output–quite the contrary. I found it confusing.

  1. I found the following example more confusing than what it was trying
    to explain:

Think of this as a much higher-level version of simple temporary
state modification that mirrors this structure:

x = 10
=> 10

old_x, x = x, nil
=> [10, nil]

x.nil?
=> true

x = old_x
=> 10


I think the parallel assignment makes it hard to track what’s going on.
If the reader is such a beginner that you need to spell out how to save
a value and then later restore it, how about doing it with absolute
crystal clarity(which in my opinion can’t be done with irb output):

x = “hello”
old_x = x

x = 30
puts x #30

x = old_x
puts x #“hello”

Personally, I think the original explanation was clear enough, and this
example should be binned.

On Thu, Feb 26, 2009 at 3:30 PM, 7stud – [email protected]
wrote:

Gregory B. wrote:

This book is aimed mostly at people with a decent technical grasp of
Ruby but not a ton of practical experience. The goal is to show how
to attack Ruby problems in the way that a seasoned Rubyist might.

I have just about 0 technical grasp of ruby, but so far I can understand
everything in your example chapter. I did have to look up a few things
in pickaxe2.

That’s great. If you can read this chapter with a reference to dive
into when you need it, then you’ll have no trouble with the whole
book. My disclaimer is mostly because I don’t make any effort to
include ‘beginner ruby’ content, but it may not be as difficult to
approach as I make it sound for those who are willing to look things
up.

== Please help me improve the book!

I still have a few weeks to make revisions, so whether you plan on
buying the book or not, feel free to offer some feedback.

Ok, here goes.

For the grammatical corrections, I’ll make all of them. I should have
put a disclaimer that this hasn’t gone through O’Reilly’s proofreaders
yet, only the internal tech review team I set up. So the typos and
bad grammar are all my fault, and will be corrected before we print,
hopefully. Of course, those who have signed up for roughcuts or read
this free chapter are still encouraged to point out these issues. The
more I fix before I hand the draft over, the better.

  1. I found this confusing:

A.new
NoMethodError: undefined method `inspect’ for #<A:0x42ac34>

Maybe IRB bug!! <-----***

You use “Maybe IRB bug!!” in several places, and it doesn’t add anything
explanatory to the output–quite the contrary. I found it confusing.

It’s part of the actual irb output, so that’s just verbatim copying,
not added commentary. I agree though. Take it up with ruby-core :slight_smile:

  1. I found the following example more confusing than what it was trying
    to explain:

I think the parallel assignment makes it hard to track what’s going on.
If the reader is such a beginner that you need to spell out how to save
a value and then later restore it, how about doing it with absolute
crystal clarity(which in my opinion can’t be done with irb output):

You’re right… I shouldn’t be expecting the reader to not have this
experience.

Personally, I think the original explanation was clear enough, and this
example should be binned.

So, actually… there are more than a few cases where an internal tech
reviewer pointed something out, and I said “If a reader points this
out, I’ll make the change”. This is one of them. Consider that
example slashed.

Thanks for the great feedback.

-greg