May 3 2006

Rad Ruby On Rails

Yesterday I installed the latest release of Rails, version 1.1.2. 1.1.2? About a month ago Rails hit the big one point oh and now they are at 1.1.2? If I dislike anything about Rails it is that Rails is not ‘stable’ enough for me. Every week there is a new release. It is a thin line between Edge Rails and Gem Rails.

Since I got the latest version of Rails I also took time to get the latest release of RadRails, version 0.6.2. I have to say that the latest version of RadRails has better code hight lites, nicer icon, and better support for updating itself. RadRails still needs work, in particular I would like to see a better code complete.

In a previous post, RadRails is Gnarly, I mentioned that I wasn’t able to run the generator scripts from RadRails. I figured out how. What you need to do is in the open the Window > Preferences. Go to Rails > Rails Installs and add a Rails installation. I had set this information all wrong, that is why I wasn’t able to create controllers or models or anything. I added a rails installation that pointed to the following:

/usr/local/lib/ruby/gems/1.8/gems/rails-1.1.2

I can’t believe how many version of Rails I have on my machine.

Technorati Tags: , , ,


May 2 2006

High Rolling With Rails

Once you get started with Rails you should to get to know the Rails code to get the best out of it. The gems that make up Rails, such as ActiveRecord, are script files located under your Ruby installation. In my OS X environment the Rails source files are found in /usr/local/bin/ruby/gems/1.8/. Looking at the Ruby code that makes up Rails will give you greater insight that you can use in your own applications.

Now, the one feature that I use the most when developing in Rails is the logging functionality. In the controller I use the logger instance in most of my actions. Here is a typical use:

def listRecent
  @items = Items.find(:all, :order => 'submitted DESC', :limit => 10)
  logger << "Found #{@items.size} items..."
end

You can also use the logger’s info, warn, error, and fatal methods as in the following bit of code:

logger.warn "This is a warning"

In the rthml view I use the debug method. Here is how you can use it in your view.

<%= debug params %>
<%= debug @items %>

Technorati Tags: , , ,


Apr 23 2006

Hello World Cocoa

Want to be a Mac Developer? Well, OS X and Xcode makes it easy to start hacking your ideas into Apple applications. I started to ‘write’ a Hello World program using Xcode. In less than five minutes and no coding at all I was able to create what I had in mind:

#import <Foundation/Foundation.h>

int main (int argc, const char * argv[]) {
   NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];

   // insert code here...
   NSLog(@"Hello, World!");
   [pool release];
   return 0;
}

The above code was auto-generated for me by Xcode. Now only if Xcode could auto-generate the code for a new killer app that I been dreaming of.

I also want to note that the above code is a command line, Foundation Tool, application.

The @”Hello, World!” piece of code is a NSString literal. You night also have noticed that there are a lot of types that start with NS. The NS prefix stands for Next Step which is where Cocoa first started out.

Technorati Tags: , , ,


Apr 20 2006

Gnarly Custom Groovy Closures

In a previous post I went over Groovy closures. In this post I will show how you can extend a Java class so as to use it to construct a closure in a Groovy script. I’ll start by defining a plain old Java class with an array of data.

public class Sentence {
    public String[] getWords() {
        return new String[]{"Hello", "World"};
    }
}

You will need to write a Sentence helper class that will iterate over the words and call the Groovy closure. Here is a sample implementation:

public class SentenceClosure {
    public static void each(Sentence s, Closure c) {
        String words = s.getWords();
        for(int i=0; i < words.length; i++) {
            c.call(words[i]);
        }
    }
}

Once you compile the above classes and add them to your classpath you can write a Groovy script such as:

def sentence = new Sentence();
use(SentenceClosure.class) {
    sentence.each {
        println " -> ${it}";
    }
}

At this point you will notice that in the Groovy script it looks like the class Sentence defines an each method but that method is actually implemented in the SentenceClosure class. The SentenceClosure helper class allows you to separate closure specific code from you business class.

Technorati Tags: , , ,


Apr 20 2006

MySQL Import Export

I recently migrated one of our MySQL 3 databases to MySQL 5. I needed to export data from MySQL 3 and import it to MySQL 5. To export the data I had to do the following:

mysqldump -u username -ppassword database_name > FILE.sql

FILE.sql is just a ascii file with create and insert SQL statements. You can import this file like this:

mysql -u username -ppassword database_name < FILE.sql

I read about MySQL Migration Toolkit 1.0 but I had trouble using it. When I used the Migration Toolkit the JVM would crash and produce a hs err log file.

Technorati Tags: , ,


Apr 20 2006

Show SQL Tables

When working with a database sometimes you will want to look up all the available database tables. To show the available tables in SQL Server you can do the following:

select * from information_schema.tables;

In Oracle you can use any of the following statements:

select * from user_tables;
select * from all_tables;

In MySQL I have been using the following since MySQL 3:

show tables;

But in MySQL 5 you can use the following:

select * from information_schema.tables;