1
0
Fork 0
mirror of https://github.com/pry/pry.git synced 2022-11-09 12:35:05 -05:00

Add support of aliases for Method

Fix issue #367 (stat command should display the list of aliases for a
given method).

You can get a list of aliases for a method like this:

  pry(main)> stat Array#map
  Method Information:
  --
  Name: map
  Alias: collect
  Owner: Array
  Visibility: public
  Type: Unbound
  Arity: 0
  Method Signature: map()
  Source Location: Not found.

Note that `Method#aliases` returns an Array of strings on MRI 1.8 and
friends, while on MRI 1.9 it returns an Array of symbols.

Signed-off-by: Kyrylo Silin <kyrylosilin@gmail.com>
This commit is contained in:
Kyrylo Silin 2012-08-26 00:02:39 +03:00
parent 93bc98b72b
commit 905bab4d7c
3 changed files with 65 additions and 0 deletions

View file

@ -16,10 +16,13 @@ class Pry
def process
meth = method_object
aliases = meth.aliases
output.puts unindent <<-EOS
Method Information:
--
Name: #{meth.name}
Alias#{ "es" if aliases.length > 1 }: #{ aliases.any? ? aliases.join(", ") : "None." }
Owner: #{meth.owner ? meth.owner : "Unknown"}
Visibility: #{meth.visibility}
Type: #{meth.is_a?(::Method) ? "Bound" : "Unbound"}

View file

@ -403,6 +403,23 @@ class Pry
source_file == Pry.eval_path
end
# @return [Array<Symbol>] All known aliases for the method. For Ruby 1.8 and
# friends returns [Array<String>].
def aliases
owner = @method.owner
# Avoid using `to_sym` on {Method#name}, which returns a `String`, because
# it won't be garbage collected.
name = @method.name
alias_list = owner.instance_methods.combination(2).select do |pair|
owner.instance_method(pair.first) == owner.instance_method(pair.last) &&
pair.include?(name)
end.flatten
alias_list.delete(name)
alias_list
end
# @return [Boolean] Is the method definitely an alias?
def alias?
name != original_name

View file

@ -397,5 +397,50 @@ describe Pry::Method do
meth.send(:method_name_from_first_line, "def obj_name.x").should == "x"
end
end
describe 'method aliases' do
before do
@class = Class.new {
def eat
end
alias fress eat
alias_method :omnomnom, :fress
def eruct
end
}
end
it 'should be able to find method aliases' do
meth = Pry::Method(@class.new.method(:eat))
if Pry::Helpers::BaseHelpers.mri_19?
meth.aliases.should == [:fress, :omnomnom]
else
meth.aliases.sort.map(&:to_sym).should == [:fress, :omnomnom]
end
end
it 'should return an empty Array if cannot find aliases' do
meth = Pry::Method(@class.new.method(:eruct))
meth.aliases.should.be.empty
end
it 'should not include the own name in the list of aliases' do
meth = Pry::Method(@class.new.method(:eat))
meth.aliases.should.not.include :eat
meth.aliases.should.not.include "eat" # For Ruby 1.8 and friends.
end
if Pry::Helpers::BaseHelpers.mri_18? || Pry::Helpers::BaseHelpers.mri_19?
it 'should be able to find aliases for methods implemented in C' do
meth = Pry::Method(Hash.new.method(:key?))
meth.aliases.should == [:include?, :member?, :has_key?]
end
end
end
end