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

Revert "Revert "Add support of aliases for Method""

This reverts commit d7ca93c62c.
This commit is contained in:
Conrad Irwin 2012-08-29 01:23:29 -07:00
parent d7ca93c62c
commit 8bf265e19a
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,24 @@ class Pry
source_file == Pry.eval_path
end
# @return [Array<String>] All known aliases for the method.
# @note On Ruby 1.8 this method always returns an empty Array for methods
# implemented in C.
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.map(&:to_s)
end
# @return [Boolean] Is the method definitely an alias?
def alias?
name != original_name

View file

@ -1,4 +1,5 @@
require 'helper'
require 'set'
describe Pry::Method do
it "should use String names for compatibility" do
@ -397,5 +398,48 @@ 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))
aliases = Set.new(meth.aliases)
aliases.should == Set.new(["fress", "omnomnom"])
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"
end
unless Pry::Helpers::BaseHelpers.mri_18?
# Ruby 1.8 doesn't support this feature.
it 'should be able to find aliases for methods implemented in C' do
meth = Pry::Method(Hash.new.method(:key?))
aliases = Set.new(meth.aliases)
aliases.should == Set.new(["include?", "member?", "has_key?"])
end
end
end
end