This is a read-only archive!

Ruby, Rails, etc.

I tried out Ruby on Rails for the first time today. My spare time (what little remains of it) has been divided between playing with my websites, and playing with Ruby. So I figured why not combine the two.

It's somewhat infuriating when I can start from scratch and make something in 15 minutes in Rails that works as well as the huge mess of PHP crap that it took me over a week to write. My previous web host didn't have Ruby, so I was forced to use PHP by necessity. PHP4, of course. Blech. Is there anyone on this planet who uses PHP by choice?

I briefly toyed with the idea of using Smarty. It seems like a nice templating system, with its only weakness being that you have to use PHP to use it.

On a different topic, this week's Ruby Quiz was amusing. The problem is essentially to split a string in every possible way (with varying split length, with some upper bounds on split length), transform the parts, recombine them, and then print or process the result. A recursive solution is very straightforward: split the string into a little left part and a big leftover right part, transform the left part, then recursively process the right part in the same way. I wrote up a solution that's about 20 lines long. But it's slow as heck.

Reading other people's solutions, I noticed a neat thing about recursion in Ruby. There are two different ways of doing this recursively. One way (the slow way and memory-intensive way, the way I did it) is to recurse all the way to the base case (run out of leftovers to process), leaving your transformed bits of string scattered all over your call stack. Then return all the parts back up the call stack putting them together as you go. When you've hit the very top of the stack again, you'll have a huge accumulated list (array) of all the results.

But another way is to pass the partly-processed and unprocessed parts of the string both down the call stack as parameters in your method; each method call eats a bit of the unprocessed part and adds a bit to the processed part and then both are passed along again. The base case is once your unprocessed part is empty. If you also pass a Ruby block along as you recurse, then when you hit the bottom of the call stack, you can yield your accumulated fully-processed result to the block. This way you don't have to collect all the results into an array before returning them. You can yield the results as soon as you compute them, one-by-one. I thought it was neat.

April 25, 2007 @ 3:55 PM PDT
Cateogory: Programming
Tags: PHP, Ruby

4 Comments

gregf
Quoth gregf on April 27, 2007 @ 1:29 AM PDT

In the process of learning ruby and ror myself. Just got through the pickaxe book and into agile web development now. If your stuck using php because of your host you may want to look into cakewalk if you have not done so already. Probably save you a little time getting pages up still. Modeled pretty close to ror in many ways. Just an idea.

gregf
Quoth gregf on April 27, 2007 @ 1:31 AM PDT

Oops, just realized i said cakewalk instead of cakephp. Sorry about the mixup.

Brian
Quoth Brian on April 27, 2007 @ 4:18 AM PDT

I just recently moved hosts for my website to one which does have Ruby, so I think I'm good. We'll see.

I have not looked at agile web development. I see that there's a book about it; is that what you're referring to? Is it worth buying?

gregf
Quoth gregf on April 27, 2007 @ 9:31 AM PDT

To be more specific i was referring to Agile Web Development with Rails. Want to clear that up because i think there are a few other Agile books. Yeah it was worth the money so far to me. Cleared up a bunch of questions i had while starting. Depends how much you already know about Rails. Other than that, It brings you through the process of creating a full e-commerce site building it with rails. Also covers some rails configuration information, and database migrations with rake. Covers sessions as well which I did not really understand how they worked in ror before reading. Both books were worth the $$$ to me.