Archive for the 'Ruby' Category

Flash in the pan

It’s been a while. :) So, I ended up buying a Mac Mini, and last week I upgraded the RAM to 2 gigs so I’m sitting pretty well right now. (With the original 512 megs the Mini had at first, things were dog slow, especially when I tried to run Photoshop or InDesign. But now it’s quite fast. I am happy. :))

In other news, at work I’ve been coding a board game in Flash. It’s effectively my first Flash project ever (years and years ago I edited a company map in Flash, but it was so long ago that I can hardly remember it, and I was only maintaining it, so it doesn’t really count). Flash is smooth. I’m not completely satisfied with ActionScript, but it certainly works well enough, and I’m sure more experience with it will make it better. Overall, my time with Flash has been good and fun.

Finally, I’m hoping to get back into more Ruby/Python/Perl coding before too long. My new job’ll require some XML magic, for which I’ll probably use Python and XSLT. And in my typographical work on the side I’ll be doing a lot with TeX and either Ruby or Python.

All of which is to say, hopefully I’ll start blogging here more often. :)

A stitch in time

At work last week my boss was about to purchase a piece of software for a substantial sum. When he told me about it, I almost laughed at the ludicrous price. “They said it took about $6,000 worth of labor,” he said. Completely outrageous. So I coded up a program in Ruby that does pretty much the same thing (all the parts that matter to us) in just a handful of hours. I hate overpriced software.

Ruby to PHP

I’ve been porting the BYUFHLC to PHP, and it’s coming along pretty well in spite of a number of mini-setbacks. The only tricky part was figuring out how to parse the HTML from the screenscrape. At first I was going to use Troy Wolf’s class_http, but it didn’t seem to have any functions for actually doing anything with the HTML once it was fetched. Then I discovered the new DOM stuff in PHP 5.

Excitedly coding up a few lines to test it, I ran into function-not-found errors. Ten minutes later, I realized that Bluehost’s servers were running PHP 4. (Side note: I’d tried to get PHP running locally on my Mac, but it wasn’t serving the PHP; instead it would just display them as plain text.) I called them up and they said it’d take a 24-hour downtime to migrate my domains over to the PHP 5 server. That was unacceptable.

So, I went hunting around in /etc/httpd/httpd.conf again, and I found a line reading “<IfModule mod_php4.c>”. Changed the ‘4′ to a ‘5′ and it all started working perfectly.

After that, it only took twenty or thirty minutes to convert my Ruby code to PHP, and I’ve got the first set of results working perfectly. But now my stomach is grumbling because I haven’t eaten in a long time, so finishing this up is going to have to wait till tomorrow. I’m glad it’s going to be a fairly easy port. (From here on out it’s smooth sailing, since I’ve worked out all the DOM issues.) Expect another status report tomorrow.

And no, I’m not converting to PHP. :) I love Ruby and will continue to use it whenever possible.

More Rails love

Lately, the need to finish these major projects at work before I switch jobs at the end of August has turned most of my coding time (well, all of it right now) into ASP.NET time. Blech. It’s okay, I suppose, and it could be worse (COBOL, anyone?), but I miss Ruby on Rails. Soon, Ben, soon. :)

In the meantime, the library finally got me Agile Web Development with Rails via ILL. I’ve been waiting a while for it. :) Just reading about Rails makes me happy. (Speaking of which, I must be behind on my Rails blog reading. Maybe I’ll do that for a while today… :))

Another little project I’ll do sometime soon is convert my Perl etext script into Ruby (it takes Project Gutenberg texts and preps them for InDesign by removing extra blank lines, removing newlines, replacing two hyphens with an em-dash, etc.). It can’t be that hard — it’s 15 lines long :) — and it’d be a nice back-to-Ruby project to tide me over until all this work stuff is finished.

Speaking of which, I really, really, really wish I could write this extraction web app in Rails instead of ASP.NET. I could get it done sooooo much faster, and I’d be happier. But the rest of the site is in ASP.NET and I’d have to get the sysadmins to install Rails on the server and it’d probably be too much trouble. ~sigh~ Oh well — only one more month of it, and then it’s goodbye forever to .NET. Period. :)

Hopping on the rails

I’m finally starting to understand Rails! :)

Now that school’s out I don’t have any homework breathing down my neck, so this morning I got up early and decided to write a Rails app that takes a GEDCOM file (using an existing GEDCOM-to-XML parser) and imports it into a Beyond database.

Instead of using the traditional scaffolding (which is the easy way to get started with Rails), I decided to go it on my own, creating the necessary models and controllers and views through “script/generate,” and then link them together. In the process, I’ve learned a lot about how Rails works. The whole MVC thing makes sense now, and I see how I can stitch the pieces together to get what I need.

About the only real difficulties I ran into were from naming fields “key” and “object_id”. I wish there were a document somewhere that told you what field names to avoid. There probably is and I just haven’t found it yet. Anyway, after a while I realized that “object_id” was a Ruby method and rel.object_id was giving me a long funky number because of that and not because something was wrong with the database. So that was nice.

Oh, and I discovered config/routes.rb, which answers a lot of the questions I’d had about URL mapping (i.e., where the “/controller/action” URL actually comes from, and how Rails knows about it). Very cool.

Anyway, I’m really, really glad I’ve gotten past the initial bump in the road. This is where the fun begins. :)

First steps in Javascript

