From be626cfadab6afc67b94d65d3af37c2965819985 Mon Sep 17 00:00:00 2001 From: Kyrylo Silin Date: Tue, 28 Aug 2012 02:08:05 +0300 Subject: [PATCH] Retouch `#aliases` so it returns Array Also, refactor tests for `Method#aliases` and use a neat trick with Sets, when matching two arrays. # Meh. [:a, :b, :c] == [:c, :b, :a] # => false # Yarr! Set.new([:a, :b, :c]) == Set.new([:c, :b, :a]) # => true Last but not least, this commit fixes build errors on different rubies (well, I hope so!). Signed-off-by: Kyrylo Silin --- lib/pry/method.rb | 7 ++++--- test/test_method.rb | 19 +++++++++---------- 2 files changed, 13 insertions(+), 13 deletions(-) diff --git a/lib/pry/method.rb b/lib/pry/method.rb index 09e1e30c..c3312e4c 100644 --- a/lib/pry/method.rb +++ b/lib/pry/method.rb @@ -403,8 +403,9 @@ class Pry source_file == Pry.eval_path end - # @return [Array] All known aliases for the method. For Ruby 1.8 and - # friends returns [Array]. + # @return [Array] 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 @@ -417,7 +418,7 @@ class Pry end.flatten alias_list.delete(name) - alias_list + alias_list.map(&:to_s) end # @return [Boolean] Is the method definitely an alias? diff --git a/test/test_method.rb b/test/test_method.rb index aed67976..f4b895f8 100644 --- a/test/test_method.rb +++ b/test/test_method.rb @@ -1,4 +1,5 @@ require 'helper' +require 'set' describe Pry::Method do it "should use String names for compatibility" do @@ -414,12 +415,9 @@ describe Pry::Method do it 'should be able to find method aliases' do meth = Pry::Method(@class.new.method(:eat)) + aliases = Set.new(meth.aliases) - if Pry::Helpers::BaseHelpers.mri_19? - meth.aliases.should == [:fress, :omnomnom] - else - meth.aliases.sort.map(&:to_sym).should == [:fress, :omnomnom] - end + aliases.should == Set.new(["fress", "omnomnom"]) end it 'should return an empty Array if cannot find aliases' do @@ -429,15 +427,16 @@ describe Pry::Method do 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. + meth.aliases.should.not.include "eat" end - if Pry::Helpers::BaseHelpers.mri_18? || Pry::Helpers::BaseHelpers.mri_19? + 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?)) - meth.aliases.should == [:include?, :member?, :has_key?] + aliases = Set.new(meth.aliases) + + aliases.should == Set.new(["include?", "member?", "has_key?"]) end end