Project

General

Profile

Actions

Backport #5807

closed

"define_method" not consistent with "def"

Added by silvioricardoc (Silvio Cordeiro) over 12 years ago. Updated over 12 years ago.

Status:
Rejected
Assignee:
-
[ruby-core:41808]

Description

=begin
Under at least Ruby 1.8.7 and 1.9.1, the following definitions are not equivalent:

def foo1
puts "foo1 called"
end

class << self
define_method(:foo2) do
puts "foo2 called"
end
end

While ((|foo1|)) always works, ((|foo2|)) is considered undefined in some contexts. For example, this method works:

(({
def call_foos
foo1
foo2
end
}))

but ((|Bad.new|)) below would not work, failing because ((|foo2|)) is undefined:

(({
class Bad
def initialize
foo1
foo2
end
end
}))

See attached file with an example code that is even stranger than this.
=end


Files

bad.rb (277 Bytes) bad.rb An example where things don't work. silvioricardoc (Silvio Cordeiro), 12/26/2011 03:28 AM

Updated by nobu (Nobuyoshi Nakada) over 12 years ago

  • Status changed from Open to Rejected

=begin
These two are different things.

def foo1
puts "foo1 called"
end

class << self
define_method(:foo2) do
puts "foo2 called"
end
end

While (({foo1})) is a private instance method of (({Object})), (({foo2})) is a singleton method of (({main})).
=end

Actions

Also available in: Atom PDF

Like0
Like0