2012-07-22 17:30:05 -04:00
|
|
|
class Pry
|
2012-07-22 17:46:13 -04:00
|
|
|
# @return [Array] Code of the method used when implementing Pry's
|
|
|
|
# __binding__, along with line indication to be used with instance_eval (and
|
|
|
|
# friends).
|
2012-07-22 17:30:05 -04:00
|
|
|
#
|
|
|
|
# @see Object#__binding__
|
2012-12-27 17:25:46 -05:00
|
|
|
BINDING_METHOD_IMPL = [<<-METHOD, __FILE__, __LINE__ + 1]
|
2012-07-22 17:30:05 -04:00
|
|
|
# Get a binding with 'self' set to self, and no locals.
|
|
|
|
#
|
|
|
|
# The default definee is determined by the context in which the
|
|
|
|
# definition is eval'd.
|
|
|
|
#
|
|
|
|
# Please don't call this method directly, see {__binding__}.
|
|
|
|
#
|
|
|
|
# @return [Binding]
|
|
|
|
def __pry__
|
2013-10-20 01:15:20 -04:00
|
|
|
binding
|
2012-07-22 17:30:05 -04:00
|
|
|
end
|
|
|
|
METHOD
|
|
|
|
end
|
|
|
|
|
2011-04-30 03:08:01 -04:00
|
|
|
class Object
|
2012-06-07 02:56:29 -04:00
|
|
|
# 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.
|
|
|
|
#
|
|
|
|
# @param [Object] object the object or binding to pry
|
|
|
|
# (__deprecated__, use `object.pry`)
|
|
|
|
# @param [Hash] hash the options hash
|
|
|
|
# @example With a binding
|
2012-04-01 06:52:50 -04:00
|
|
|
# binding.pry
|
2012-06-07 02:56:29 -04:00
|
|
|
# @example On any object
|
|
|
|
# "dummy".pry
|
|
|
|
# @example With options
|
2012-04-01 06:52:50 -04:00
|
|
|
# def my_method
|
|
|
|
# binding.pry :quiet => true
|
|
|
|
# end
|
|
|
|
# my_method()
|
2012-06-07 02:56:29 -04:00
|
|
|
# @see Pry.start
|
|
|
|
def pry(object=nil, hash={})
|
|
|
|
if object.nil? || Hash === object
|
|
|
|
Pry.start(self, object || {})
|
|
|
|
else
|
|
|
|
Pry.start(object, hash)
|
2012-04-01 04:51:55 -04:00
|
|
|
end
|
2011-04-30 03:08:01 -04:00
|
|
|
end
|
2010-12-30 10:01:11 -05:00
|
|
|
|
2011-04-30 03:08:01 -04:00
|
|
|
# Return a binding object for the receiver.
|
2012-06-06 23:52:18 -04:00
|
|
|
#
|
2012-06-07 02:23:17 -04:00
|
|
|
# The `self` of the binding is set to the current object, and it contains no
|
|
|
|
# local variables.
|
2012-06-06 23:52:18 -04:00
|
|
|
#
|
2012-06-07 02:23:17 -04:00
|
|
|
# The default definee (http://yugui.jp/articles/846) is set such that:
|
2012-06-06 23:52:18 -04:00
|
|
|
#
|
2012-06-07 02:23:17 -04:00
|
|
|
# * 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{ }`)
|
2012-06-06 23:52:18 -04:00
|
|
|
#
|
2012-06-07 02:23:17 -04:00
|
|
|
# Newly created constants, including classes and modules, will also be added
|
|
|
|
# to the default definee.
|
|
|
|
#
|
|
|
|
# @return [Binding]
|
2011-04-30 03:08:01 -04:00
|
|
|
def __binding__
|
2012-07-22 17:30:05 -04:00
|
|
|
# If you ever feel like changing this method, be careful about variables
|
|
|
|
# that you use. They shouldn't be inserted into the binding that will
|
2017-09-12 21:35:00 -04:00
|
|
|
# eventually be returned.
|
2012-07-22 17:30:05 -04:00
|
|
|
|
2012-06-07 02:23:17 -04:00
|
|
|
# When you're cd'd into a class, methods you define should be added to it.
|
2011-04-30 03:08:01 -04:00
|
|
|
if is_a?(Module)
|
2017-09-12 21:35:00 -04:00
|
|
|
# A special case, for JRuby.
|
|
|
|
# Module.new.class_eval("binding") has different behaviour than CRuby,
|
|
|
|
# where this is not needed: class_eval("binding") vs class_eval{binding}.
|
|
|
|
# Using a block works around the difference of behaviour on JRuby.
|
|
|
|
# The scope is clear of local variabless. Don't add any.
|
|
|
|
#
|
|
|
|
# This fixes the following two spec failures, at https://travis-ci.org/pry/pry/jobs/274470002
|
|
|
|
# 1) ./spec/pry_spec.rb:360:in `block in (root)'
|
|
|
|
# 2) ./spec/pry_spec.rb:366:in `block in (root)'
|
|
|
|
return class_eval {binding} if Pry::Helpers::BaseHelpers.jruby? and self.name == nil
|
2012-06-07 02:23:17 -04:00
|
|
|
# class_eval sets both self and the default definee to this class.
|
2017-09-12 21:35:00 -04:00
|
|
|
return class_eval("binding")
|
2010-12-25 08:59:37 -05:00
|
|
|
end
|
|
|
|
|
2012-07-14 08:00:50 -04:00
|
|
|
unless respond_to?(:__pry__)
|
2012-06-07 02:23:17 -04:00
|
|
|
# The easiest way to check whether an object has a working singleton class
|
|
|
|
# is to try and define a method on it. (just checking for the presence of
|
|
|
|
# the singleton class gives false positives for `true` and `false`).
|
2012-07-14 08:00:50 -04:00
|
|
|
# __pry__ is just the closest method we have to hand, and using
|
2012-06-06 23:52:18 -04:00
|
|
|
# it has the nice property that we can memoize this check.
|
2011-04-30 03:08:01 -04:00
|
|
|
begin
|
2012-06-07 02:23:17 -04:00
|
|
|
# instance_eval sets the default definee to the object's singleton class
|
2013-03-02 19:01:55 -05:00
|
|
|
instance_eval(*Pry::BINDING_METHOD_IMPL)
|
2017-09-12 21:38:13 -04:00
|
|
|
|
2012-06-07 02:23:17 -04:00
|
|
|
# If we can't define methods on the Object's singleton_class. Then we fall
|
|
|
|
# back to setting the default definee to be the Object's class. That seems
|
|
|
|
# nicer than having a REPL in which you can't define methods.
|
2013-04-25 04:09:06 -04:00
|
|
|
rescue TypeError, Pry::FrozenObjectException
|
2017-09-12 21:38:13 -04:00
|
|
|
# class_eval sets the default definee to self.class
|
2013-03-02 19:01:55 -05:00
|
|
|
self.class.class_eval(*Pry::BINDING_METHOD_IMPL)
|
2010-12-25 08:59:37 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2012-07-14 08:00:50 -04:00
|
|
|
__pry__
|
2011-04-30 03:08:01 -04:00
|
|
|
end
|
2010-12-25 08:59:37 -05:00
|
|
|
end
|
2013-10-20 00:40:15 -04:00
|
|
|
|
2013-10-24 01:58:26 -04:00
|
|
|
class BasicObject
|
|
|
|
# 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 new
|
|
|
|
# methods defined will be added to the singleton class of the BasicObject.
|
|
|
|
#
|
|
|
|
# @return [Binding]
|
|
|
|
def __binding__
|
|
|
|
# BasicObjects don't have respond_to?, so we just define the method
|
|
|
|
# every time. As they also don't have `.freeze`, this call won't
|
|
|
|
# fail as it can for normal Objects.
|
|
|
|
(class << self; self; end).class_eval <<-EOF, __FILE__, __LINE__ + 1
|
|
|
|
# Get a binding with 'self' set to self, and no locals.
|
|
|
|
#
|
|
|
|
# The default definee is determined by the context in which the
|
|
|
|
# definition is eval'd.
|
|
|
|
#
|
|
|
|
# Please don't call this method directly, see {__binding__}.
|
|
|
|
#
|
|
|
|
# @return [Binding]
|
|
|
|
def __pry__
|
|
|
|
::Kernel.binding
|
|
|
|
end
|
|
|
|
EOF
|
|
|
|
self.__pry__
|
2013-10-20 00:40:15 -04:00
|
|
|
end
|
|
|
|
end
|