Skip to content

Commit

Permalink
Merge pull request #2312 from kachick/fix-symbol-to_proc
Browse files Browse the repository at this point in the history
Returned Proc of Symbol#to_proc should raise an ArgumentError when calling #call without receiver parameter
  • Loading branch information
dbussink committed Apr 24, 2013
2 parents d34a579 + e7ea412 commit 118d7e2
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 2 deletions.
5 changes: 4 additions & 1 deletion kernel/common/symbol18.rb
Expand Up @@ -26,6 +26,9 @@ def to_proc
# we leave the symbol in sym and use it in the block. # we leave the symbol in sym and use it in the block.
# #
sym = self 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
end end
5 changes: 4 additions & 1 deletion kernel/common/symbol19.rb
Expand Up @@ -113,6 +113,9 @@ def to_proc
# we leave the symbol in sym and use it in the block. # we leave the symbol in sym and use it in the block.
# #
sym = self 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
end end
4 changes: 4 additions & 0 deletions spec/ruby/core/symbol/to_proc_spec.rb
Expand Up @@ -12,6 +12,10 @@
obj.should_receive(:to_s).and_return("Received #to_s") obj.should_receive(:to_s).and_return("Received #to_s")
:to_s.to_proc.call(obj).should == "Received #to_s" :to_s.to_proc.call(obj).should == "Received #to_s"
end 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 end


describe "Symbol#to_proc" do describe "Symbol#to_proc" do
Expand Down

0 comments on commit 118d7e2

Please sign in to comment.