4 ms·
Ruby inline conditionals. They read like English. Short and sweet. if condition? return end if !i_probably_shouldnt? do_something end Becomes
by kendallpark 8y ago
Ruby inline conditionals. They read like English. Short and sweet.
if condition?
return
end
if !i_probably_shouldnt?
do_something
end
Becomes:
return if condition?
do_something unless i_probably_shouldnt?
- deleted 8y ago[deleted]
- derefr 8y agoWhen writing one-off Ruby scripts, I especially like using the single-line unless with Kernel.fail: fail "bad arg" unless dir = ARGV.shift (If you could figure out a way to also stuff that line with a transformation to a Pathname for not-nil results, I'd be in heaven.)
- jitl 8y agoTakes a bit of setup to monkey-patch String, but: class String def to_pn Pathname.new(self) end end fail "bad arg" unless dir = ARGV.shift&.to_pn