Class: Object

Inherits:
BasicObject
Defined in:
lib/elected/core_ext.rb

Overview

Instance Method Summary collapse

Instance Method Details

#local_methods(obj = self) ⇒ Object

Return a list of methods defined locally for a particular object. Useful for seeing what it does whilst losing all the guff that’s implemented by its parents (eg Object).



7
8
9
# File 'lib/elected/core_ext.rb', line 7

def local_methods(obj = self)
  (obj.methods - obj.class.superclass.instance_methods).sort
end

#try(*a, &b) ⇒ Object



11
12
13
# File 'lib/elected/core_ext.rb', line 11

def try(*a, &b)
  try!(*a, &b) if a.empty? || respond_to?(a.first)
end

#try!(*a, &b) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
# File 'lib/elected/core_ext.rb', line 15

def try!(*a, &b)
  if a.empty? && block_given?
    if b.arity.zero?
      instance_eval(&b)
    else
      yield self
    end
  else
    public_send(*a, &b)
  end
end