Write conditionals in smalltalk OO style with Ruby

I recently was studying into the language of smalltalk, and I am amazed at how pure it is as an OO language(yes, purer than Ruby even). In Smalltalk, theres no conditional statements like if…elsif…else statement, and instead you write a boolean object followed by two methods iftrue(chained by else) or iffalse(chained by else). Below is a sample smalltalk style conditional statement(kinda like a number comparison method):

self < 0 
      ifTrue: [-1] 
      ifFalse: [self > 0 ifTrue: [1] ifFalse: [0]]

which is equivalent to this in Ruby:

if self < 0
    return -1
elsif self > 0
    return 1
    return 0

The beauty of smalltalk’s conditionals is that they are more object oriented, as you are actually calling the method ifTrue and ifFalse, rather than writing traditional conditional branches which are more procedural. Most programming languages do not support such elegant OO syntax though, but with Ruby its actually possible. Below is a library from github built to give smalltalk style OO interface for conditional statements in ruby:

What do you think? Do you like this idea, and does it look better?

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.