The Artima Developer Community
Sponsored Link

Agile Buzz Forum
Ruby Tips

0 replies on 1 page.

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 0 replies on 1 page
Jared Richardson

Posts: 1031
Nickname: jaredr
Registered: Jun, 2005

Jared Richardson is an author, speaker, and consultant who enjoys working with Ruby and Rails.
Ruby Tips Posted: Jun 20, 2006 9:16 PM
Reply to this message Reply

This post originated from an RSS feed registered with Agile Buzz by Jared Richardson.
Original Post: Ruby Tips
Feed Title: Jared's Weblog
Feed URL: http://www.jaredrichardson.net/blog/index.rss
Feed Description: Jared's weblog. The web site was created after the launch of the book "Ship It!" and discusses issues from Continuous Integration to web hosting providers.
Latest Agile Buzz Posts
Latest Agile Buzz Posts by Jared Richardson
Latest Posts From Jared's Weblog

Advertisement

I've recently been working on a Ruby and/or Rails driver for the Ingres database and have discovered a bit about Ruby and ActiveRecord, so I thought I'd post it here. I was something of a Ruby tinkerer before this project, but definitely a "newbie", so I thought others new to the platform might find a few tips helpful. Nothing heavy or deep, just a few hints that helped me.

First, editors. There are a lot of great editors for Ruby. Eclipse has a lot of Ruby support. There's RadRails. But I've been on three (really four) platforms very regularly and needed something lightweight and I wanted fast and cheap. Then Neal Ford told me he was using JEdit for Ruby code. It's written in Java, so it runs on Windows, Red Hat, Kubuntu, and OS X. Ruby syntax hightlighting is built in... nothing extra to download. Actually, it's got syntax highlighting for about everything! I do download the "tabs" plugin, but it's got a built-in plugin manager to make additions and updates easy. And it's free. :)

Next, breakpoint. Breakpoint is your friend. :) Your good friend!

Many people have posted about getting a Ruby debugger running in Eclipse, RadRails, and other IDEs, but honestly, I haven't tried. Firstly, I was also coding some C extensions and I didn't think any debugger would handle me passing from Ruby to C and back again. Also, in Java I tend to use Ant scripts and a syntax highlighting editor. I'm doing the same with Ruby, so my debugging was largely prints... "puts" in Ruby.

Just a quick hint... when you want to print a Ruby variable, or any code snippet, put it inside of this

#{  }
. For instance, so see the value of the variable first_name, use
puts #{first_name}
. If you want to print a part of an array,
#{ this_arry[0..8] }
.

When you get a subset of an Array (puts f[0,2]), you're starting at index 0 and getting two entries. You're ~not~ going from entry 0 to entry 2. That would be three entries.

But I digress... Mike Laster showed me breakpoints.

Anywhere in your code where you have something that could use a closer look, drop in the string "breakpoint". Like this:

  def test_column_null_not_null
    subscriber = Subscriber.find(:first)
    assert subscriber.column_for_attribute("name").null

    breakpoint

    assert !subscriber.column_for_attribute("nick").null
  end
Then just run the code. When the breakpoint is encountered, you drop into the IRB shell. From here you can print variables, change values, just go crazy. Anything you can do in Ruby, you can do at the prompt.

When you're done looking and experimenting, type "exit" and your program will continue it's execution.

I also use a pretty printer utility called pp. Just type "require 'pp'" from your IRB shell then you can do this:

pp self

and see everything about the object you're debugging. Especially useful if you're in something like ActiveRecord and, like myself, aren't that familiar with ActiveRecord. :)

Breakpoint was mentioned briefly in the Pick Axe book but wasn't really emphasized. I missed it completely. When Mike told me it was in the book, I assured him it wasn't, grabbed my copy to show him, and found the entry... oops! Next time I'll read it more carefully. ;)

Another useful bit...

"pp caller" shows you who invoked the code you're running. Also a very useful bit of information when you want to know who called "quote".

One final tidbit, in case you decide to write a database adapter yourself. :) There are two key routines, quote and type_def.

When you write data to the database, it goes through quote. Are your booleans not getting translated? Your strings not getting quoted properly? Look at quote.

And type_def... it's called anytime data comes in from the database. It converts your boolean 1s to TRUE and your 0s to FALSE. Date/time conversions? Yup.

Both seem simple in hindsight, but it took me quite a while to understand how key these two routines were.

Hopefully this will save a few people a few minutes of digging out a bug or two. If nothing else, I've got my own ready reference. ;)

Enjoy!

Jared

Read: Ruby Tips

Topic: The Free Weird Al Previous Topic   Next Topic Topic: Microsoft become: IBM2

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use