[Ruby] Catching an error when using class methods (Ryan Davis)

Aaron Patterson aaron at tenderlovemaking.com
Thu Jul 19 09:58:54 PDT 2007


On Thu, Jul 19, 2007 at 12:50:40PM -0700, Ofer Matan wrote:
> Ryan/Evan
> 
> The reason I want to do this is similar to the 2003 post I have a open
> webservices request object from another library that I am caching and
> reusing each in multiple methods:
> 
> request_object.CallTheWebserviceINeed
> 
> The cached object goes stale and raises an error - so I need to
> reinitialize. I'd like to avoid writing again and again
> 
> begin
>   request_object.CallTheWebserviceINeed
> rescue ErrorFromStale
>    <reinitialize request_object>
>    Retry
> end

Is the exception the only way to tell if the object is stale?  Typically
there is some sort of flag on objects like that.

> 
> 
> currently I have something like 
> 
> def request_object 
> 	@request_obj ||= Iniatilize_and_return_request_object
> End
> 
> I am trying to avoid removing the '||' and reinitializing every time and was
> hoping to catch the 'stale' error and only reinitialize then.
> 
> Thoughts ?

If the object had a flag, you could just do something like this:

  def request_object
    @request_obj ||= Iniatilize_request_object
    if @request_object.stale?
      ...
    end
    @request_object
  end

-- 
Aaron Patterson
http://tenderlovemaking.com/


More information about the Ruby mailing list