From: Lionel Thiry Date: 2005-12-01T20:27:32+09:00 Subject: Re: If like smalltalk Jules Jacobs a 辿crit : > 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 > (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 Thiry Personal web site: http://users.skynet.be/lthiry/