Sha256: d8dd8de3b4a3d8fe3fd4510bcc87bd44c5ee09e6d443a744fd4a941e2e9606a5

Contents?: true

Size: 1.02 KB

Versions: 8

Compression:

Stored size: 1.02 KB

Contents

class Object
  
  ##
  # Returns a hash indifferent to symbols or strings for 
  # accessor keys.
  #
  # === Examples
  #
  #   hash = indifferent_hash
  #   hash['foo'] = 'bar'
  #
  #   hash[:foo]  #=> 'bar'
  #   hash['foo'] #=> 'bar'
  #
  
  def indifferent_hash
    Hash.new { |hash, key| hash[key.to_s] if key.is_a? Symbol }
  end
  
  ##
  # Yield and return +value+.
  #
  # === Examples
  #
  #   people = []
  #   people << 'tj'
  #   people << 'foo'
  #
  #   returning [] do |people|
  #     people << 'tj'
  #     people << 'foo'
  #   end
  #
  
  def returning value, &block
    yield value
    value
  end
  
  ##
  # Retry a _block_ of statements upto a number of _times_.
  # When no error is raised the value returned by _block_ is 
  # simply returned.
  #
  # === Examples
  #
  #   try 3 do
  #     open 'http://vision-media.ca'
  #   end
  #   # => allows 3 tries to fetch the response
  #
  
  def try times = 1, options = {}, &block
    yield
  rescue options[:on] || Exception
    retry if (times -= 1) > 0
  end
  
end

Version data entries

8 entries across 8 versions & 1 rubygems

Version Path
rext-0.7.0 lib/rext/object/helpers.rb
rext-0.6.2 lib/rext/object/helpers.rb
rext-0.6.1 lib/rext/object/helpers.rb
rext-0.6.0 lib/rext/object/helpers.rb
rext-0.5.0 lib/rext/object/helpers.rb
rext-0.4.1 lib/rext/object/helpers.rb
rext-0.4.0 lib/rext/object/helpers.rb
rext-0.3.5 lib/rext/object/helpers.rb