Archive for June, 2007

FirebugLogger Released

So I finally decided to release one of my toy Rails plugin to the public, hoping that someone besides me can benefit from it.

My Rails development consists of using multiple screens, with one running tail -f logs/development.log . Now the problem I found with Rails development is that the logs were hard to read/decipher through. Sure you can trim them in various ways, but when you want things fast and instant, its a bit tedious. Especially when you are debugging a page, and breakpoints are not available to you. (Darn you v1.8.5!)

One of my favorite tools for web development is Firebug, a Firefox extension that makes web development a whole lot easier. Its terrific for debugging JavaScript and Ajax, modifying the DOM tree and bending it to your will, and generally all around awesome!

I decided to create a simple plugin that would log things to the Firebug console. Actually its very simple, just save the log calls and output them to the view! It’s more like a hack! But hey it does its job.

Example

Now FirebugLogger isn’t the same as the default Logger. In the Controller a new object, firebug, is exposed for you to submit your logs.

Inside your View you just have to place:

After it is all baked and done, here is a screenshot for you:
FirebugLogger

If you are familiar with PHP development, then you’d probably know about the echo/var_dump type of debugging. But you can’t do that in Rails, thus the intention for this plugin was something like that. At least it doesn’t break your beautiful pages :).

But it is also smart in that it won’t display anything in your test or production environments. (You could if wanted to). So you can leave the logs there without worrying too much.

I opted not to mix it with the default Logger as I didn’t want the ActionController or ActiveRecord logs filling up the JavaScript console! If I wanted that I could just tail the logs, which I already do.

Now FirebugLogger does not seek to replace breakpointer, nor is it supposed to replace the default Logger.

You can find out more information about FirebugLogger here. But there isn’t really much more to it.

Note: You don’t need Firefox to you use Firebug, you can replace it Firebug Lite.

Here are some more screenshots of it running using Firebug Lite in Safari, Internet Explorer 7, and Opera.
FirebugLogger - Safari FirebugLogger IE7 FirebugLogger Opera

Installation

Installing FirebugLogger is just like installing any other plugin:
./script/plugin install http://svn.aizatto.com/firebug_logger/trunk/

Or if your project is in subversion you can add the -x flag.

License

FirebugLogger is released under the MIT license.

Comments (5)

Experimenting with Haml

HAML has made a bit of noise in the Rails scene with its approach to templating. Just have a look further down.

Embedded Ruby Code (.rhtml):

Haml (.haml):

From the HAML site:

Haml is based on one primary principal. Markup should be beautiful.

Now beautiful is subjective, and it takes a while to get used to HAML actually. Took me a while to get used to Ruby code as well.

Sass is also bundled with Haml. While Haml is the counterpart for the templating, Sass is the counterpart for stylesheets.

I understood why both Haml and Sass were created, and now I wouldn’t recommend them per se but its nice to have options. I am replacing pieces of my embedded ruby code to Haml, but I haven’ t moved a project 100% over. (I wonder if its even possible?). Its nice in that it removes alot of redundancy from your templates, and it sort of beautifies the code by removing all the sharp angly brackets, so you can’t cut yourself writing (X)HTML!

Now I won’t cover Sass as well, I don’t really use it. Faster development? Well my stylesheets haven’t had a need for the problems that Sass was created to solve. Constants in CSS, nexted rules, arithmetic, dynamic colors.

Installing Haml and Sass

Now the installation suggests to install you do it via the normal:
./script/plugin install http://svn.hamptoncatlin.com/haml/tags/stable

But that ain’t cool! It creates a folder called stable in my plugins directory that:

  1. could clash with other plugins because of its vague name
  2. doesn’t give me an idea of what the plugin is for

So I cheated and pulled via subversion:
svn co http://svn.hamptoncatlin.com/haml/tags/stable ./vendor/plugins/haml

Vim Support

So it appears that there is Haml syntax support for Vim, which is a major plus for me, else I might opt not to use it. Syntax highlighting is very important to me, as it makes life a whole lot easier.

