Ruby and object paradigm

A

Andres

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

Gary Wright

Hi ruby list,

I=B4m a smalltalk developer and now i'm beginning with Ruby. I found
words like "puts" on the ruby syntax that i don=B4t 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
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 Wright=
 
J

Justin Collins

Andres said:
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 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
 
R

Rick DeNatale

Hi ruby list,

I=B4m a smalltalk developer and now i'm beginning with Ruby. I found
words like "puts" on the ruby syntax that i don=B4t 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=
 
P

Peña, Botp

From: Andres [mailto:[email protected]]=20
# I=B4m 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=B4t 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
=3D> nil

note, other objects may implem puts too, but you already know that ;)
=20
# factorial
# ^self =3D 0 ifTrue: [ 1 ] ifFalse: [ self * ((self-1) factorial) ]

another way in ruby

059:1> def factorial
060:2> self=3D=3D0 ? 1 : self * (self-1).factorial
061:2> end
=3D> nil

=20
# 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=B4t
# 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
 
A

Albert Schlef

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
 
R

Rick DeNatale

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

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"})
 
A

Andres

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 ;)

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


# 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 ;)

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,994
Messages
2,570,223
Members
46,813
Latest member
lawrwtwinkle111

Latest Threads

Top