Forum: Ruby Re: Smalltalk's favorite Boolean method

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
87e9a89c53ccf984db792113471c2171?d=identicon&s=25 Kroeger, Simon (ext) (Guest)
on 2006-04-11 15:32
(Received via mailing list)
> 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 topic is locked and can not be replied to.