Class: Object

Inherits:
BasicObject
Includes:
InstanceExecHelper
Defined in:
lib/core_ext/object.rb

Defined Under Namespace

Modules: InstanceExecHelper

Instance Method Summary collapse

Instance Method Details

#instance_exec(*args, &block) ⇒ Object



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/core_ext/object.rb', line 29

def instance_exec(*args, &block)
  begin
    old_critical, Thread.critical = Thread.critical, true
    n = 0
    n += 1 while respond_to?(mname="__instance_exec#{n}")
    InstanceExecHelper.module_eval{ define_method(mname, &block) }
  ensure
    Thread.critical = old_critical
  end
  begin
    ret = send(mname, *args)
  ensure
    InstanceExecHelper.module_eval{ remove_method(mname) } rescue nil
  end
  ret
end

#tap {|_self| ... } ⇒ Object

Yields:

  • (_self)

Yield Parameters:

  • _self (Object)

    the object that the method was called on



4
5
6
7
# File 'lib/core_ext/object.rb', line 4

def tap
  yield self
  self
end

#with(hash) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/core_ext/object.rb', line 9

def with(hash)
  hash.each do |key, value|
    meta_def(key) { hash[key] } unless respond_to?(key)
    meta_def("#{key}=") { |v| hash[key] = v } unless respond_to?("#{key}=")
  end
  
  return unless block_given?

  result = yield

  hash.each do |key, value|
    meta_eval { remove_method(key) }
    meta_eval { remove_method("#{key}=") }
  end

  result
end