Book Review: Pro Active Record

Posted by Rick DeNatale Tue, 30 Oct 2007 16:31:00 GMT

I've been meaning to write a review of the recently published Apress book "Pro Active Record" by Kevin Marshall, Chad Pytel, and Jon Yurek for a while now. I have to admit that I was prompted to sit down and actually do it after reading Josh Susser's recent review of the same book.

While I generally agree with Josh's assessment, We do differ a bit on which audience the book best serves.

The goal of the book is to cover Active Record in depth, outside of the context of Rails. The result, as I see it, is a book which is useful to intermediate to advanced users of Active Record, those who want to dig in to understanding the implementation and perhaps extending it.

My own reading of the book gave me the impetus to explore the code of Active Record to the extent where I felt comfortable submitting contributions to Rails. Since reading the book, I've written and submitted two active record patches to the Rails Trac. The first fixed an oversight which made the schema.db file dumped for MySQL tables with non-standard primary keys to lack those primary key declarations, and the second is an enhancement which allows the :joins option of methods like find and count in ActiveRecord::Base to take values like the :include option as an alternative to a sql joins clause string. Both have made it into rails edge!

Read more...

Posted in ,  | Tags  | no comments | no trackbacks

Chicken Typing Isn't Duck Typing

Posted by Rick DeNatale Mon, 22 Oct 2007 14:43:00 GMT

Trans just posted a question on ruby-talk.

It seems that he uncovered a conflict between the facet's library and the Ruby standard open_uri library. Open-uri defines a module called Meta which it uses to extend certain StringIo instances. The Meta module adds several methods including one called meta, which returns a hash of meta information. In two places in open_uri, the message "respond_to? :meta" is used to test whether a particular StringIO instance has been extended.

The problem is that Facets also defines a method in either Object or Kernel called meta which provides a clever way to access the singleton class of an object. So the respond_to? in open_uri was getting false positives.

Trans asked "So where lies the fault in this conflict? Are extensions the bad guy, or is respond_to? really not a good oop concept? Or..?"

David Black replied that he thinks "this is just the age-old issue about adding methods to existing classes and hitting conflicts. #respond_to? is relatively dumb; it can't tell you what the method actually does. I think of it as an important tool for "soft" duck typing, in contrast with "hard" duck typing where you just send the object the message. "

Reading this made me realize that a much better name than "soft" duck typing, for using kind_of?/is_a?, respond_to?, or other such "sniff first" techniques, is Chicken Typing

Read more...

Posted in ,  | Tags ,  | 5 comments | no trackbacks

True Confesssions

Posted by Rick DeNatale Thu, 18 Oct 2007 14:52:00 GMT

Shhhh! I've got a confession. I've been known to use rdebug

Today, my evil twin, Pat Mueller, posted an article alerting me to the fact that some rubyists seem to think that using a debugger is some kind of moral failing.

Pat took that belief to task, and I've got to agree.

Like all of you, my gentle readers, I'm perfect. I never make mistakes, so I never need a debugger. Well I thought that I made a mistake once, but discovered I was wrong!

Seriously, when I first encountered Ruby it was really lacking in debugger support. Although I missed a good debugger, I struggled along, and yes, I didn't miss it all that much. Why? Because a debugger has always been just one tool in my arsenal.

Most of the time I'm happy to write tests, see that the test fails, write code, make the test pass, lather, rinse and repeat. By taking small steps, I find that it usually goes rather well.

But, on the rare occasion, that fourth step poses problems, and that's when I pull out rdebug.

In fact it's usually: rdebug test/unit/someclass_test.rb although I run rails under rdebug from time to time.

Ruby IDEs like netbeans and aptana are now provide good integrated debugging. Pat mentions this, although he's not as hooked in to the ruby community as he should be, so I don't think he realizes that they are using rdebug through an ide interface wrapper.

Now using IDEs seems to be another moral failing by some luminaries estimation, but then again maybe not.

Personally, I've got nothing against a good IDE, Heck, I used to work with and on Smalltalk IDEs (VisualAge), and Java IDEs (VA/JAVA, VA MicroEdition, and Eclipse). But I've been almost as happy using VIM on Linux and now TextMate on OSX. There's even a TextMate bundle which talks to rdebug and let's you set breakpoints.

I don't use that much, but when I need it, I'm happy and unashamed.

3 comments | no trackbacks

You Can't Judge a Book... Some Mental Traps in Learning Ruby

Posted by Rick DeNatale Wed, 17 Oct 2007 17:01:00 GMT

Quick, in the Ruby expression:
a[10]

What's the class of a?

Many beginning, and intermediate, rubyists whould instinctively say Array! The real answer is that given just this code, you don't know.

Recently, on the rails-talk mailing list there was a discussion of this idea from Jamis Buck. Jamis uses alias_method in ActiveRecord::Base so that he can use shortcuts like:

Person[1]

as a shortcut for:

Person.find(1)

This is particularly nice when playing with ActiveRecord in the rails console, but many seem to think that this should be made part of Rails core, and I don't see a real objection.

Now one objection raised by the folks who answer Array to the opening question is that, since ActiveRecord rows can be deleted, Person[2] might fail even though Person[1], and Person[3] work. The reasoning is that the [] makes it "look like an array" and arrays don't act like that.

