Class: Object

Inherits:
BasicObject
Defined in:
lib/pry/core_extensions.rb

Instance Method Summary collapse

Instance Method Details

#__binding__Binding

Return a binding object for the receiver.

The self of the binding is set to the current object, and it contains no local variables.

The default definee (http://yugui.jp/articles/846) is set such that:

  • If self is a class or module, then new methods created in the binding will be defined in that class or module (as in class Foo; end).
  • If self is a normal object, then new methods created in the binding will be defined on its singleton class (as in class << self; end).
  • If self doesn't have a real singleton class (i.e. it is a Fixnum, Float, Symbol, nil, true, or false), then new methods will be created on the object's class (as in self.class.class_eval{ })

Newly created constants, including classes and modules, will also be added to the default definee.

Returns:

  • (Binding)


47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/pry/core_extensions.rb', line 47

def __binding__
  # When you're cd'd into a class, methods you define should be added to it.
  if is_a?(Module)
    # class_eval sets both self and the default definee to this class.
    return class_eval "binding"
  end

  unless respond_to?(:__pry__)
    binding_impl_method = ["      # Get a binding with 'self' set to self, and no locals.\n      #\n      # The default definee is determined by the context in which the\n      # definition is eval'd.\n      #\n      # Please don't call this method directly, see {__binding__}.\n      #\n      # @return [Binding]\n      def __pry__\n        binding\n      end\n    METHOD\n\n    # The easiest way to check whether an object has a working singleton class\n    # is to try and define a method on it. (just checking for the presence of\n    # the singleton class gives false positives for `true` and `false`).\n    # __pry__ is just the closest method we have to hand, and using\n    # it has the nice property that we can memoize this check.\n    begin\n      # instance_eval sets the default definee to the object's singleton class\n      instance_eval(*binding_impl_method)\n\n    # If we can't define methods on the Object's singleton_class. Then we fall\n    # back to setting the default definee to be the Object's class. That seems\n    # nicer than having a REPL in which you can't define methods.\n    rescue TypeError\n      # class_eval sets the default definee to self.class\n      self.class.class_eval(*binding_impl_method)\n    end\n  end\n\n  __pry__\nend\n", __FILE__, __LINE__ + 1]

#pry(object = nil, hash = {}) ⇒ Object

Start a Pry REPL on self.

If self is a Binding then that will be used to evaluate expressions; otherwise a new binding will be created.

Examples:

With a binding

binding.pry

On any object

"dummy".pry

With options

def my_method
  binding.pry :quiet => true
end
my_method()

Parameters:

  • object (Object) (defaults to: nil)

    the object or binding to pry (deprecated, use object.pry)

  • hash (Hash) (defaults to: {})

    the options hash

See Also:



20
21
22
23
24
25
26
# File 'lib/pry/core_extensions.rb', line 20

def pry(object=nil, hash={})
  if object.nil? || Hash === object
    Pry.start(self, object || {})
  else
    Pry.start(object, hash)
  end
end