From: 0x0dea+redmine@... Date: 2015-06-19T02:25:37+00:00 Subject: [ruby-core:69673] [Ruby trunk - Bug #11283] Block assigned implicitly Issue #11283 has been updated by D.E. Akers. > * Either provide a method with empty proc (`Proc.new`) That is, in fact, exactly what Ruby is doing: ```ruby def build Class.new { define_method :foo, Proc.new } end build{ :bar }.new.foo # => :bar ``` `Proc.new`, called without an explicit block, will instead attempt to use the one that was passed to the surrounding method: ```ruby def foo Proc.new.call 1 end foo { |x| x + 1 } # => 2 ``` Note well that methods need not explicitly declare that they take a block. This double whammy of implicit behavior is certainly "astonishing" the first time you encounter it, but it all hangs together in the final analysis. > * Or call `SyntaxError` as a strict way to ask for the programmer's intention This is a semantic rather than syntactic concern, and Ruby does warn against it. That it doesn't raise an `ArgumentError` as it does for standalone `Proc.new` is indeed unexpected, and may well be a bug. ---------------------------------------- Bug #11283: Block assigned implicitly https://bugs.ruby-lang.org/issues/11283#change-53033 * Author: Andrew Kozin * Status: Open * Priority: Normal * Assignee: * ruby -v: 1.9.3, 2.0, 2.1, 2.2, ruby-head * Backport: 2.0.0: UNKNOWN, 2.1: UNKNOWN, 2.2: UNKNOWN ---------------------------------------- That is how it works: module Test def self.build(&block) klass = Class.new(Object) klass.__send__(:define_method, :foo) klass.__send__(:define_method, :bar) klass end end Tested = Test.build { :foo } # warning: tried to create Proc object without a block # => Tested Tested.new.foo # => :foo Tested.new.bar # => :foo The block is assigned to all calls to `:define_method` via `Object#__send__` implicitly, while it wasn't asked to. The behaviour is tested under MRI 1.9.3, 2.0, 2.1, 2.2, ruby-head. It doesn't occur under rbx-2 and jruby (1.7, 9.0.0.0). For the context look at this thread https://github.com/mbj/mutant/issues/356 -- https://bugs.ruby-lang.org/