3 ms·
An alternative to 'if' that I like but havent seriously used before is <expression>.true? {block}. an example in ruby: (3==3).true?{'Yes'} => "Yes" (4==4).tru
by donpdonp 16y ago
An alternative to 'if' that I like but havent seriously used before is <expression>.true? {block}. an example in ruby:
(3==3).true?{'Yes'} => "Yes"
(4==4).true?{'Yes'} => nil
using this implementation:
class Object
def true?
yield
end
end
class FalseClass
def true?
end
end
class NilClass
def true?
end
end
- Xuzz 16y agoSort of like how Smalltalk replaces 'if' statements with ifTrue: messages?
- danudey 16y agoI'm only familiar with Ruby syntax, and not 100% fluent in it, but it seems to me like this kind of shortcut could make your code much less readable for not a whole lot of gain. I'm curious what the major benefit is over doing it 'the old fashioned way'.