Ruby and object paradigm

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

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.

Andres wrote:

“puts”?

“puts” is a message and is implemented in the Kernel module:
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

On Tue, Apr 22, 2008 at 10:35 PM, Andres [email protected]
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/

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

From: Andres [mailto:[email protected]]

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

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

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

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

On Wed, Apr 23, 2008 at 10:06 AM, Albert S. [email protected]
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/

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

On 23 abr, 07:51, Peña, Botp [email protected] wrote:

From: Andres [mailto:[email protected]]

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

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

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