Pivotal Labs

Main menu

Skip to primary content
Skip to secondary content
  • About
  • Case Studies
  • Team
    • Executives
    • Locations
      • San Francisco (HQ)
      • Boston
      • Boulder
      • Denver
      • London
      • Los Angeles
      • New York
  • Community
    • Blogs
    • Tech Talks
    • Events
  • Careers
    • Lifestyle
    • Principles & Practices
    • Benefits
    • FAQ
    • Apply
  • Contact
    • Press Room
    • Press Releases
    • In The News
    • Press Kit
  • All
  • Labs
  • Standup
  • Tracker

Standup 06/25/2009: return vs. next vs. break vs. yslow vs. cap

Joe Moore
Thursday, June 25, 2009

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.

  • 0 Shares
  • Share on Facebook
  • Share on Twitter

2 Comments

  1. Marc-André Lafortune says:

    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.

    June 27, 2009 at 5:06 pm

  2. Marc-André Lafortune says:

    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

    June 27, 2009 at 5:08 pm

Add New Comment Cancel reply

Your email address will not be published.

Joe Moore

Joe Moore
New York

Recent Posts

  • How We Use tmux for Remote Pair Programming
  • Integrating Remote Developers: Intuitive, Flexible Video Conferencing
  • Pair Programming Matrix
Subscribe to Joe's Feed

Author Topics

agile (117)
pair programming (6)
remote (4)
remote pair programming (3)
tmux (1)
android (10)
mobile (10)
robolectric (4)
java (4)
c2dm (2)
testing (8)
apple (1)
ipad (1)
pivotal tracker (6)
sf.tug (2)
desert (2)
ruby on rails (13)
javascript (2)
capistrano (1)
palmpre (1)
active record (3)
activerecord (6)
mysql (1)
rubymine (1)
fun (4)
acts_as_fu (1)
rspec (2)
xp (1)
flash (1)
  • About
  • Case Studies
  • Team
  • Community
  • Careers
  • Contact
  • Labs
  • Events

Contact Us

contact@pivotallabs.com
+1 415-77-PIVOT
TwitterLinkedInFacebook

Pivotal Tracker

Tracker is the award-winning agile project management tool that enables real-time collaboration around a shared, prioritized backlog.
Visit pivotaltracker.com >