Weekend trip and Jruby demo

September 5, 2007

This past weekend a large group of us in Bangalore took a trip to Mysore. It’s a city which is about 3 hours outside of Bangalore and was a great experience. We saw several palaces, a few temples and got to see more of the Indian country side outside of the city. The contrast between the overstretched infrastructure of Bangalore to the beautiful green and open country is quite shocking. All in all the trip was great and the sites were beautiful (photos on flickr).

I also had the opportunity to hear Ola Bini give a great intro to Jruby. I’ve toyed around with it a bit in the past few months but Ola’s talk was really enlightening. He was able to get right down to the “why should I care” about the project, and it made quite an impression on all who were in attendance. He was frank about the things he feels that Matz’ Ruby Implementation (MRI) isn’t doing so well (threads, concurrency, c extensions, etc) and made awesome points about how Jruby addresses many of these concerns. Wish I could attend RailsConf Europe to hear more about the things he’s doing with Jruby and Mingle.

I’m heading back home to the states this Friday and I’ve yet to hear where I’ll be staffed, so hopefully something should come up soon.

Advertisements

00004

Lots of cool stuff going on here. Yesterday, we had an awesome presentation about how ThoughtWorks makes fixed-priced projects work. Not just work, but work in an agile manner and achieve great results. I’ve had relatively bad experiences on fixed price projects in the past, but this talk was really inspiring. It basically involved creating a client contingency plan by giving them a “bucket” of free stories. When the client decided that they wanted some new feature that was outside of scope, rather than bickering and fighting about price, scope, etc., the client could simply take a few story points out of their bucket. This made the relationship better and the project smoother. Cool stuff.

Also, yesterday Roy Singham, our founder, showed up in Bangalore and took our Immersion class out to dinner. The food was good, the iPhone ripping began approximately 5 minutes after sitting down, and Roy proposed a chili eating contest. Ola Bini destroyed the competition with 10 chili’s. I ate literally 1/10th of 1 and felt horrendous the rest of the night (pictures forth coming).

Today, we had a quick talk from some of the ThoughtWorks Studios guys. Studios is essentially our product arm. It inspired me to get both CruiseControl.rb and Mingle (two Studios products) installed on my Ubuntu box and I’m pretty happy with each. I feel like they’re neat examples of the power ruby (and jruby) can have on the enterprise level.

Halfway thought Immersion, I’m really happy with the way things are going. We’re doing a trip to Mysore tomorrow where there’s apparently a pretty cool temple.

From time to time, people talk about how dynamic languages aren’t suitable for “real” problems as they are quite a bit slower than static, compiled languages. However, I believe that the combination of dynamic languages’ expressiveness and speed more than make up for the lack of pure speed when compare to static languages. And guess what? Dynamic languages aren’t as slow as you think.

I ran across a perfect example today while reading the ruby mailing list. This post describes the implementation of a spelling correcter (much like the one wordpress is using now as a type this blog). Essentially, this application is given a word and then attempts to verify the correctness of it’s spelling. If the word is spelled incorrectly, it gives back suggestions for the word the user was trying to type. This type of technology is used by Google, among others. This problem seems to be quite daunting and, if one would attempt to solve it, it seems like the use of a static language for pure speed would be needed here. The truth is that this is not the case. Some folks who are much more clever than I am were able to write a 21 line Python script and a similarly sized Ruby script to accomplish this task. And they are fast (the Python script was able to process 10 lines per second, and the darn thing is not optimized at all).

Reading these solutions, I was struck with how completely expressive these languages truly are. Take a look at the post and I’m sure you’ll see what I mean. Reading solutions like this – programmed by obviously brilliant people in these two languages – lead me to believe that dynamic OO languages are the wave of the future.

Coming from a Java background, I’ve found arrays to be very “rigid”. And by rigid I mean they sort of suck. However, Ruby makes arrays flexible and a blast to work with. Let’s show by example rather than have me ranting about it. First, we assume we have an array with 100 random integers between 0 and 99. I would show you how to do this but typing the append symbol breaks wordpress (grr).

1. Summing elements: This is a fairly common task. We’ll use Ruby’s inject method to sum all the items in the array and then print out the sum:

puts my_array.inject(0){|sum,item| sum + item}

2. Double every item: This is a class of problem where we want to preform an operation on every element of the array. Again, this is fairly simple using Ruby’s map method. Think of performing a “mapping” from the first array to the second based on the function in the block. Keep in mind, this will return a new array and will NOT effect the original array. If we want to do a destructive map (change the initial array) we would use map!. This is a common convention in Ruby:

my_array.map{|item| item*2 }