To me this is a case of judging a book by looking at it's cover. Something which I strongly suggest needs to be resisted in order to achieve ruby mastery.

Let's have a look at a couple of "can't see past the cover" traps in Ruby.

Read more...

Posted in , ,  | 7 comments | no trackbacks

It's Official

Posted by Rick DeNatale Mon, 15 Oct 2007 15:34:00 GMT

180px Mini Merc Press Suit.3.rjd I haven't said anything publicly, but for some months I've been doing some work for Terralien. and Nathaniel made me an official "crew member" just over a month ago.

He's finally updated the people page so I guess I should make my own announcement here.

I love the arrangement. I get to focus on consulting work, and Terralien acts as a manager/agent, and provides some infrastructure and team support. Nathaniel has built a great team, and if you've got a great idea and need experienced Ruby/Rails developers to realize it, contact Terralien, and tell Nathaniel I sent you.

Posted in  | 1 comment | no trackbacks

Refactoring With Continuous Testing - a Guided Tour

Posted by Rick DeNatale Thu, 11 Oct 2007 10:47:00 GMT

DavidCensoredOnRefrigerator
Refrigerator Censorship
©
Today on Ruby Fleebie, Frank poses some ruby code to be "rubyized." I took this as an opportunity to do a little exposition of re-factoring under the watchful eye of autotest.

So I've taken Frank's code and run it through the re-factoring machine several times. A word to the 'squeamish' because of the use case, there are a few words in the code which some folks, and spam filters might find mildly offensive, but we're all adults here right?

Now I've got to admit that I really came up with the final solution pretty quickly after seeing the blog post, but then I went back and eased up to it for pedagogical purposes. So if you're in the mood, sit down and lets re-factor some ruby!

Read more...

Posted in ,  | Tags , , ,  | 8 comments | no trackbacks

My First Serious TextMate Automation

Posted by Rick DeNatale Tue, 09 Oct 2007 12:15:00 GMT

I recently got assimilated by the Mac/TextMate borg. I'm slowly teaching my fingers to dance the TextMate tango and unlearning old vim habits.

One resource has been James Edward Gray II's book on TextMate published by the Pragmatic Programmers. I finally sat down and got serious about writing some automations of my own.

I've got to say that I'm pretty impressed by TextMate. At the last Raleigh.rb hack night I was talking to another vim user. I'd mentioned to him that you can extend TextMate easily in Ruby, without really having experienced it. Today, I wrote a neat little TextMate command to help in building Rails database test fixtures.

It acts like a tab triggered snippet, but it's a smart little critter. If I'm editing a fixture file, say 'test/fixtures/users.yml', I can type item then tab and it will produce the skeleton yaml for a new record, with:

  1. A dummy name selected for overtyping.
  2. The id set to the next available primary key
  3. Each column name as a yaml key ...
  4. ... A tabstop on a value which shows the column type

For example:

Read more...

Posted in  | Tags , , ,  | 1 comment | no trackbacks

Observing Fields With Rails in the Browser

Posted by Rick DeNatale Sun, 07 Oct 2007 16:34:00 GMT

The AJAX support in Rails is great, but sometimes you don't want to have to make an unnecessary trip to the server to make your UI dynamic. For example, let's say you want to do something like Basecamp does when you create a new message. If you have a Basecamp setup to allow you to talk to both members of your team and also clients, it provides some nice checkboxes to let you control who will get notified of the new message. Those checkboxes are grouped with a checkbox for each company, and then checkboxes for all of the people from that company below the company checkbox. If you check or uncheck a company checkbox all of the people listed under the company are checked or unchecked to match.

This can all be done within the browser. It just requires manipulation of existing elements within the domain object model, so there's no need to go back to the server.

Rails provides the observe_field helper method which is normally used to generate a remote request when a DOM element changes. It can be used to do what we want, but how to do so isn't well documented, either in the Rails documentation, or via google.

So here's how to do it.

Read more...

Posted in  | Tags ,  | 1 comment | no trackbacks

Ruby Blocks, do or brace?

Posted by Rick DeNatale Tue, 02 Oct 2007 11:04:00 GMT

Conventional wisdom in Ruby is to use do/end to delimit blocks which contain more than one line of code, and braces for one-line blocks. I've always tended to loosely follow this advice.

Thanks to ruby-talk, I just became aware of Jim Weirich's suggestion to use braces for blocks when the value is being used, and do/end for blocks which are primarily sequences of statements. Jim actually posted this over three years ago, and Joe O'Brien brought it up more recently.

On the whole, I like this idea and will probably adopt it to tune my use of do/end vs. braces.

Read more...

Posted in ,  | Tags  | 3 comments | no trackbacks

Duck A La Range

Posted by Rick DeNatale Thu, 20 Sep 2007 15:36:00 GMT

On ruby-talk, Sammy Larbi recently asked if it would make sense to add a length method to Range, something like this:
class Range
     def length
          self.end - self.begin
     end
end

This led to an interesting discussion about the varieties of ranges, and ultimately led me to this article about duck types.

Read more...

Posted in  | Tags  | 3 comments | no trackbacks

Older posts: 1 2 3 4 5 6 ... 11