Skip to content
This repository
Browse code

Fixed ArgumentError: Can't create Binding from C level Proc

  • Loading branch information...
commit 8a222fb70993fbfd3d40cbcb7b9ab3d810051319 1 parent eb295c1
Michael Dvorkin authored
12  lib/awesome_print/core_ext/array.rb
@@ -58,7 +58,17 @@ def grep(pattern, &blk)
58 58
       original_grep(pattern)
59 59
     else
60 60
       original_grep(pattern) do |match|
61  
-        eval("%Q/#{match.to_s.gsub('/', '\/')}/ =~ #{pattern.inspect}", blk.binding)
  61
+        #
  62
+        # The binding can only be used with Ruby-defined methods, therefore
  63
+        # we must rescue potential "ArgumentError: Can't create Binding from
  64
+        # C level Proc" error.
  65
+        #
  66
+        # For example, the following raises ArgumentError since #succ method
  67
+        # is defined in C.
  68
+        #
  69
+        # [ 0, 1, 2, 3, 4 ].grep(1..2, &:succ)
  70
+        #
  71
+        eval("%Q/#{match.to_s.gsub('/', '\/')}/ =~ #{pattern.inspect}", blk.binding) rescue ArgumentError
62 72
         yield match
63 73
       end
64 74
     end
9  spec/formats_spec.rb
@@ -601,6 +601,15 @@ def inspect
601 601
       grepped.ai(:plain => true, :multiline => false).should == '[ "1", "2" ]'
602 602
     end
603 603
 
  604
+    # See https://github.com/michaeldv/awesome_print/issues/85
  605
+    if RUBY_VERSION >= "1.8.7"
  606
+      it "handle array grep when a method is defined in C and thus doesn't have a binding" do
  607
+        arr = (0..6).to_a
  608
+        grepped = arr.grep(1..4, &:succ)
  609
+        grepped.ai(:plain => true, :multiline => false).should == '[ 2, 3, 4, 5 ]'
  610
+      end
  611
+    end
  612
+
604 613
     it "returns value passed as a parameter" do
605 614
       object = rand
606 615
       self.stub!(:puts)

0 notes on commit 8a222fb

Please sign in to comment.
Something went wrong with that request. Please try again.