On 11/30/05, Jules Jacobs <julesjacobs / gmail.com> 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.
>
> So in ruby code:
>
> true.if_true do
> #code will be executed
> 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 Wilger
http://johnwilger.com

-----------
Alice came to a fork in the road. "Which road do I take?" she asked.
"Where do you want to go?" responded the Cheshire cat.
"I don't know," Alice answered.
"Then," said the cat, "it doesn't matter."
- Lewis Carrol, Alice in Wonderland