Project

General

Profile

Actions

Bug #20043

closed

`defined?` checks for method existence but only sometimes

Added by tenderlovemaking (Aaron Patterson) 12 months ago. Updated 5 months ago.

Status:
Closed
Assignee:
-
Target version:
-
ruby -v:
ruby 3.3.0dev (2023-12-05T21:25:34Z master 56eccb350b) [arm64-darwin23]
[ruby-core:115595]

Description

When an expression is passed to defined?, it will sometimes check if a method in a sub-expression is defined and sometimes it won't.

For example:

$ ./miniruby -e'p defined?(a)'
nil
$ ./miniruby -e'p defined?([a])'
nil

In the above case, Ruby will check whether or not the method a is defined, and it returns nil. However, if you use a splat, it will not check:

$ ./miniruby -e'p defined?([*a])'
"expression"

The same thing seems to happen with method parameters:

$ ./miniruby -e'p defined?(itself)'
"method"
$ ./miniruby -e'p defined?(itself(a))'
nil
$ ./miniruby -e'p defined?(itself(*a))'
"method"

Oddly, defined? will check contents of arrays, but won't check contents of hashes:

$ ./miniruby -e'p defined?([[[[a]]]])'
nil
$ ./miniruby -e'p defined?({ a => a })'
"expression"

I think all of the cases that refer to a should check whether or not a is defined regardless of splats or hashes.


Files

Actions

Also available in: Atom PDF

Like1
Like0Like1Like0Like0Like0Like0Like0Like0Like0Like0