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 #------------------------------------------------------------------------------