From: Rick DeNatale Date: 2008-04-23T11:57:06+09:00 Subject: Re: Ruby and object paradigm On Tue, Apr 22, 2008 at 10:35 PM, Andres wrote: > Hi ruby list, > > Iīm a smalltalk developer and now i'm beginning with Ruby. I found > words like "puts" on the ruby syntax that i donīt understand. > > The basic object paradigm formula is "receiver + message", an emisor > send a message to a receiver. > > Is puts a message? if the answer is true, then what is the receiver? > and when is "puts" implemented? if the answer is false, then What is > "puts"? puts is a message. The puts method is implemented in the Kernel module which is included in the Object class. In Ruby, in most cases the self pseudo-variable is not required, so puts is equivalent to self.puts At the top-level in ruby there is a singleton object which is bound to self. > A smalltalk code similar to ruby code > >> puts "Hello world" > > is > > Transcript show: 'Hello world' Kernel#puts writes its output to stdout. Other Ruby classes also implement puts, for example IO#puts writes the output to receiver, so you can send puts to, say a file object, which is a better analog to Smalltalk's Transcript.show: > when "Transcript" is a global variable and the receiver of the message > #show: and 'Hello world' is an argument of the message. > > other case is the bifurcation example > > class Integer > def factorial > if self == 0 > return 1 > else > return self * ((self-1).factorial) > end > end > end > > in smalltalk > > factorial > > ^self = 0 ifTrue: [ 1 ] ifFalse: [ self * ((self-1) factorial) ] > > the message #ifTrue:ifFalse: is implemented in the class Boolean and > in his subclasses, and receive two block argmuments, the behavior if > the boolean is true and the behavior if the boolean is false. I donīt > understand like it works in ruby, when is implemented the message "if > else"? This is different in Smalltalk and Ruby. In Ruby control flow is done via compiled code control transfer rather than message send. Of course most Smalltalk compilers don't really implement ifTrue:ifFalse: and similar methods via message sending either, instead they compile such 'messages' to branching byte codes, perhaps with a fallback if the 'receiver' isn't a boolean. There are other areas where Smalltalk implementations 'cheat', for example, sending value to a block often doesn't really send a message, and some Smaltalks have little head scratching puzzles such as a value method in Block which looks like value ^self.value which is only there in case someone does something like aBlock perform: #value I do know a bit about Smalltalk, or at least I used to, I was a founding member, and Secretary of X3J20, the ANSI Smalltalk standards commitee. -- Rick DeNatale My blog on Ruby http://talklikeaduck.denhaven2.com/