Project

General

Profile

Actions

Bug #17726

open

`respond_to_missing?` doesn't raise when called from `defined?`

Added by tenderlovemaking (Aaron Patterson) 2 months ago. Updated 2 months ago.

Status:
Open
Priority:
Normal
Assignee:
-
Target version:
-
ruby -v:
ruby 3.1.0dev (2021-03-16T19:10:11Z master 58660e9434) [x86_64-darwin20]
[ruby-core:102893]

Description

I'm not sure whether or not this is expected behavior, but if respond_to_missing? is called from defined?, it won't raise an exception.

For example:

class Foo
  def respond_to_missing?(*args)
    puts "hi"
    raise
  end
end
foo = Foo.new
p defined?(foo.bar)

If you run this program, the output is this:

$ ruby -v test.rb
ruby 3.1.0dev (2021-03-16T19:10:11Z master 58660e9434) [x86_64-darwin20]
hi
nil

But I would expect this to raise an exception. Is the current behavior the desired behavior?

Thanks!

Updated by jeremyevans0 (Jeremy Evans) 2 months ago

I believe this is expected. defined? swallows exceptions in other cases, such as when a method raises an exception:

class Foo
  def bar(*args)
    puts "hi"
    raise
  end
end
foo = Foo.new
p defined?(foo.bar.to_s)
Actions

Also available in: Atom PDF