Skip to content
Browse files

Merge pull request #2173 from ryoqun/lambda-method

Don't change a given proc to lambda when shouldn't
  • Loading branch information...
2 parents 98e4d4b + 76983d5 commit e9162111a909c954c9c8dc2731ee1b96e35d9384 @dbussink dbussink committed Feb 25, 2013
View
8 kernel/common/kernel.rb
@@ -155,14 +155,6 @@ def block_given?
alias_method :iterator?, :block_given?
module_function :iterator?
- def lambda(&prc)
- raise ArgumentError, "block required" unless prc
- prc.lambda_style!
- return prc
- end
-
- module_function :lambda
-
def caller(start=1, exclude_kernel=true)
# The + 1 is to skip this frame
Rubinius.mri_backtrace(start + 1).map do |tup|
View
7 kernel/common/kernel18.rb
@@ -93,6 +93,13 @@ def open(path, *rest, &block)
end
module_function :open
+ def lambda(&prc)
+ raise ArgumentError, "block required" unless prc
+ prc.lambda_style!
+ return prc
+ end
+
+ module_function :lambda
alias_method :proc, :lambda
module_function :proc
View
35 kernel/common/kernel19.rb
@@ -140,12 +140,6 @@ def object_id
raise PrimitiveFailure, "Kernel#object_id primitive failed"
end
- def proc(&prc)
- raise ArgumentError, "block required" unless prc
- return prc
- end
- module_function :proc
-
def open(obj, *rest, &block)
if obj.respond_to?(:to_open)
obj = obj.to_open(*rest)
@@ -177,6 +171,35 @@ def require_relative(name)
end
module_function :require_relative
+ def lambda
+ env = nil
+
+ Rubinius.asm do
+ push_block
+ # assign a pushed block to the above local variable "env"
+ # Note that "env" is indexed at 0.
+ set_local 0
+ end
+
+ raise ArgumentError, "block required" unless env
+
+ prc = Proc.__from_block__(env)
+
+ # Make a proc lambda only when passed an actual block (ie, not using the
+ # "&block" notation), otherwise don't modify it at all.
+ prc.lambda_style! if env.is_a?(Rubinius::BlockEnvironment)
+
+ return prc
+ end
+
+ module_function :lambda
+
+ def proc(&prc)
+ raise ArgumentError, "block required" unless prc
+ return prc
+ end
+ module_function :proc
+
def String(obj)
return obj if obj.kind_of? String
View
2 kernel/common/module.rb
@@ -277,7 +277,7 @@ def define_method(name, meth = undefined, &prc)
be = meth.block.dup
be.change_name name
code = Rubinius::BlockEnvironment::AsMethod.new(be)
- meth = lambda(&meth)
+ meth.lambda_style!
end
when Method
exec = meth.executable
View
1 kernel/common/proc.rb
@@ -22,6 +22,7 @@ def self.new(*args)
Rubinius.asm do
push_block
# assign a pushed block to the above local variable "env"
+ # Note that "env" is indexed at 1, not 0. "args" is indexed at 0.
set_local 1
end
View
16 spec/ruby/core/kernel/lambda_spec.rb
@@ -30,6 +30,14 @@
l.call(1).should == :called
l.call(1, 2).should == :called
end
+
+ it "does nost check the arity when passing a Proc with &" do
+ l = lambda { || :called }
+ p = proc { || :called }
+
+ lambda { l.call(1) }.should raise_error(ArgumentError)
+ lambda { p.call(1) }.should raise_error(ArgumentError)
+ end
end
ruby_version_is "1.9" do
@@ -48,6 +56,14 @@
lambda { l.call }.should raise_error(ArgumentError)
lambda { l.call(1, 2) }.should raise_error(ArgumentError)
end
+
+ it "checks the arity when passing a Proc with &" do
+ l = lambda { || :called }
+ p = proc { || :called }
+
+ lambda { l.call(1) }.should raise_error(ArgumentError)
+ p.call(1).should == :called
+ end
end
it "accepts 0 arguments when used with ||" do
View
1 spec/tags/19/ruby/core/proc/lambda_tags.txt
@@ -1 +0,0 @@
-fails:Proc#lambda? is preserved when passing a Proc with & to the lambda keyword
View
1 spec/tags/20/ruby/core/proc/lambda_tags.txt
@@ -1 +0,0 @@
-fails:Proc#lambda? is preserved when passing a Proc with & to the lambda keyword

0 comments on commit e916211

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