5 ms·
My personal up until recently underused keyword was (surprise!) "rescue". I'm not sure how this has been escaping me since 07, but I had no idea you can apply
by old-gregg 18y ago
My personal up until recently underused keyword was (surprise!) "rescue".
I'm not sure how this has been escaping me since 07, but I had no idea you can apply it at the end of an expression, i.e. when you're facing nasty functions that insist on trhowing exceptions, instead of
begin
value = function_that_throws
rescue
value = "default"
end
you can just write:
value = function_that_throws rescue "default"
- sc 18y agoCool, sure, but blind rescues are as the crow flies to bugs: use them wisely, or use them _why-ly.
- iamwil 18y agothat's along the lines of using or for nil returns. value = function_that_nils || "default" I've found it makes code more succinct.
- old-gregg 18y agoCareful here: I've seen plenty of "Rubiomatic" bugs that look like this: value = function_that_nils or "default" ... which results in nil assignment to value. Accidental confusion between || and 'or' is very common in Ruby world IMO, almost like famous == vs = in C.
- iamwil 18y agoAgreed. I think one is for statements (or) and the other one is for expressions (||), so they have different operational precedence, even though you could use them interchangeably, as long as you knew what got executed first.