From 437810bff542a8e103637e9cdeb1a02f39e436a5 Mon Sep 17 00:00:00 2001 From: Mike Dvorkin Date: Fri, 6 May 2011 14:49:51 -0700 Subject: [PATCH] Fixed array grep for patters with / character --- lib/ap/core_ext/array.rb | 2 +- spec/awesome_print_spec.rb | 7 +++++++ 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/lib/ap/core_ext/array.rb b/lib/ap/core_ext/array.rb index ea8f542..2c260d1 100644 --- a/lib/ap/core_ext/array.rb +++ b/lib/ap/core_ext/array.rb @@ -56,7 +56,7 @@ class Array #:nodoc: arr = unless blk original_grep(pattern) else - original_grep(pattern) { |match| eval("%Q/#{match}/ =~ #{pattern.inspect}", blk.binding); yield match } + original_grep(pattern) { |match| eval("%Q/#{match.to_s.gsub('/', '\/')}/ =~ #{pattern.inspect}", blk.binding); yield match } end if self.instance_variable_defined?('@__awesome_methods__') arr.instance_variable_set('@__awesome_methods__', self.instance_variable_get('@__awesome_methods__')) diff --git a/spec/awesome_print_spec.rb b/spec/awesome_print_spec.rb index e815c7f..21e1ba1 100644 --- a/spec/awesome_print_spec.rb +++ b/spec/awesome_print_spec.rb @@ -462,6 +462,13 @@ EOS end weird.new.ai(:plain => true).should == '' end + + # See https://github.com/michaeldv/awesome_print/issues/35 + it "handle array grep when pattern contains / chapacter" do + hash = { "1/x" => 1, "2//x" => :"2" } + grepped = hash.keys.grep(/^(\d+)\//) { $1 } + grepped.ai(:plain => true, :multiline => false).should == '[ "1", "2" ]' + end end #------------------------------------------------------------------------------