Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

Returned Proc of Symbol#to_proc should raise an ArgumentError when calling #call without receiver parameter #2312

Merged
merged 2 commits into from

2 participants

@kachick
Collaborator

MRI

  • ruby 1.8.7 (2012-10-12 patchlevel 371)
  • ruby 1.9.3p392 (2013-02-22 revision 39386)
:object_id.to_proc.call #=> ArgumentError: no receiver given
:DUMMY.to_proc.call     #=> ArgumentError: no receiver given

Rubinius

  • rubinius 2.0.0.rc1 (1.8.7 aacddf7 yyyy-mm-dd JI)
  • rubinius 2.0.0.rc1 (1.9.3 aacddf7 yyyy-mm-dd JI)
:object_id.to_proc.call #=> 26
:DUMMY.to_proc.call     #=> NoMethodError: undefined method `DUMMY' on nil:NilClass.
@dbussink dbussink commented on the diff
kernel/common/symbol18.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)
@dbussink Owner

Since we always shift here, would it be better to do a nil check against that?

recv = args.shift
raise ArgumentError, "no receiver given" unless recv
recv.__send__(sym, *args)

Or perhaps already extract the recv in the block arguments?

@dbussink Owner

Something like this:

    Proc.new do |recv, *args|
      raise ArgumentError, "no receiver given" unless recv
      recv.__send__(sym, *args)
    end
@kachick Collaborator
kachick added a note

Thank you.

1.

recv = args.shift
raise ArgumentError, "no receiver given" unless recv

I'm afraid when receiver is nil.

nil.tap(&:display)

2.

My firstd idea is |recv, *args| .
But that is modified arity number.

@dbussink Owner

Hmm, the arity argument is right yeah. Probably best to with this approach for now then yeah.

@kachick Collaborator
kachick added a note

Thank you for the catch!

@dbussink Owner

Thank you for having this thought through better than I did when commenting :).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
@dbussink dbussink merged commit 118d7e2 into rubinius:master
@kachick kachick deleted the kachick:fix-symbol-to_proc branch
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
This page is out of date. Refresh to see the latest.
View
5 kernel/common/symbol18.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)
@dbussink Owner

Since we always shift here, would it be better to do a nil check against that?

recv = args.shift
raise ArgumentError, "no receiver given" unless recv
recv.__send__(sym, *args)

Or perhaps already extract the recv in the block arguments?

@dbussink Owner

Something like this:

    Proc.new do |recv, *args|
      raise ArgumentError, "no receiver given" unless recv
      recv.__send__(sym, *args)
    end
@kachick Collaborator
kachick added a note

Thank you.

1.

recv = args.shift
raise ArgumentError, "no receiver given" unless recv

I'm afraid when receiver is nil.

nil.tap(&:display)

2.

My firstd idea is |recv, *args| .
But that is modified arity number.

@dbussink Owner

Hmm, the arity argument is right yeah. Probably best to with this approach for now then yeah.

@kachick Collaborator
kachick added a note

Thank you for the catch!

@dbussink Owner

Thank you for having this thought through better than I did when commenting :).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
+ end
end
end
View
5 kernel/common/symbol19.rb
@@ -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
View
4 spec/ruby/core/symbol/to_proc_spec.rb
@@ -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
Something went wrong with that request. Please try again.