News for hackers. Stuff that matters.
Re:Ruby’s Most Underused Keyword by old-gregg on Friday February 06, 2009 @07:44PM (#470640)
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"

Re:Ruby’s Most Underused Keyword by sc on Friday February 06, 2009 @08:03PM (#470659)
Cool, sure, but blind rescues are as the crow flies to bugs: use them wisely, or use them _why-ly.
Re:Ruby’s Most Underused Keyword by iamwil on Saturday February 07, 2009 @12:00AM (#470808)
that's along the lines of using or for nil returns.

value = function_that_nils || "default"

I've found it makes code more succinct.

Re:Ruby’s Most Underused Keyword by old-gregg on Saturday February 07, 2009 @12:12AM (#470816)
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 by iamwil on Saturday February 07, 2009 @08:26AM (#471105)
Agreed. 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.
Re:Ruby’s Most Underused Keyword by petercooper on Saturday February 07, 2009 @04:13AM (#470985)
redo, 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 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 by judofyr on Saturday February 07, 2009 @09:15AM (#471126)
You are absolutely right... if you're an interpreter...

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 :-)

Re:Ruby’s Most Underused Keyword by petercooper on Saturday February 07, 2009 @10:08AM (#471156)
Since we're having the discussion in two places, I'll be quicker here.. ;-)

"run this block again" signifies "loop" to me and "loop" signifies "iteration." If I see a block of code being looped, I don't think (as a coder, not as an interpreter) that it's recursive. Perhaps others would, though. If we got in a time machine back to the 80s, I kinda feel that "redo" would turn into "goto".. :)

(Hey, we should be doing this on RubyFlow as well - you could be earning some serious karma :))

Re:Ruby’s Most Underused Keyword by DougBTX on Saturday February 07, 2009 @04:19PM (#471505)
Simplistically, tail call optimisation is an automated way to convert recursion into iteration. You could do that by hand for any tail call recursive function, whether you do so is a function of style and the language you are using.
Re:Ruby’s Most Underused Keyword by jimbokun on Friday February 06, 2009 @06:24PM (#470549)
Using redo for tail recursion in Ruby is an interesting idea, similar to Clojure's recur keyword.
Re:Ruby’s Most Underused Keyword by sunkencity on Saturday February 07, 2009 @03:04AM (#470942)
but ruby doesn't support tail recursion effectively? or is this something new in 1.9. Stack level is not deep enough in 1.8 :P
Re:Ruby’s Most Underused Keyword by judofyr on Saturday February 07, 2009 @09:17AM (#471127)
1.9 supports it, together with some other optimizations, but everything is turned off in 1.9.1. 1.9.2 will be sweet :-)