Couple of things happended tonight: 1. I found the VisualWorks IRC Channel: irc://irc.parcplace.net/smalltalk 2. I started watching James Robertson’s Smalltalk Screencasts I was watching the screencasts so I could brush up on my Smalltalk before I went to beCamp. I will be sending everyone I talk to to Cincom to download the NC version and to check out the screencasts. There was one in particular that caused me to fire up IRB: the second screencast under Basic Introduction.
I already knew that Smalltalk executes message sends on a left-to-right basis. This really isn’t an issue unless you’re dealing with Arithmetic. The example given (which is part of the Introduction Workspace of a new VW install) was:5 + 3 * 4The answer is obviously 17. However, in Smalltalk, as is, the answer returned is 32. This is because Smalltalk will send * 4 to the result of sending + 3 to 5. Clear as mud, eh? Don’t worry if that doesn’t make sense. The interesting bit is next.
Knowing Ruby borrowed from Smalltalk I wanted to know how Ruby would evaluate ‘5 + 3 * 4’ Drum roll please - it returns 17. So I posted my observation to the #smalltalk channel, stating Ruby must do some kind of look-ahead when parsing math. There was a question if those were operators or messages. I didn’t know, exactly, but I did remember from reading David A. Black’s Ruby for Rails book about Ruby’s syntatic sugar. So I tried 5.+(3) and received 8 as my answer; not operators, method calls. And:(5 + 3) == 5.+(3)So I posted to the #smalltalk channel those are method calls. But does:(5 + 3 * 4) == 5.+(3).*(4)
The answer is: No. I received 32 as my answer, the same as from Smalltalk. So Ruby must be doing some kind of Math magic when the expression is written in ‘operator-style’ format.