Showing posts with label ruby. Show all posts
Showing posts with label ruby. Show all posts

Thursday, February 08, 2007

Ruby Threads

Today, I encountered my first Ruby gripe.

Variables mapped from collection passed into Ruby threads are not independent of one another. If one thread changes the passed variable, all of other threads share the new variable.

Let's suppose I have an Array of items = ["item 1", "item 2", "item 3"]

Here's some code that takes each item in the array and does some processing on it, printing out "finished!" statements after each element is processed.

items.map{|item|
Thread.new(item){
#process with variable execution time goes here
puts "#{item} finished!"
}
}

Let's suppose the map task associated with "item 3" starts last and ends first. Then the output could look like this:

item 3 finished!
item 3 finished!
item 3 finished!

The explanation for this peculiar behavior is detailed in Ruby Central's Pragmatic Programmer's Guide:
"A thread shares all global, instance, and local variables that are in existence at the time the thread starts."

Sunday, January 07, 2007

Dusting Off My Ruby Skills

I just finished writing my first non-trivial Ruby script.

It searches a website (more than a page) for pictures that match given search criteria and downloads the pictures along with meta data.

It's only 61 lines long, and there are a bunch of comments and blank lines. It's not complete yet, but the final version should be less than 100 lines long.

Our Railpad server was up for 288 days before I shut it down yesterday. I haven't coded any significant Ruby code since I worked on Railpad, so this picture grabbing script was a much-needed refresher.

Monday, January 01, 2007

New Year's Resolutions

1. Maintain a sleep schedule
2. Catch up with friends
3. Learn Django
4. Improve Ruby skills
5. _______?

Thursday, December 14, 2006

Installing Ruby on Rails the Easy Way

I created this script to install Ruby on Rails on OS X 10.4. It should also work on Ubuntu or other types of Linux as long as you have Bash.

First, have MySQL and gcc installed.

Then, open the Terminal and use this command:
sudo curl http://students.washington.edu/kgc/ror.sh|bash

After a few minutes you should see something like this:
Select which gem to install for your platform
1. mysql 2.7.1 (mswin32)
2. mysql 2.7 (ruby)
3. mysql 2.6 (ruby)
4. mysql 2.5.1 (ruby)
5. Cancel installation

Select the option highest on the list that has (ruby) in the description. In this case it's 2.

Here's what the script installs:
readline 5.2
ruby 1.8.5
rubygems 0.9.0
rails
fastcgi 2.4.0
ruby-fcgi 0.8.7
pcre 6.7
lighttpd 1.4.11
mysql native bindings

If you have any problems, leave a comment and I'll see what I can do.

If this helped you, please Digg it. That way it's more likely to help others as well.

The script is an updated version of the process found here.

Sunday, October 29, 2006

True Names in the wild!

Yukihiro "Matz" Matsumoto, the guy behind Ruby of Ruby on Rails fame, referenced "True Names" in his talk about Ruby Design Principles.

The Presentation MP3

When he mentions True Names, he's on this slide.