1
0
Fork 0
mirror of https://github.com/awesome-print/awesome_print synced 2023-03-27 23:22:34 -04:00

Added explicit sort! block since Ruby 1.8.x can't sort arrays of symbols

This commit is contained in:
Mike Dvorkin 2011-05-06 17:23:26 -07:00
parent cef1856d13
commit 2fc756b535
2 changed files with 20 additions and 1 deletions

View file

@ -19,7 +19,7 @@ class Array #:nodoc:
arr = self.__send__(:"original_#{operator.object_id}", *args)
if self.instance_variable_defined?('@__awesome_methods__')
arr.instance_variable_set('@__awesome_methods__', self.instance_variable_get('@__awesome_methods__'))
arr.sort!
arr.sort! { |a, b| a.to_s <=> b.to_s } # Need the block since Ruby 1.8.x can't sort arrays of symbols.
end
arr
end

View file

@ -406,4 +406,23 @@ describe "Methods arrays" do
out = Hello.methods.grep(/^m(\d)$/) { %w(none one)[$1.to_i] }.ai(:plain => true)
out.should == "[\n [0] none() Hello\n [1] one() Hello\n]"
end
# See https://github.com/michaeldv/awesome_print/issues/30 for details.
it "grepping methods and converting them to_sym should work as expected" do
class Hello
private
def him; end
def his
private_methods.grep(/^h..$/) { |n| n.to_sym }
end
def her
private_methods.grep(/^.e.$/) { |n| n.to_sym }
end
end
hello = Hello.new
(hello.send(:his) - hello.send(:her)).sort_by { |x| x.to_s }.should == [ :him, :his ]
end
end