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...
>>>p=Socrates:s=thinking:lh=None:rh=None:to_l=Utensil:to_r=Utensil
Plato is dining...
>>>p=Plato:s=dining:lh=Utensil:rh=Utensil:to_l=None:to_r=None
CC is thinking...
>>>p=CC:s=wants:thinking:lh=None:rh=None:to_l=None:to_r=Utensil
DD is thinking...
>>>p=DD:s=thinking:lh=None:rh=None:to_l=Utensil:to_r=Utensil
EE is thinking...
>>>p=EE:s=wants:thinking:lh=None:rh=None:to_l=Utensil:to_r=Utensil


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...
>>>p=Socrates:s=dining:lh=Utensil:rh=Utensil:to_l=None:to_r=None
Plato is thinking...
>>>p=Plato:s=thinking:lh=None:rh=None:to_l=None:to_r=Utensil
CC is dining...
>>>p=CC:s=dining:lh=Utensil:rh=Utensil:to_l=None:to_r=None
DD wants to eat but can't (CC has Utensil <--> EE has None)
>>>p=DD:s=wants:dining:lh=None:rh=None:to_l=None:to_r=Utensil
EE wants to eat but can't (DD has None <--> Socrates has Utensil)
>>>p=EE:s=wants:dining:lh=None:rh=None:to_l=Utensil:to_r=None


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