Class: Object
- Inherits:
- BasicObject
- Defined in:
- lib/rext/object/helpers.rb,
lib/rext/object/metaclass.rb
Instance Method Summary collapse
-
#indifferent_hash ⇒ Object
Returns a hash indifferent to symbols or strings for accessor keys.
-
#meta_def(name, &block) ⇒ Object
(also: #metadef)
Define a singleton method.
-
#meta_eval(string = nil, &block) ⇒ Object
(also: #metaeval)
Evaluate a
string
orblock
in context to this object metaclass. -
#metaclass ⇒ Object
Return the metaclass of this object.
-
#returning(value) {|value| ... } ⇒ Object
Yield and return
value
. -
#try(times = 1, options = {}, &block) ⇒ Object
Retry a block of statements upto a number of times.
Instance Method Details
#indifferent_hash ⇒ 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'
17 18 19 |
# File 'lib/rext/object/helpers.rb', line 17 def indifferent_hash Hash.new { |hash, key| hash[key.to_s] if key.is_a? Symbol } end |
#meta_def(name, &block) ⇒ Object Also known as: metadef
Define a singleton method.
24 25 26 |
# File 'lib/rext/object/metaclass.rb', line 24 def name, &block { define_method name, &block } end |
#meta_eval(string = nil, &block) ⇒ Object Also known as: metaeval
Evaluate a string
or block
in context to this object metaclass.
15 16 17 18 |
# File 'lib/rext/object/metaclass.rb', line 15 def string = nil, &block return .class_eval(string) if string .class_eval &block end |
#metaclass ⇒ Object
Return the metaclass of this object.
7 8 9 |
# File 'lib/rext/object/metaclass.rb', line 7 def class << self; self end end |
#returning(value) {|value| ... } ⇒ Object
Yield and return value
.
Examples
people = []
people << 'tj'
people << 'foo'
returning [] do |people|
people << 'tj'
people << 'foo'
end
36 37 38 39 |
# File 'lib/rext/object/helpers.rb', line 36 def returning value, &block yield value value end |
#try(times = 1, options = {}, &block) ⇒ Object
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
54 55 56 57 58 |
# File 'lib/rext/object/helpers.rb', line 54 def try times = 1, = {}, &block yield rescue [:on] || Exception retry if (times -= 1) > 0 end |