Class: Object

Inherits:
BasicObject
Defined in:
lib/ext/metaclass.rb,
lib/ext/object.rb,
lib/inactive_support/core_ext/blank.rb

Overview

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.unique_methodsObject

Lists methods unique to a class.



8
9
10
# File 'lib/ext/object.rb', line 8

def self.unique_methods
  (public_methods - Object.methods).sort.map(&:to_sym)
end

Instance Method Details

#args_and_opts(*args) ⇒ Object

Returns a list of arguments and an options hash. Source taken from RSpec.



13
14
15
16
# File 'lib/ext/object.rb', line 13

def args_and_opts(*args)
  options = Hash === args.last ? args.pop : {}
  return args, options
end

#blank?Boolean

An object is blank if it’s nil, empty, or a whitespace string. For example, “”, “ ”, nil, [], and {} are blank.

This simplifies

if !address.nil? && !address.empty?

to

if !address.blank?

Returns:

  • (Boolean)


9
10
11
# File 'lib/inactive_support/core_ext/blank.rb', line 9

def blank?
  respond_to?(:empty?) ? empty? : !self
end

#class_def(name, &blk) ⇒ Object

Defines an instance method within a class



14
15
16
# File 'lib/ext/metaclass.rb', line 14

def class_def name, &blk
  class_eval { define_method name, &blk }
end

#meta_def(name, &blk) ⇒ Object

Adds methods to a metaclass



9
10
11
# File 'lib/ext/metaclass.rb', line 9

def meta_def name, &blk
  meta_eval { define_method name, &blk }
end

#meta_eval(&blk) ⇒ Object



6
# File 'lib/ext/metaclass.rb', line 6

def meta_eval(&blk); metaclass.instance_eval(&blk); end

#metaclassObject

The hidden singleton lurks behind everyone



5
# File 'lib/ext/metaclass.rb', line 5

def metaclass; class << self; self; end; end

#unique_methodsObject

Lists methods unique to an instance.



3
4
5
# File 'lib/ext/object.rb', line 3

def unique_methods
  (public_methods - Object.methods).sort.map(&:to_sym)
end