• Home
  • About

stonean

Feeds:
Posts
Comments

Using ensure to reset a value

April 25, 2009 by stonean

I have a bunch of links I need to build on page, links that are a modification to the current path. I wanted to be able to do my modifications and return the modified path (@updated_path) without changing the original path variable (@path).

This isn’t difficult stuff, the only thing that I had to ponder on for a moment is how to get the updated path and reset it to the original state (@path.dup) in one nice little call.

This worked out nicely:


def updated_path
  @updated_path
ensure
  @updated_path = @path.dup
end

The updated_path method will return the old value and reset it afterward. Nice and simple, I like.

Posted in Ruby | 3 Comments

3 Responses

  1. on April 25, 2009 at 10:50 am Bill Siggelkow

    Hmmm … will @updated_path return an error? I thought ensure would only be fired if an exception was raised.

    If it’s just that it could be nil then you could ||= …

    Oh I think I understand … the first ref to @updated_path should be to the method call updated_path … then the ensure block will be executed if an exception is raised … nice!


    • on April 25, 2009 at 10:57 am stonean

      Actually, the call to updated_path will return the value of @updated_path. The ensure statement will then reset @updated_path to the original value.

      For instance:

      new_path = add_key_value_to_path(‘miles’,'50′)

      As add_key_value_to_path does it’s thing, it updates @updated_path. The last line in add_key_value_to_path is the updated_path call. This will correctly return the value that new_path should get and reset @updated_path. Ready for the next iteration.

      No exceptions are involved. Ensure is being used for it’s order of execution.


      • on April 25, 2009 at 1:33 pm Bill Siggelkow

        Oh I see! The ensure block is run regardless — resetting @updated_path to @path.dup; but the changed @updated_path is returned from the call … very interesting albeit a bit confusing. It saves having to create tmp variables.



Comments are closed.

  • Open Source


    Lockdown (GitHub)
    RuHL (GitHub)

  • Recommend Me

  • twitter: stonean

    • @ubermuda that's good to know, thank you! 16 hours ago
    • @chanmix51 cool, thanks! 16 hours ago
    • In vi, :set list to show hidden characters, to revert, :set nolist Very handy. 18 hours ago
    • Team just did their first PechaKucha [ http://vurl.me/EEP ] session. I love this format and am very impressed with results! 1 day ago
    • Just posted Lauren's new 'Curry Chicken Salad' recipe to http://lowsaltlauren.com . 3 days ago
  • Categories

    classy-inheritance Code Style DataMapper Fusion git Haml Interview Questions jQuery lockdown Merb MysqlUtils Process Quick Tips REST RSpec Ruby RubyOnRails rubytrends Stage thoughts Uncategorized


  • Archives

    • November 2009
    • May 2009
    • April 2009
    • March 2009
    • February 2009
    • January 2009
    • December 2008
    • November 2008
    • October 2008
    • August 2008
    • July 2008
    • June 2008
    • May 2008
    • April 2008
    • March 2008
    • February 2008
    • January 2008
    • December 2007
    • November 2007

Blog at WordPress.com.

Theme: Mistylook by Sadish.