Some things are broken though, the regex to select the class or id after the element doesn’t accept underscores or dashes (-). Just pull up the syntax file:
Lines 37 and 38:

syn region  hamlCssClass    start="\.[\-_a-zA-Z0-9]*[=~]" end="$" contains=@rubyTop,hamlRubyCode,hamlRubyHash
syn match   hamlCssClass    "\.[\-_a-zA-Z0-9]*"

Lines 40 and 41:

syn region  hamlCssClass    start="\.[\-_a-zA-Z0-9]*[=~]" end="$" contains=@rubyTop,hamlRubyCode,hamlRubyHash
syn match   hamlCssClass    "\.[\-_a-zA-Z0-9]*"

Easy html2haml Conversion

Actually when you install Haml via the Ruby Gem (gem install haml Terminal) it installs a script called html2haml, which can nicely convert your html to haml :). Beware though when using .rhtml it leaves the <% %> in place, and some other things, but its a good start. Plus you don’t have to do all the work in converting.

Notes

Now the Haml site does have examples, but it doesn’t cover everything.

Embedded Ruby Code:

Equivalent Haml Code:

Important Notes:
Here are the ones that you may not notice instantly.

  • a minus/dash at the start of the line represses the output of that line. (Line 3, 11)
  • Iterating through lists is still possible, but do note that you do not need to specify end. (Line 3). All blocks work this way, including the form_tag.
  • For dynamic ids and classes the arguments to a tag actually accepts a hash, and the values are interpreted. In Line 4 I use the user id and a odd_or_even method. You do not need to quote them in strings. I only quoted them because I wanted the strings ‘user’ and ‘row’ to be present.

General Notes: Here are the things which are expected to happen, but just noted.

  • The indentation is very important, it indicates how the code should be nested.
  • HTML tags are now reduced to %tag. (Lines 1, 2, 4, 5)
  • Only when the text you want displayed is not nested with other elements you can place it after that tag. (Line 1, 5, 6)
  • When nested you have to place it on the next line (Line 8 )
  • Instance variables are still accessible. (Line 3)
  • Similar to CSS, to specify ids and classes you append to the tag either a hash followed by the id (Line 2) or a period followed by the class name(Line 5, 6). Multiple classes can be joined together by appending the classes, ie: %ul.textCenter.horizontalList
  • To output the value of a variable you use the equals operator (=) at the start of a line, a tags id and class declarations (Line 5, 6). It can also be used on its own, it does not create a div (Line 9)
  • Due to the prevalence of the div tag, you can omit it directly (ie: %div) and just specify either an id or a class. (Line 7) to easily divide up my list items.
  • When the equals operator (=) is used on its own, it does not create a div (Line 9)
  • Helper methods are still available and made public to the view. (Line 9, 11)

Comments (1)

Beginning Ruby

It seems that my advocacy and persistence has caused a stir in the local crowd. After the launch of the Malaysia Ruby Brigade, I seem to be picking up more and more followers, and a lot of them are new to Ruby and interested in learning more. So I thought I’d write a post on Beginning Ruby.

I personally think that Ruby has at the same time a high barrier and low barrier of entry. For people new to programming, it’s a very simple language to learn, but if you’re coming from a programming background, there are some perceived conventions that you’ll have to overcome.

I was never much into reading programming books, about 99% of the ones I pick up, I stop after reading 25% of it.

Why you ask? Because I

  • get bored
  • had itchy fingers
  • feel that I know enough and can start hacking on my own

Because of that I’ll go over the interactive introductions to Ruby first over the books, because well I never finished reading them. Because of this I also don’t tend to buy programming books. This list is intentionally kept short so that it provides a nice variety of sources, but does not flood the user with so many possibilities that he doesn’t know where to start.

These are also the projects that I believe are the best resource in learning programming Ruby.

Official Ruby Site

