Re: Smalltalk's favorite Boolean method

Recently, Ruby “got me” and I was wondering how the
famous ifTrue:ifFalse: could be added to the System.

1 = 2
ifTrue:
[Transcript show: “true!” ]
ifFalse:
[Transcript show: “false!”]
[…]

What about:

def true.ifTrue
yield; self
end

def true.ifFalse
self
end

def false.ifTrue
self
end

def false.ifFalse
yield; self
end

(1==2).
ifTrue {p “true!”}.
ifFalse {p “false!”}

(1!=2).
ifTrue {p “true!”}.
ifFalse {p “false!”}

But note that in ruby not only true is ‘true’
but every object other than nil or false.

cheers

Simon

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs