Permalink
Browse files

Merge pull request #2312 from kachick/fix-symbol-to_proc

Returned Proc of Symbol#to_proc should raise an ArgumentError when calling #call without receiver parameter
  • Loading branch information...
2 parents d34a579 + e7ea412 commit 118d7e24262e60487a269835b8eab4e1b5edab06 @dbussink dbussink committed Apr 24, 2013
Showing with 12 additions and 2 deletions.
  1. +4 −1 kernel/common/symbol18.rb
  2. +4 −1 kernel/common/symbol19.rb
  3. +4 −0 spec/ruby/core/symbol/to_proc_spec.rb
@@ -26,6 +26,9 @@ def to_proc
# we leave the symbol in sym and use it in the block.
#
sym = self
- Proc.new { |*args| args.shift.__send__(sym, *args) }
+ Proc.new do |*args|
+ raise ArgumentError, "no receiver given" if args.empty?
+ args.shift.__send__(sym, *args)
+ end
end
end
@@ -113,6 +113,9 @@ def to_proc
# we leave the symbol in sym and use it in the block.
#
sym = self
- Proc.new { |*args, &b| args.shift.__send__(sym, *args, &b) }
+ Proc.new do |*args, &b|
+ raise ArgumentError, "no receiver given" if args.empty?
+ args.shift.__send__(sym, *args, &b)
+ end
end
end
@@ -12,6 +12,10 @@
obj.should_receive(:to_s).and_return("Received #to_s")
:to_s.to_proc.call(obj).should == "Received #to_s"
end
+
+ it "raises an ArgumentError when calling #call on the Proc without receiver" do
+ lambda { :object_id.to_proc.call }.should raise_error(ArgumentError)
+ end
end
describe "Symbol#to_proc" do

0 comments on commit 118d7e2

Please sign in to comment.