Ruby’s Most Underused Keyword
rubyrailways.com
Ruby’s Most Underused Keyword
1–10 of 13 posts
Re: Ruby’s Most Underused Keyword
#2Re: Ruby’s Most Underused Keyword
#3I'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"
endyou can just write:
value = function_that_throws rescue "default"
Re: Ruby’s Most Underused Keyword
#4My 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"
Re: Ruby’s Most Underused Keyword
#5My 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"
value = function_that_nils || "default"
I've found it makes code more succinct.
Re: Ruby’s Most Underused Keyword
#6My 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"
that's along the lines of using or for nil returns. value = function_that_nils || "default" I've found it makes code more succinct.
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.
Re: Ruby’s Most Underused Keyword
#7Using redo for tail recursion in Ruby is an interesting idea, similar to Clojure's recur keyword.
Re: Ruby’s Most Underused Keyword
#8 def fib(i)
n, result = 1, 0
while i != -1
i, n, result = i - 1, n + result, n
end
result
end
Cleaner, faster (by 10% on Ruby 1.8) and shorter, but certainly iterative.(Separate to this, the fib routines shown produce incorrect results - my example above maintains this, just for comparison with the original code. fib(10) is 55 not 89 - it's one off in its input.)
Re: Ruby’s Most Underused Keyword
#9Earlier quoted context omitted.
that's along the lines of using or for nil returns. value = function_that_nils || "default" I've found it makes code more succinct.
Careful 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.
Re: Ruby’s Most Underused Keyword
#10redo, in the Fibonacci example, is still iterative - it's presented as a third way, when it's just a cruftier way of being iterative. If you remove the cruft of the lambda and the redo, you end up with: def fib(i) n, result = 1, 0 while i != -1 i, n, result = i - 1, n + result, n end result end Cleaner, faster (by 10% on Ruby 1.8) and shorter, but certainly iterative. (Separate to this, the fib routines shown produce…
From a user's point of view, the redo says "run this block again"; and since the block == the method it's just another way to run acc(). Of course, it's still faking, but I've never claimed anything else :-)