unbound imagination - a blog about programming

Posts tagged with "programming"

«

Book Review: The Ruby Programming Language

May 19, 2008

This past month, I've had the opportunity to read through O'Reilly's new Ruby book, The Ruby Programming Language; thanks to OCRuby and O'Reilly for providing the book.

Now before I share my opinions on this book, let me give you a quick background about me and my programming history just so you have a better understanding of where I'm coming from. I'm a 21 year old college student studying Information & Computer Science at UCI. I've been programming for about 7 years now: first in PHP, then a bit of Java, and for the past 2 years Ruby. I've worked professional in Rails, done numerous Rails projects working for myself (the most recent which recently launched is UselessHypothetical.com), worked on school projects in Ruby (AI project class, operating systems project class), solved several project euler problems using Ruby, and of course written tons of small Ruby scripts as utilities and for fun. I feel I have a pretty good understanding of the Ruby language ranging from syntax to standard library to those 'gotchas' that most (dare I say all) languages have. Alright, enough with the boring stuff, time to get to the book.

For those of you that don't know: Ruby was created by Yukihiro Matsumoto (a.k.a "Matz"). Fortunately, Matz is also the co-author of this book, which is awesome because well, he probably knows the language better than anyone. I don't know it's because of Matz or if it's just coincidence, but this book is incredibly thorough. In the third chapter, "Datatypes and Objects", I learned quite a bit about the nitty gritty of Ruby's syntax and how some of the built-in classes work. For example, I'd seen this syntax a lot:

   1  %Q{This string is quoted and can have "quotes" without a problem}

But did you know that it doesn't have to be braces that surrounds the string?

   1  %Q^This string is quoted and can have "quotes" without a problem^

Yea, I didn't know that. Also, did you know that there is Ruby syntax for single character codes?

   1  ?a # => 97, the character code for 'a'

Anyway, there were tons of small things like that in this chapter that were new to me including fun facts about how the Strings, Ranges, Hashes, and Symbols work. One of the great things about how the book is structured is how it avoids bombarding you with out-of-context standard library methods and usages; each chapter/section gets to the point and stays on topic.

The later chapters get into more advanced topics such as lambdas, procs, and closures and of course the well-known metaprogramming aspect of Ruby. I didn't get a chance to read each and every paragraph yet but I've skimmed pretty much everything. It's got tons of info jam-packed into all 444 pages and does an amazing job of keeping it all organized and presenting it in a manner that can be easily understood.

Throughout the book, there are sections that include explanations about how something is changing from Ruby 1.8 to Ruby 1.9, which is helpful for anyone intending to switch to Ruby 1.9 when that is completed and becomes mainstream. Unfortunately, these sections aren't marked by any visual indicator, so skimming for them doesn't work too well.

In conclusion, this book is by far the best Ruby language book I've seen yet and extensively covers Ruby and it's mainly features. It's an easy read, organized into logical chapters, and is relatively short in comparison to the Pickaxe book (864 pages) without losing content.

I recommend this book to:

  1. experienced programmers looking to learn Ruby
  2. Ruby programmers who are interested in knowing every-little-thing about Ruby
  3. Ruby programmers who will be switching to Ruby 1.9

Update: The purpose of this post isn't to educate anyone on what this book contains or what it covers but simply to give you a quick insight into how I enjoyed it and how I found it useful.

Current Git Branch in Bash Prompt

April 12, 2008

Earlier today, my roommate and I were enjoying our afternoon watching the TextMate for Rails 2 Peepcode and I noticed that in the bash prompt, the current branch was shown in parenthesis. I thought this was a wonderful idea and thought I'd add it to my own bash prompt.

Update: Some much better alternative methods have been posted in the comments, please take a look below.

Here is the code to do it:

   1  export PS1="\[\033[38m\]\u@\h\[\033[01;34m\] \w \[\033[31m\]\`ruby -e \"print (%x{git branch 2> /dev/null}.grep(/^\*/).first || '').gsub(/^\* (.+)$/, '(\1) ')\"\`\[\033[37m\]$\[\033[00m\] "

This will make your bash prompt look like this:

   1  user@hostname currentDirectory (branch name) $

The important part of the PS1 in the first code snippet is this:

   1  # this goes somewhere in your PS1 (it'll make the branch name red)
   2  \[\033[31m\]\`ruby -e \"print (%x{git branch 2> /dev/null}.grep(/^\*/).first || '').gsub(/^\* (.+)$/, '(\1) ')\"\`\[\033[37m\]

I decided to try to do this with sed and not ruby. I came up with this:

   1  export PS1="...\`git branch 2> /dev/null | grep -e ^* | sed -E  s/^\\\\\\\\\*\ \(.+\)$/\(\\\\\\\\\1\)\ /\`\[\033[37m\]$\[\033[00m\] "
   2  # no, your browser is not having rendering issues
   3  # there seriously are that many backslashes

Other than the disgusting backslashes, it's pretty nice. If you use single quotes on the outside instead of double, you can lose a few backslashes:

   1  export PS1='...`git branch 2> /dev/null | grep -e ^* | sed -E  s/^\\\\\*\ \(.+\)$/\(\\\\\1\)\ /`\[\033[37m\]$\[\033[00m\] '

AI is Hard

March 26, 2008

I just finished a 10-week project course in Artificial Intelligence at my university and I've found that AI is a lot harder than it appears on the second impression. I'll clarify why I said second impression later.

This was the second AI course I've taken, the first was an intro class which turned out to be pretty easy. For this AI project course, we were asked to pick our own topic and make our project proposals and explain how this is considered to be an advanced AI topic from our textbook. My topic was basically a Roomba clone, except it was purely a simulation and no actual hardware was involved. It turned out to perform pretty efficiently compared to the Roomba's demo video. Of course theirs is real and mine is a pure simulation.

Read more...

Getting rid of that extra ActiveRecord fluff

March 15, 2008

On the web app I'm currently working on, I needed to get all the restaurants that a user was associated with either through visiting it or rating it. My original solution is shown below.

   1  from_outings = outings.find(:all, :include => {:restaurant => :ratings}).map {|o| o.restaurant}
   2  from_ratings = ratings.find(:all, :include => :restaurant).map{|r| r.restaurant}
   3  (from_ratings + from_outings).uniq

Here's what's going on here:

  1. It runs a query that will return all the visits (outings) the current user has visited.
  2. It creates an object for every Outing, Restaurant, and Rating returned from that query.
  3. Iterates through each Outing and collects the Restaurant object associated with each (there may be repeats.)
  4. Then it runs a query that gets all the ratings the particular user has made and the restaurants they were for.
  5. It takes the results from the previous step and creates Rating and Restaurant objects for each result returned (even if we have a Rating or Restaurant object for this particular record from the previous step.)
  6. Once again, iterates through the Rating objects and collects the Restaurant from each.
  7. Finally, combines both lists of Restaurant objects and takes out duplicates.

That's 2 queries and tons of unused Outing objects and possible repeated Restaurant and Rating objects.

After writing a custom SQL to just get all the Restaurants that I wanted in 1 query, I was still left with having to load the ratings for each restaurant which results in 1 + N queries which is worse than my 2 queries from above.

Read more...