Ruby
The official Ruby site is the place of congregation for all things Ruby related. I personally think its a beautiful site. The site has a lot of resources for you to get started on Ruby.

Here are some recommended readings from their site, that I won’t go over:

Interactive

Try Ruby!

Try Ruby!
Before you even install Ruby, why don’t you Try Ruby! first in your Web browser. No need to worry about how to set up Ruby, or which text editors to use, or even destroying your System in the process. Try Ruby! is an excellent way to well, try it without worrying about the hassles with installing and setting up Ruby. It even comes with a 15 minute tutorial to get you started.

Try Ruby! is actually the interactive ruby (irb) application that comes with Ruby. It allows you to use Ruby from the command line. Nifty for experimenting and debugging code.

So what happens when you’ve grown tired of Try Ruby! and want more? Don’t worry, from the same creator of Try Ruby! comes Hackety Hack: The Coder’s Starter Kit.

Hackety Hack: The Coder’s Starter Kit

Hackety Hack.net

Hackety Hack (or sometimes referred to as HH) is the latest work of why (yes as in his name), bringing with him his colorful personality into a world. At the moment Hacket Hack is still in beta. The goal of Hackety Hack is to act as an introduction into programming, and the choice of language used is Ruby. Sadly at the moment it is only available on Ubuntu, Windows XP and Windows Vista.

In the attempt to lower the barrier of entry into programming, by default, installing Hackety Hack, you also install Ruby making it very easy to new programmers. Interestingly enough the programs you create are also referred to as “hacks“. You don’t use an IDE at all, or a text editor. You code within the Hackety Hack program itself, and it saves your “hacks” into a folder. You write Hackety Hack also uses its own set of libraries to let you do things quickly. For example, parsing feeds, doing web popups, etc.

Hackety Hack comes with its own forums, Talkety Talk, to facilitate discussions within the community. But an even nicer feature is the ability to ’share hacks’. When you register on the forum, you are given a workspace where you can upload your hacks and tables via Hackety Hack. Mine is a bit empty at the moment. Have a look at why’s.

I’ve previously written another post regarding Hackety Hack before.

I personally find Hackety Hack a nice introduction into both programming and Ruby.

Reading

Ruby in Twenty Minutes

Ruby in 20 Minutes

How about trying Ruby in Twenty Minutes. This requires that you have interactive ruby (irb) installed on your computer. But don’t worry, you can try these out in the Try Ruby!. You may not notice but it is actually four pages long. The pagination is located at the title.

Why’s (Poignant) Guide to Ruby

Poignant Guide.net
Another of why’s project is the Why’s (Poignant) Guide to Ruby. I’d consider this a radical book in that it approaches programming in such a different way that it really stand out. It is unique in that sense. It stands out because it is highly illustrated with bizarre comics.

I personally found it difficult to follow the Poignant Guide because it was just so bizarre. Though I do know other people follow it religiously. It may not be for me, but if you are up to it. Do have a read.

It is also available in PDF, for easy reading and printing, a soundtrack, and a purchasable a book.

The book and its materials are also licensed under the Creative Commons By Attribution Share Alike 2.5 License. So you are pretty much free to do whatever you want with the book, as long as the license is the same.

Programming Ruby

Programming Ruby
Programming Ruby (otherwise known as the Pickaxe book) is the original book I followed when learning Ruby (though I never finished reading it). Its a bit outdated (for use with v1.6 of Ruby), but alot of it is still applicable. A newer version is available, though its not available on the Internet. For a currently established Programmer, I found this book to be a great straight forward introduction into Ruby.

Ruby Cookbook

Ruby Cookbook

I find the O’Reilly Ruby Cookbook real handy as it gives practical examples to get things done quickly with Ruby. For example, setting up database connections, graphics manipulation, web services, automating tasks and more.

Other Resources?

Do you think there are other worth while resources that should be here? Please do recommend some then, and I’ll have a look at them.

Note: There are alot and a growing number of Ruby resources out there. This post will be updated as I see fit.

Comments (1)

« Previous entries