Hello Ruby Universe

The last two days I jumped into Ruby. It's a programming language. It's pretty easy to use, although I have to get used to it since I went like 3 hours yesterday without being able to do anything! But it's just different.

I wrote a solution to the dining philosophers problem (it shows threading and synchronizing access to data using mutual exclusion in Ruby), which you can find if you go to my downloads and search for the label "ruby". Its output looks like this:

Socrates is thinking...
Plato is dining...
CC is thinking...
DD is thinking...
EE is thinking...

It'll loop indefinitely. The philosophers will decide whether they want to think or dine, and then they'll do it if they can! See, there's 5 philosophers at the table, and only 5 utensils, one between each of them. So, only two can be eating at the same time, but two will only be eating at the same time if they're not right next to each other, and two want to eat at the same time.

Here's another loop. It shows what happens when a philosopher wants to eat but can't:

Socrates is dining...
Plato is thinking...
CC is dining...
DD wants to eat but can't (CC has Utensil <--> EE has None)
EE wants to eat but can't (DD has None <--> Socrates has Utensil)

Download ruby at ruby-lang and a whole bunch of other places if you want to play with it. It's a pretty decent language so far.

blog comments powered by Disqus