Class: Object

Inherits:
BasicObject
Includes:
InstanceExecHelper
Defined in:
lib/ruby/try.rb,
lib/ruby/blank.rb,
lib/ruby/object.rb,
lib/ruby/instance_exec.rb

Defined Under Namespace

Modules: InstanceExecHelper

Combinators collapse

List Constructors collapse

Instance Method Summary collapse

Instance Method Details

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

Yields ‘self` to a block argument

Examples:

nil.bind{|a| a.nil? }   #=> true
100.bind{|a| a.nil? }   #=> false

Yields:

  • (_self)

Yield Parameters:

  • _self (Object)

    the object that the method was called on



39
40
41
# File 'lib/ruby/object.rb', line 39

def bind
  yield self
end

#blank?Boolean

Always ‘false`. Note that NilClass#blank? is overridden to return `true`

Examples:

false.blank?    #=> false
100.blank?      #=> false

Returns:

  • (Boolean)


60
61
62
# File 'lib/ruby/blank.rb', line 60

def blank?
  false
end

#cons(array = []) ⇒ Array

Prepend the item to the front of a new list

Examples:

1.cons              #=> [1]
1.cons(2.cons)      #=> [1, 2]
1.cons([0, 0, 0])   #=> [1, 0, 0, 0]

Returns:



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

def cons(array = [])
  [self] + array
end

#eigenclassClass

Return the “eigenclass” where singleton methods reside

Returns:

  • (Class)


60
61
62
# File 'lib/ruby/object.rb', line 60

def eigenclass
  class << self; self; end
end

#instance_exec(*args, &block) ⇒ Object



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/ruby/instance_exec.rb', line 8

def instance_exec(*args, &block)
  thread = Thread.current.object_id.abs
  object = object_id.abs
  mname  = "__instance_exec_#{thread}_#{object}"
  InstanceExecHelper.module_eval { define_method(mname, &block) }

  begin
    __send__(mname, *args)
  ensure
    begin
      InstanceExecHelper.module_eval { remove_method(mname) }
    rescue
    end
  end
end

#present?Boolean

Returns:

  • (Boolean)


64
65
66
# File 'lib/ruby/blank.rb', line 64

def present?
  true
end

#snoc(array = []) ⇒ Array

Append the item to rear of a new list

Examples:

1.snoc              #=> [1]
1.snoc(2.snoc)      #=> [2, 1]
1.snoc([0, 0, 0])   #=> [0, 0, 0, 1]

Returns:



26
27
28
# File 'lib/ruby/object.rb', line 26

def snoc(array = [])
  array + [self]
end

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

Yields ‘self` to a side-effect block argument and return `self`

@example:

100.bind{|a| puts "debug: #{a}" }   #=> 100

Yields:

  • (_self)

Yield Parameters:

  • _self (Object)

    the object that the method was called on

Returns:

  • self



49
50
51
52
# File 'lib/ruby/object.rb', line 49

def tap
  yield self
  self
end

#try(*args, &block) ⇒ Object

Sends the arguments to ‘self` or yields `self` (when `self` is non-`nil`). This is overridden by NilClass#try, which always returns `nil`.

Examples:

"non-nil".try(&:length)       #=> 7
nil.try(&:length)             #=> nil

"non-nil".try(:slice, 0, 3)   #=> "non"
nil.try(:slice, 0, 3)         #=> nil


16
17
18
19
20
21
22
# File 'lib/ruby/try.rb', line 16

def try(*args, &block)
  if args.empty?
    yield self
  else
    __send__(*args, &block)
  end
end