# Ruby and object paradigm

**URL:** <https://www.ruby-forum.com/t/ruby-and-object-paradigm/135558>\
**Category:** Ruby\
**Created:** [April 23, 2008, 4:36am UTC](https://www.ruby-forum.com/t/ruby-and-object-paradigm/135558 "2008-04-23T04:36:07Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![andres](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/a4c791/32.png) [@andres](https://www.ruby-forum.com/u/andres)\
**Post date:** [April 23, 2008, 4:36am UTC](https://www.ruby-forum.com/t/ruby-and-object-paradigm/135558/1 "2008-04-23T04:36:07Z")

</div>

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”?

A smalltalk code similar to ruby code

> > puts “Hello world”

is

Transcript show: ‘Hello world’

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”?

Thanks  
Andrés

---

<div class="post-metadata">

**Author:** ![andres](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/a4c791/32.png) [@andres](https://www.ruby-forum.com/u/andres)\
**Post date:** [April 23, 2008, 4:49am UTC](https://www.ruby-forum.com/t/ruby-and-object-paradigm/135558/2 "2008-04-23T04:49:39Z")

</div>

On Apr 22, 2008, at 10:35 PM, Andres wrote:

> “puts”?  
> puts is a message  
> self is the receiver

If you look at the inheritance structure you’ll  
find the class ‘Object’ at the top. So any  
instance method of Object can be called via  
the implicit use of ‘self’ as the receiver.

The method ‘puts’ is actually defined in the module  
Kernel and Kernel is included as a mixin to Object  
so that instance methods defined in Kernel are  
available to all objects via the inheritance/method  
lookup rules.

Gary W.

---

<div class="post-metadata">

**Author:** ![andres](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/a4c791/32.png) [@andres](https://www.ruby-forum.com/u/andres)\
**Post date:** [April 23, 2008, 4:58am UTC](https://www.ruby-forum.com/t/ruby-and-object-paradigm/135558/3 "2008-04-23T04:58:25Z")

</div>

Andres wrote:

> “puts”?

“puts” is a message and is implemented in the Kernel module:  
[http://ruby-doc.org/core/classes/Kernel.html#M005995](http://ruby-doc.org/core/classes/Kernel.html#M005995)

The implicit receiver is “self”, which at the top level is an object  
called “main”.

> 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”?
> 
> Thanks  
> Andrés

In Ruby, “if” and family are keywords, not messages. I suspect this is  
because Ruby doesn’t have the ability to do “keyworded” methods, making  
something like “if…elseif…else…” difficult.

-Justin

---

<div class="post-metadata">

**Author:** ![andres](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/a4c791/32.png) [@andres](https://www.ruby-forum.com/u/andres)\
**Post date:** [April 23, 2008, 4:59am UTC](https://www.ruby-forum.com/t/ruby-and-object-paradigm/135558/4 "2008-04-23T04:59:00Z")

</div>

On Tue, Apr 22, 2008 at 10:35 PM, Andres [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> “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:

> ```
> return 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/](http://talklikeaduck.denhaven2.com/)

---

<div class="post-metadata">

**Author:** ![andres](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/a4c791/32.png) [@andres](https://www.ruby-forum.com/u/andres)\
**Post date:** [April 23, 2008, 1:03pm UTC](https://www.ruby-forum.com/t/ruby-and-object-paradigm/135558/5 "2008-04-23T13:03:13Z")

</div>

Hi –

On Wed, 23 Apr 2008, Peña, Botp wrote:

> # send a message to a receiver.
> 
> hello  
> 060:2\> self==0 ? 1 : self \* (self-1).factorial  
> 1 current ruby does not support multiple blocks as arguments  
> I would say that Ruby doesn’t support multiple blocks as blocks, but  
> it does support multiple Proc objects as arguments, if you want to go  
> that route.

David

---

<div class="post-metadata">

**Author:** ![andres](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/a4c791/32.png) [@andres](https://www.ruby-forum.com/u/andres)\
**Post date:** [April 23, 2008, 12:51pm UTC](https://www.ruby-forum.com/t/ruby-and-object-paradigm/135558/6 "2008-04-23T12:51:57Z")

</div>

From: Andres [[mailto:removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)]

# I´m a smalltalk developer and now i’m beginning with Ruby. I found

smalltalk is cool.

# words like “puts” on the ruby syntax that i don´t understand.

rtfm 😉

# The basic object paradigm formula is “receiver + message”, an emisor

# send a message to a receiver.

yes. but ruby is nice. it does not “forces” us to do things just  
because of some oo paradigm… remember, not everyone view the  
programming world as receiver.message only; eg, i feel at ease writing  
1+1 instead of 1.+ 1 😉

# is

# Transcript show: ‘Hello world’

if you want a longer way, no problem.

070:0\> Kernel.puts “hello”  
hello  
=\> nil

note, other objects may implem puts too, but you already know that 😉

# factorial

# ^self = 0 ifTrue: [1] ifFalse: [self \* ((self-1) factorial)]

another way in ruby

059:1\> def factorial  
060:2\> self==0 ? 1 : self \* (self-1).factorial  
061:2\> end  
=\> nil

# 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"?

1 current ruby does not support multiple blocks as arguments  
2 current ruby does not have boolean class  
3 this ruby group is composed of many smalltalk hackers (who have now  
become ruby hackers too). you are not alone. search the archives. see  
also the rubinius project.

kind regards -botp

---

<div class="post-metadata">

**Author:** ![andres](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/a4c791/32.png) [@andres](https://www.ruby-forum.com/u/andres)\
**Post date:** [April 23, 2008, 7:47pm UTC](https://www.ruby-forum.com/t/ruby-and-object-paradigm/135558/7 "2008-04-23T19:47:59Z")

</div>

On Wed, Apr 23, 2008 at 10:06 AM, Albert S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> ```
> if self
> true_block.call
> else
> false_block.call
> end
> 
> ```
> 
> end  
> end

Sort of a circular definition though. In the spirit of the Smalltalk  
implementation it would be more like:

class Object  
def if\_else(true\_proc, false\_proc)  
true\_proc.call  
end  
end

class NilClass  
def if\_else(true\_proc, false\_proc)  
false\_proc.call  
end  
end

class FalseClass  
def if\_else(true\_proc, false\_proc)  
false\_proc.call  
end  
end

And the call would need to be something like:

expr.if\_else lambda { “True”}, lambda {“False”}

or Proc.new… if you wanted to say return from one of the legs, i.e.  
the Smalltalk

expr ifTrue:[^“True”] ifFalse:[“Whatever”]

would need to be

expr.if\_else( Proc.new {return “True”}, lambda {“Whatever”})

–  
Rick DeNatale

My blog on Ruby  
[http://talklikeaduck.denhaven2.com/](http://talklikeaduck.denhaven2.com/)

---

<div class="post-metadata">

**Author:** ![andres](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/a4c791/32.png) [@andres](https://www.ruby-forum.com/u/andres)\
**Post date:** [April 23, 2008, 4:06pm UTC](https://www.ruby-forum.com/t/ruby-and-object-paradigm/135558/8 "2008-04-23T16:06:03Z")

</div>

> 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.

BTW, you _can_ implement if/else in Ruby using methods alone.

That smalltalk line can be written in Ruby as…

(self == 0).ifelse lambda { 1 }, lambda { self \* ((self-1).factorial) }

…if you bother to define an ‘ifelse’ method for Object:

class Object  
def ifelse(true\_block, false\_block)  
if self  
true\_block.call  
else  
false\_block.call  
end  
end  
end

---

<div class="post-metadata">

**Author:** ![andres](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/a4c791/32.png) [@andres](https://www.ruby-forum.com/u/andres)\
**Post date:** [April 23, 2008, 11:26pm UTC](https://www.ruby-forum.com/t/ruby-and-object-paradigm/135558/9 "2008-04-23T23:26:09Z")

</div>

On 23 abr, 07:51, Peña, Botp [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> From: Andres [[mailto:removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)]
> 
> # I´m a smalltalk developer and now i’m beginning with Ruby. I found
> 
> smalltalk is cool.
> 
> # words like “puts” on the ruby syntax that i don´t understand.
> 
> rtfm 😉

Yes, you are right. sorry

> # The basic object paradigm formula is “receiver + message”, an emisor
> 
> # send a message to a receiver.
> 
> yes. but ruby is nice. it does not “forces” us to do things just because of some oo paradigm… remember, not everyone view the programming world as receiver.message only; eg, i feel at ease writing 1+1 instead of 1.+ 1 😉

Yes i see ruby like a very nice language, only i have some questions.

I don´t want a language that “forces” us to do things just because of  
some paradigm, but when the language is more pure all is more simple.  
Example, if “if else” is a message i can implement this in other  
classes when i want other behavior, if “if else” is a keyword i can  
´t.  
If there are less keyword on the syntax, then there are less things to  
remember ;).

> note, other objects may implem puts too, but you already know that 😉  
> Ok, the receiver is implicit, perfect. I don´t want write the receiver  
> i only want read the message implementation or redefine the message in  
> other class.

Thanks to all

Regards  
Andrés
