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

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

This commit is contained in:
Mike Dvorkin 2012-09-03 13:06:16 -07:00
parent eb295c106d
commit 8a222fb709
2 changed files with 20 additions and 1 deletions

View file

@ -58,7 +58,17 @@ class Array #:nodoc:
original_grep(pattern)
else
original_grep(pattern) do |match|
eval("%Q/#{match.to_s.gsub('/', '\/')}/ =~ #{pattern.inspect}", blk.binding)
#
# The binding can only be used with Ruby-defined methods, therefore
# we must rescue potential "ArgumentError: Can't create Binding from
# C level Proc" error.
#
# For example, the following raises ArgumentError since #succ method
# is defined in C.
#
# [ 0, 1, 2, 3, 4 ].grep(1..2, &:succ)
#
eval("%Q/#{match.to_s.gsub('/', '\/')}/ =~ #{pattern.inspect}", blk.binding) rescue ArgumentError
yield match
end
end

View file

@ -601,6 +601,15 @@ EOS
grepped.ai(:plain => true, :multiline => false).should == '[ "1", "2" ]'
end
# See https://github.com/michaeldv/awesome_print/issues/85
if RUBY_VERSION >= "1.8.7"
it "handle array grep when a method is defined in C and thus doesn't have a binding" do
arr = (0..6).to_a
grepped = arr.grep(1..4, &:succ)
grepped.ai(:plain => true, :multiline => false).should == '[ 2, 3, 4, 5 ]'
end
end
it "returns value passed as a parameter" do
object = rand
self.stub!(:puts)