# If like smalltalk

**URL:** <https://www.ruby-forum.com/t/if-like-smalltalk/49170>\
**Category:** Ruby\
**Created:** [November 30, 2005, 10:13pm UTC](https://www.ruby-forum.com/t/if-like-smalltalk/49170 "2005-11-30T22:13:54Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![jules](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/41988e/32.png) [@jules](https://www.ruby-forum.com/u/jules)\
**Post date:** [November 30, 2005, 10:13pm UTC](https://www.ruby-forum.com/t/if-like-smalltalk/49170/1 "2005-11-30T22:13:54Z")

</div>

Hi, I have a question about Ruby if constructs. Why aren’t they like  
smalltalk if’s, where you have a boolean class and two subclasses: true  
and false. They both have these methods: ifTrue and ifFalse. If you use  
a block with a ifTrue on a True object, it will be yielded. If you use  
it on a false object, nothing will happen.

So in ruby code:

true.if\_true do  
#code will be executed  
end

and:  
false.if\_true do  
#code will NOT be executed  
end

and if\_false is available too.

so you can do this too:

(var == ‘a’).if\_true do  
puts ‘var = “a”’  
end

I know it would be difficult to do an if-else thing with this because  
the return value from the block would be the receiver:

(var == ‘a’).if\_true do  
puts ‘var = “a”’  
end.if\_false do  
#code  
end

So is this the reason for if(var == ‘a’) not being syntactic sugar for  
(var == ‘a’).if\_true?

Thanks for answering!

Jules

---

<div class="post-metadata">

**Author:** ![jules](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/41988e/32.png) [@jules](https://www.ruby-forum.com/u/jules)\
**Post date:** [November 30, 2005, 10:23pm UTC](https://www.ruby-forum.com/t/if-like-smalltalk/49170/2 "2005-11-30T22:23:12Z")

</div>

On 11/30/05, Jules J. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Hi, I have a question about Ruby if constructs. Why aren’t they like  
> smalltalk if’s, where you have a boolean class and two subclasses: true  
> and false. They both have these methods: ifTrue and ifFalse. If you use  
> a block with a ifTrue on a True object, it will be yielded. If you use  
> it on a false object, nothing will happen.

They aren’t like Smalltalk ifs because Ruby isn’t Smalltalk. That  
said, people have been able to make things that work remarkably  
similar to the Smalltalk stuff. Do a search in the archives and you  
should find it.

-austin

---

<div class="post-metadata">

**Author:** ![jules](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/41988e/32.png) [@jules](https://www.ruby-forum.com/u/jules)\
**Post date:** [November 30, 2005, 10:35pm UTC](https://www.ruby-forum.com/t/if-like-smalltalk/49170/3 "2005-11-30T22:35:23Z")

</div>

Jules J. wrote:

> end  
> (var == ‘a’).if\_true do  
> end
> 
> So is this the reason for if(var == ‘a’) not being syntactic sugar for  
> (var == ‘a’).if\_true?
> 
> Thanks for answering!
> 
> Jules

What would be the point of creating another smalltalk like language?  
smalltalk is still active as far as I can tell, so if you like  
smalltalk, why not use that instead of ruby? I don’t want the ifs to be  
like smalltalk if. I want standard ifs.

---

<div class="post-metadata">

**Author:** ![jules](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/41988e/32.png) [@jules](https://www.ruby-forum.com/u/jules)\
**Post date:** [December 1, 2005, 1:45am UTC](https://www.ruby-forum.com/t/if-like-smalltalk/49170/4 "2005-12-01T01:45:29Z")

</div>

An interesting difference, though, is that the Smalltalk version  
utilizes the language’s capability to accept multiple blocks in a method  
call – ‘ifTrue: […] ifFalse: […]’. In the Ruby version given,  
it needs to have a (may be) hack to return the value of the original  
condition, from the ‘if’ block.

JS

---

<div class="post-metadata">

**Author:** ![jules](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/41988e/32.png) [@jules](https://www.ruby-forum.com/u/jules)\
**Post date:** [November 30, 2005, 10:35pm UTC](https://www.ruby-forum.com/t/if-like-smalltalk/49170/5 "2005-11-30T22:35:23Z")

</div>

On 11/30/05, Jules J. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> end
> 
> and:  
> false.if\_true do  
> #code will NOT be executed  
> end

Just do this:

– BEGIN –  
class Object  
def if\_true(&block)  
block.call if self  
self  
end

def if\_false(&block)  
block.call unless self  
self  
end  
end  
– END –

> (var == ‘a’).if\_true do  
> puts ‘var = “a”’  
> end.if\_false do  
> #code  
> end

The above modifications to Object will allow this to work as well.

(Although I feel it necessary to add: that particular syntax is just,  
well, horrid. The only possible advantage is variable scoping, and  
even then there’s probably a better way to express what you want.)

–  
Regards,  
John W.

> **[John Wilger](http://johnwilger.com)**
>
> The personal website of John Wilger; Dad, Cyclist, Software Developer

---

<div class="post-metadata">

**Author:** ![jules](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/41988e/32.png) [@jules](https://www.ruby-forum.com/u/jules)\
**Post date:** [December 1, 2005, 2:17am UTC](https://www.ruby-forum.com/t/if-like-smalltalk/49170/6 "2005-12-01T02:17:45Z")

</div>

You can define these methods in TrueClass and FalseClass so you don’t  
polute the Object class.

class TrueClass  
def if\_true? …; end  
def if\_false?..; end  
end

class FalseClass

# the opposite…

end

---

<div class="post-metadata">

**Author:** ![jules](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/41988e/32.png) [@jules](https://www.ruby-forum.com/u/jules)\
**Post date:** [December 1, 2005, 4:39am UTC](https://www.ruby-forum.com/t/if-like-smalltalk/49170/7 "2005-12-01T04:39:01Z")

</div>

On 11/30/05, Laza [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> You can define these methods in TrueClass and FalseClass so you don’t  
> polute the Object class.

I had thought about doing it that way, but then you could only use  
that syntax on actual instances of TrueClass and FalseClass – if  
statements are quite often applied to arbitrary objects since, IIRC,  
everything other than FalseClass and NilClass is the same as true for  
that purpose.

–  
Regards,  
John W.

> **[John Wilger](http://johnwilger.com)**
>
> The personal website of John Wilger; Dad, Cyclist, Software Developer

---

<div class="post-metadata">

**Author:** ![jules](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/41988e/32.png) [@jules](https://www.ruby-forum.com/u/jules)\
**Post date:** [December 1, 2005, 1:05pm UTC](https://www.ruby-forum.com/t/if-like-smalltalk/49170/8 "2005-12-01T13:05:29Z")

</div>

Jules J. a Ã©crit :

> end  
> (var == ‘a’).if\_true do  
> end
> 
> So is this the reason for if(var == ‘a’) not being syntactic sugar for  
> (var == ‘a’).if\_true?
> 
> Thanks for answering!
> 
> Jules

(var == ‘a’).if\_true?  
is parsed as a method call and then needs a method dispatch

if var ==‘a’  
is an unambigous imperative instruction and then already understood at  
parse time

SmallTalk is intellectually pure OO, while Ruby is purely practical OO  
hence the difference. 🙂

–  
Lionel T.

Personal web site: [http://users.skynet.be/lthiry/](http://users.skynet.be/lthiry/)

---

<div class="post-metadata">

**Author:** ![jules](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/41988e/32.png) [@jules](https://www.ruby-forum.com/u/jules)\
**Post date:** [December 2, 2005, 1:37am UTC](https://www.ruby-forum.com/t/if-like-smalltalk/49170/9 "2005-12-02T01:37:46Z")

</div>

I’m not sure if I’m understanding your question, but it’s actually  
easier than it was in Smalltalk.

In Ruby, everything but nil and False evaluates to true. There’s no  
ifTrue: because you don’t need one. You can say if anything then  
anything, and postconditions are supported too.

For example (Pseudo-Smalltalk in parens)

if group.members then group.some\_action end  
(members isNil ifFalse: [some\_action])  
unless group.save then some\_action end  
(group save ifTrue: [someAction]  
some\_action if !group.empty? – postcondition  
(group isEmpty ifFalse: [someAction])

I’m finding it clearer and more terse than Smalltalk, with no added  
limitations