I haven’t been in much of a coding mood lately, thus the scarcity of posts. But I think that’s changing. Today I made my initial foray into Javascript coding. At work we’re porting our genealogy extraction software to the web, so I’m writing some code to manage the thumbnails. Right now I’m not using any extra Javascript libraries (Prototype or Dojo or whatnot), because I want to get a feel for what plain Javascript is like. Then I can actually appreciate the benefits of the other libraries. :)

My first impression is that Javascript is actually pretty fun to code. Easy, too. I should’ve started learning it years ago…

Back to the thumbnails, the next step is to implement a draggable preview. On top of the thumbnail image we overlay a slightly transparent rectangle showing which part of the image is in the viewport. The user can draw a new rectangle on the thumbnail, which then moves the viewport to show that part of the image. Kind of like the rubberband select in fluxiom. I’ve got to figure out how they did that…

Come to think of it, though, I wonder if this thumbnail navigation is even useful. I don’t think I would ever actually use it — the normal navigation via the main viewport would be enough (along with keyboard shortcuts). The thumbnails would mainly be useful for choosing which image shows up.

Well, enough thinking aloud. :) It’s been forever since I did anything with Ruby or Rails, and that needs to change. Finals are this coming Wednesday and Thursday, so life’ll be a little hectic, but then I’m done with classes till September. ~sigh of relief~

Update: I forgot to mention how incredibly helpful Firebug has been so far. In fact, I can’t imagine what web coding would be like without it. Seriously.

REXML dabblings

Finally started doing some Ruby coding today. I’m writing a pedigree parser that takes an XML pedigree and outputs it to HTML, and so the natural first step was to see what Ruby’s XML capabilities are. REXML is cool. I’ve done a fair amount of XML work in .NET at my job, and in spite of the differences, I was able to figure the basics out in a very short amount of time. That’s more a statement about how good Ruby and REXML are than it is one about my skill as a coder. :) Anyway, I seem to have heard about other XML libraries in Ruby (which makes me wonder if REXML has hidden deficiencies that I’m unaware of), but I won’t worry about those until I reach REXML’s limits.

And Ruby’s a delight to code in. I think I’ve grokked the block concept (myarray.each { |e| print e }, for example) and iterators and stuff like that. Now it’s just a matter of taking all those ingredients in my head and baking them into some real code. Then the mastery will start to come, and that’s when the real fun starts. I want to start coding DSLs (domain-specific languages) in Ruby, by the way… Mmm.

Can I just say that I love TextMate? Being able to run Ruby code straight from the editor (both via ⌘-R for the separate results window and through Control-Shift-E right in the editor window) is amazingly nice. Sure, other IDEs have that as well, but TextMate is delicious and fun to use.

Ruby Levenshtein

Today I read about the Levenshtein distance algorithm and decided to code it in Ruby.

In information theory, the Levenshtein distance or edit distance between two strings is given by the minimum number of operations needed to transform one string into the other, where an operation is an insertion, deletion, or substitution of a single character.

The Wikipedia article also has the algorithm in pseudocode, which is nice:

int LevenshteinDistance(char str1[1..lenStr1], char str2[1..lenStr2])
   // d is a table with lenStr1+1 rows and lenStr2+1 columns
   declare int d[0..lenStr1, 0..lenStr2]
   // i and j are used to iterate over str1 and str2

   declare int i, j, cost

   for i from 0 to lenStr1
       d[i, 0] := i
   for j from 0 to lenStr2
       d[0, j] := j

   for i from 1 to lenStr1
       for j from 1 to lenStr2
           if str1[i] = str2[j] then cost := 0
                                else cost := 1
           d[i, j] := minimum(
                                d[i-1, j  ] + 1,     // deletion

                                d[i  , j-1] + 1,     // insertion
                                d[i-1, j-1] + cost   // substitution
                            )

   return d[lenStr1, lenStr2]

I’ve almost got it working, but there’s a slight bug right now and I’ll wait to post my code till I get it working. :)

I dream of Ruby

Last night I dreamed a dream. It was this:

family.add(dad, 48, mom, 44, ben, 22)

And so on. family is an object of class Family, and dad, mom, and ben are objects of class Person. The kicker in the dream was that I could parse the arguments (the add method would take *args for flexibility), taking the name of the object as whatever we wanted to add, then checking the next argument to see if it was a number. If so, it would be the age. If it was an object, it would be the next person.

And in my dream it was brilliant. In the light of day, however, I haven’t got a clue what on earth was so great about it. ~sigh~ :)

Something is rotten in Redmond

Here at work we use ASP.NET, doing most of our coding in VB.NET. After a year and half of this, can I just say that I feel like VB.NET is rotting my brain? Sure, it’s a real language, but the more I use it the dumber I feel, like I’m not really programming at all. On the other hand, when I code in Python or Perl or Ruby (not that I’ve done all that much coding in any of these, but I’m starting to), I feel smarter. Is this just an anti-Microsoft emotional reflex, or is there something to it? I don’t know. I doubt it’s actually anti-Microsoft, because C# feels like more of a real language (and I can’t really say anything about it because I haven’t looked at it at all).

Regardless, I do know that I’d much rather code in Ruby or Python than in VB. Life is precious — why punish oneself? Oh, yeah, because that’s what everything runs on at work. ~sigh~ It’d be nice to convert everything over to Ruby on Rails, but alas, it’s probably not going to happen here.