3. Finding all items that meet your criteria: If you want to collect all the values in the array that meet some criteria, we can do this using the (duh) find_all method. Again, this will return an array. The code below finds all items that are multiple’s of three :

my_array.find_all{|item| item % 3 == 0 }

4. Combine techniques: Let’s now say we want to find the sume of all elements in our array that are multiples of 3. Ruby to the rescue! This is very simple because we can chain methods together gracefully in Ruby. Check it out:

my_array.find_all{|item| item % 3 == 0 }.inject(0){|sum,item| sum + item }

5. Sorting: We can sort items in an array quite easily. Below, I will show the standard sort and then a sort based on the negative value of the number. Both are so simple, my head just exploded:

my_array.sort
my_array.sort_by{|item| item*-1}

Bonus!: As a bonus for all of you lovely readers, I will show you how to filter an array of strings based on a pattern. Let’s say you have huge array of strings and you want to collect any string that looks like this: 555-555-5555. Assume that the 5’s could be any digit (we are looking for phone numbers). First, we create a regular expression that expresses this phone number jazz: /\d{3}-\d{3}-\d{4}/ (I’m not going into regular expressions here, but you can google them if you want to know more). Now, we simply use the find_all method discussed earlier combined with Ruby’s slick =~ operator. This will tell us if there is our string matches the regular expression. So, without further ado, here is how you would filter an array to find strings that contain phone numbers:

my_array.find_all{|item| item =~ /\d{3}-\d{3}-\d{4}/ }

Stay tuned, more Ruby goodness to come!

Don’t be shy, check out everything you can do with Ruby’s Array

CSV Manipulation w/ Ruby

March 13, 2007

Lately, I’ve been trying to follow why’s advice by using Ruby at work whenever possible. I love Ruby because it’s very friendly to those who have little (or no) programming background. However, some people feel they have no place to use programming in their jobs. Well, let’s look at the example of processing a csv file (something most of use have done). Suppose we have a file with categories and totals. Assume we want to find unique categories and the sum of the totals for each category.

1. Let’s first install the FasterCSV gem. It’s easier to use and faster (duh) than Ruby’s standard csv library. Install it by doing the following from the command line: gem install fastercsv

2. Ok, so we now have fastercsv installed. Assume our file is called “orig.csv”. Here’s a script that will do what we described above. Take a look at the code and I’ll describe it in detail below.

Code

Description: In a nutshell, we include fastercsv in our code, read through our file and and stick the category-total pairs into a hash and then write out the totals to a new file. The only tricky part is line 11, where we check to see if an entry for the current category exists. If not, we create one with a total of 0, otherwise we do nothing. If you have specific questions, please post a comment and I’ll respond.

Ok, so I know that there are other “getting started with rails” tutorials out there, but I thought I’d take a stab at it. I’m doing this from the “teach a 2 year old” point of view, so I’ll try to walk you through everything. And I’ll try to do it in 10 steps. Now, if you don’t know what Rails is, check out the rails website. Also, there is a solution out there called InstantRails for windows that will allow you to install rails from a single application. However, following the steps below will give you much greater flexibility in the future and you might actually learn something along the way! The goal is to end up with a rails development environment on your Windows box including ruby, rails, mysql (your database) and mogrel (your development web server). Don’t worry if you don’t know what these things are, just follow along. Here we go!

1. Download and install the ruby one-click installer.
2. Go to Start -> Run, type “cmd”. At the command line, type the following: gem update – -system (no space between the dashes, just for clarity’s sake).
3. Still at the command line, type: gem install rails – -include-dependencies (again, no spaces)
4. Again, at the command line type: gem install mongrel – -include-dependencies (do I have to say it again? no spaces)
5. Download and install MySql Server 5.0. Pick any mirror. Uncheck the option for changing the root password during the install. Make sure you check the option to run the service when you finish the installation.
6. Download and install HeidiSQL. This will let you view your MySQL database through a graphical user interface.
7. Create a database for our application. Start HeidiSQL, create a new connection, accept ALL the defaults (no password or anything else) and click connect. Create a new schema (right click on the left hand pane) named: myrailsapp_development. Exit the program.
8. Create your rails application. Go to Start -> Run. Type “cmd” and hit enter. At the command line, type: rails myrailsapp. Now, type: cd myrailsapp. You now have a new rails app and you are inside the directory!
9. Still at the command line type: ruby script/server. Your mongrel server is running!
10. Open up your internet browser and into the address bar type: http://localhost:3000. You’re rolling with rails! Yahoo!

Hope that all worked out for you! For more information, check out the rails website. There are also a ton of getting started tutorials out there and the rails site should point you to a lot of them. Good luck, and enjoy rails!