Wednesday, June 13, 2007

Blogging and Ruby

So I did a recent post on this topic on my other blog but discovered a few more things in the past couple of days that I thought were good. My blog was about source code formatting when posting blog entries. Every time I want to do a post that contains some source code it always seems like it can be a pain. There’s no way that I’d want to post a lot if it took me fifteen minutes to manually format some source code in html format just to get some nice snippets. So just by chance I tried copying my code snippets from Eclipse (using Aptana) and pasting them into Pages and then exporting what I had to html format. The great thing was was that when I copy from Eclipse and paste into Pages it appears exactly how it does in Eclipse (color, indentations etc). It worked great and I found myself using Pages like this to past html into the post that I was currently working on. Well the only problem with this is that Pages will also add a style-sheet and add classes to divs and what not. So this wasn’t a ‘perfect’ solution. So then I figured since I’m getting back into Ruby I should write a quick little Ruby script that would convert my exported html document into exactly what I wanted for my blog. My last post was using a quick test of what I had in the beginnings of this process. It ended up being a very small script and it does exactly what I want and quickly. So now for all my blogging that I do on my Mac I’ll be using Pages to write my whole blog, Eclipse to copy-and-paste code, and my lil’ script to get it ready for blogger. I can spell-check in Pages, insert links(like above) and now do my source code so it’s almost perfect.

So now I can spend more time blogging and less time doing manual html labor.

Sunday, June 10, 2007

Passing multiple functions/closures

Code blocks are a very nice feature in Ruby that makes doing simple call backs a breeze. I have seen on the web a couple complaints about this feature though. Some have complained that you can’t pass multiple blocks to a single method at a time. While this is true you can pass multiple closure like constructs to a single method. So lets do some quick code.

Here’s the class that we’ll be using

class A
# This method will just take a simple block
  def test1
    yield if block_given?
# This method is taking a block as a parameter
  def test2(&block)
if block_given?
# This method takes 2 closures
  def test3(func1, func2)

And here’s the different ways of passing blocks/closures

# Here is the basic block
a =
a.test1 { puts
'A' }
a.test2 { puts
'B' }
# Here's how to pass multiple functions using lambdas
function1 = lambda { puts
'C' }
function2 = lambda { puts
'C' }
a.test3(function1, function2)
# Here's another way using Procs (lambdas are usually recommended)
function3 = { puts
'D' }
function4 = { puts
'D' }
a.test3(function3, function4)
# And yet another way
def callback
# If you wanted to use a method from within a class you would do
# = classInstance.method(:methodName)
function5 = method(
function6 = method(
a.test3(function5, function6)

One thing you’ll want to consider is that if you want to use a return statement in any of these then you will only get the expected result from the last two scenarios above. If you place a return statement in the others they will return to the caller whereas the lambda and method ways will return only from the closure.

Saturday, June 9, 2007

Ruby editors?

I usually code in c# so figuring out which editor to use is pretty much a no-brainer. Well with Ruby there is a little bit more of a choice. When I first started looking into Ruby I had been using the RDT plugin for Eclipse which was great but I noticed recently that they haven't been doing much with it. This doesn't mean it's bad or anything, I mean it still works like it did before, I just thought that maybe there were bigger and better things now-days. So my quest began. I remembered RadRails but wanted something that was good for plain Ruby development and Rails development. RadRails was basically for Rails. I looked into just using Emacs, but I wanted something that could debug and do all the fancy things I was used to. There was ActiveState's Komodo IDE which looked really great until I saw that there was a price tag, and a hefty one too for someone like me who was just tinkering. Then I finally stumbled accross TextMate and Aptana's RadRails. These are both really good (TextMate is only really good if you have a Mac though, no Windows/*nix versions). I noticed that everyone on the Rails team uses TextMate, but I've used Eclipse before for Java dev and really liked it so I was kinda stuck.

Yes yes I have the RadRails in there 2x. The story behind this is that when I first started looking into Ruby RadRails was mainly for Rails and that was it. Now the Company Aptana has taken RadRails project AND the RDT project and melded them together. So now you can get both the Aptana + Rails config and it works for plain Ruby or Rails stuff, it also has lots of nice features for css/html/javascript editing.

So I guess the biggest thing for me right now is the price. TextMate is great and everything.. but it's 39 Euros (About $53 US) and Aptana's RadRails is free and is completely focused on Ruby only whereas TextMate is a text editor for lots of things.

Upgrading to Ruby 1.8.6 on OS X

So I've been playing around with Ruby and decided I also wanted to learn the Rails framework while I continue my Ruby learning. I mean Rails is pretty much the big driving force of me learning Ruby so I might as well learn both at the same time. So anyhow I do my Ruby development in OS X which comes with Ruby 1.8.2 by default (OS X 10.4). I wanted to upgrade to the latest stable (1.8.6) but didn't want to spend a lot of time playing around in the shell. I could do it in the shell I suppose, but I'm not that big of a geek anymore :). So here's what I did to get everything up and going..

Step one - I downloaded MacPorts  and installed it (easy enough)

Step two - after MacPorts was installed I opened up a shell and typed in 'sudo port upgrade ruby'

Step three - I downloaded RubyGems and un-compressed it

Step four - another shell and 'sudo ruby setup.rb' from the un-compressed gems folder

Step five - another shell and 'sudo gem install rails --include-dependencies


well.. not so fast.. I did run into a little show-stopper when I tried installing Rails from RubyGems. I kept getting an exception when trying to retrieve any gems. After a little Googling around I found that there were quite a few people that have experienced the gems not working. The problem ended up being the gem cache. When trying to pull a gem from the server it wouln't find it and throw an exception. So to fix it I pulled up a shell and typed in 'gem env', which prints the environment settings of RubyGems, then grabbed the path info ('/opt/local/lib/ruby/gems/1.8'). Once I got into the gem path directory I deleted 'source_cache'. I guess this fixed many peoples issues, I ended up having to re-install RubyGems to get it working. Oh well all is good now though.

Saturday, June 2, 2007

Singletons in Ruby

So I was reading my great-big-book-of-everything-Ruby the other night and figured I'd better post this before I forget.
I've used singletons quite a bit in c#, but have never really used them in any dynamic languages (Python, Ruby) that I've used. I found two different approaches to this and will post them both with a few explanations...

The first (which looks somewhat familiar)

class SingletonClass
  private_class_method :new
  @@singletonClass = nil
    @@singletonClass = new unless @@singletonClass

The first line is just a simple class declaration
Line two (which is a little different if you're coming from another language, like me:) ) makes the 'new' call to this class private so you cannot directly create new instances. "private_class_method makes existing class methods private".
Then we have our method that returns the instance of our singleton class
Line five will set the variable '@@singletonClass' to a new instance of the class unless it has already been set.
Now lastly my most undesired feature in Ruby. The single line "@@singletonClass" returns the variable. In Ruby every method returns the last thing. Since our last line was our variable that is what is returned.

The second (quite a bit different than what I'm used to)

require 'singleton'
class SingletonClass2
  include Singleton

Whoa! that's it? That's all you need to do for the second approach.
The third line ('include Singleton') is taking advantage of a Ruby feature called mixins.

My new Ruby blog!

Finally! I'm gonna start playin' around with Ruby.... again.

I started learning Ruby a little over a year and a half ago. I bought a Ruby book and read through it, but never really did anything with it. My boss came in the other day and announced that he wanted to get back in the coding game, so he was gonna start tinkering with RoR. This kinda triggered me to want to get back into the Ruby stuff again. So I started this blog to post stuff I want to remember as I learn the language.