Module: Nakajima::Object::InstanceMethods

Defined in:
lib/nakajima/core_ext/object.rb

Defined Under Namespace

Classes: ProxyReturningMe

Instance Method Summary collapse

Instance Method Details

#instance_exec(*arguments, &block) ⇒ Object



28
29
30
# File 'lib/nakajima/core_ext/object.rb', line 28

def instance_exec(*arguments, &block)
  block.bind(self)[*arguments]
end

#tap(p = nil) ⇒ Object

plucked from raganwald’s andand



47
48
49
50
51
52
53
54
55
56
57
# File 'lib/nakajima/core_ext/object.rb', line 47

def tap(p = nil)
  if block_given?
    yield(self)
    self
  elsif p
    p.to_proc.call(self)
    self
  else
    ProxyReturningMe.new(self)
  end
end

#try(method_id, *args, &block) ⇒ Object



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

def try(method_id, *args, &block)
  respond_to?(method_id) ? send(method_id, *args, &block) : nil
end

#with(hash) ⇒ Object

Like JavaScript, but in Ruby…



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

def with(hash)
  hash.each do |key, value|
    meta_def(key) { hash[key] }
    meta_def("#{key}=") { |v| hash[key] = v }
  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