Bug #5914

Calling extend with an anonymous module requires use of parentheses

Added by Mark Somerville over 2 years ago. Updated about 2 years ago.

[ruby-core:42189]
Status:Rejected
Priority:Normal
Assignee:-
Category:-
Target version:-
ruby -v:2.0.0dev Backport:

Description

=begin

Given Ruby 2.0.0dev, 1.9.3 or 1.8.7 and this code:

class Dude
def initialize
extend Module.new do
def hello
puts "Word!"
end
end
end
end

Dude.new.hello

No warning is given, but it does not behave how I expect and throws a NoMethodError.

However, if parentheses are used on the call to extend, things behave as expected:

class Dude
def initialize
extend(Module.new do
def hello
puts "Word!"
end
end)
end
end

Dude.new.hello
=> Word!

I don't understand what is happening in the first case (without parentheses). Is this a bug or a misunderstanding on my part?

=end

noname (855 Bytes) Mark Somerville, 01/20/2012 09:29 PM

History

#1 Updated by Yura Sokolov over 2 years ago

When you call without parentheses, then block do is going to extend
method, but not Module.new

2012/1/20 Mark Somerville mark@scottishclimbs.com

Issue #5914 has been reported by Mark Somerville.


Bug #5914: Calling extend with an anonymous module requires use of
parentheses
https://bugs.ruby-lang.org/issues/5914

Author: Mark Somerville
Status: Open
Priority: Normal
Assignee:
Category:
Target version:
ruby -v: 2.0.0dev

=begin

Given Ruby 2.0.0dev, 1.9.3 or 1.8.7 and this code:

class Dude
def initialize
extend Module.new do
def hello
puts "Word!"
end
end
end
end

Dude.new.hello

No warning is given, but it does not behave how I expect and throws a
NoMethodError.

However, if parentheses are used on the call to extend, things behave as
expected:

class Dude
def initialize
extend(Module.new do
def hello
puts "Word!"
end
end)
end
end

Dude.new.hello
=> Word!

I don't understand what is happening in the first case (without
parentheses). Is this a bug or a misunderstanding on my part?

=end

http://bugs.ruby-lang.org/

#2 Updated by Mark Somerville over 2 years ago

On Fri, Jan 20, 2012 at 08:52:08PM +0900, Юрий Соколов wrote:

When you call without parentheses, then block do is going to extend
method, but not Module.new

Doh! That seems so obvious now that you have pointed it out.

Thanks a lot, it was really confusing me!

#3 Updated by Koichi Sasada about 2 years ago

  • Status changed from Open to Rejected

Also available in: Atom PDF