Joe MooreJoe Moore
Standup 06/25/2009: return vs. next vs. break vs. yslow vs. cap
edit Posted by Joe Moore on Thursday June 25, 2009 at 01:40PM

Interesting Things

  • You can never return: ... except when you can. From a block, that is. Returning from a block rarely works:

    result = ['one', 'two'].each do |x|
     return x
    end
    => LocalJumpError: unexpected return
    

    But, you can pass next and break arguments, which will allow you to assign return values from the block:

    result = ['one', 'two'].each do |x|
      break(x)
    end
    => "one"
    
    
    result = ['one', 'two'].each do |x|
      next(x)
    end
    => ["one", "two"]
    

You can return from a lambda, though.

  • Check out Google Page Speed, which is like Yahoo's YSlow, only "better."

    Page Speed is an open-source Firefox/Firebug Add-on. Webmasters and web developers can use Page Speed to evaluate the performance of their web pages and to get suggestions on how to improve them.

  • Like chef? Love capistrano? Check out chef-deploy, which "... Uses the same directory layout as capistrano and steals the git remote cached deploy strategy from cap and adapts it to work without cap and under chef."

  • SFTUG FTW! Another successful SF Tracker User Group Meetup on 06/24. Watch for future events on the Meetup site.

Comments

  1. Marc-André Lafortune Marc-André Lafortune on June 27, 2009 at 05:06PM

    Hi Joe! I fear your return blurb is a bit confused. I believe the reason you can't return in your example is because you are not inside a method! You were playing in irb, right? Usually you will be in a method and return will work as you would expect it to.

    I think the following shows better the differences between next, break and return.

    def five_times
      result = 5.times do |i|
        puts "#{i}: #{yield i}"
      end
      puts "Result was #{result}"
      result
    end
    
    def show
      r = five_times do |i|
        next i**2
        raise "We never get here"
      end
      puts "Next: #{r}"
      r = five_times do
        break 41
        raise "We never get here either"
      end
      puts "Break: #{r}"
      r = five_times do
        return 42
        raise "We definitely won't here"
      end
      raise "Nor here because of the return"
    end
    
    puts "Show returns: #{show}"
    

    This will show that next will skip the first raise but the whole loop will run and we'll first see: 0: 0 1: 1 2: 4 3: 9 4: 16 Result was 5 Next: 5 The break will break right out of the loop and return, skipping the "Result was " part, so we'll get only: Break: 41

    The return will return altogether from the context of the block, i.e. the method show, so the last raise will also be skipped.

  2. Marc-André Lafortune Marc-André Lafortune on June 27, 2009 at 05:08PM

    This will show that next will skip the first raise but the whole loop will run and we'll first see:

    0: 0
    1: 1
    2: 4
    3: 9
    4: 16
    Result was 5
    Next: 5
    

    The break will break right out of the loop and return, skipping the "Result was " part, so we'll get only:

    Break: 41
    

    The return will return altogether from the context of the block, i.e. the method show, so the last raise will also be skipped. We'll only get the final:

    Show returns: 42