Project

General

Profile

Actions

Feature #7414

closed

Now that const_get supports "Foo::Bar" syntax, so should const_defined?.

Feature #7414: Now that const_get supports "Foo::Bar" syntax, so should const_defined?.

Added by robertgleeson (Robert Gleeson) almost 13 years ago. Updated almost 12 years ago.

Status:
Closed
Target version:
[ruby-core:49770]

Description

class Foo
end
Object.const_get "Object::Foo" => Foo
Object.const_defined? "Object::Foo" => NameError (expected true).

Updated by robertgleeson (Robert Gleeson) almost 13 years ago Actions #1 [ruby-core:49773]

I realise defined? with a constant works just as well but this is about API consistency.
I'd expect const_get & const_defined? to support the same syntax ("Object::Foo").

Updated by mame (Yusuke Endoh) almost 13 years ago Actions #2 [ruby-core:49983]

  • Status changed from Open to Assigned
  • Assignee set to tenderlovemaking (Aaron Patterson)

Aaron, can you fix it easily?

--
Yusuke Endoh

Updated by sferik (Erik Michaels-Ober) almost 13 years ago Actions #3 [ruby-core:50550]

I also noticed this inconsistency. It seems very odd to have different behavior for Module#const_get and Module#const_defined?. I hope it's possible to get this change into 2.0.0. It's been a while since I've written any C but I'd be happy to contribute failing specs if that would be helpful.

Updated by benolee (Ben Holley) almost 13 years ago Actions #4 [ruby-core:50952]

I have been working on a patch, but I have some questions about expected behavior.

In the case when the parent is missing, for example:

# Foo is missing
Object.const_defined?("Foo::Bar")

should it return false? Or raise NameError for "Foo"? Or call Object.const_missing(:Foo)?

Second, in the case when the parent is not a Module or Class, for example:

Foo = Object.new
Object.const_defined?("Foo::Bar")

should it return false? Or raise error "Foo is not a class/module"?

Updated by nobu (Nobuyoshi Nakada) almost 13 years ago Actions #5 [ruby-core:51319]

I vote for false in both cases, like defined? operator.

Updated by ko1 (Koichi Sasada) over 12 years ago Actions #6 [ruby-core:52807]

aaron, could you continue this discussion?

Updated by ko1 (Koichi Sasada) over 12 years ago Actions #7 [ruby-core:52808]

  • Target version changed from 2.0.0 to 2.1.0

Updated by nobu (Nobuyoshi Nakada) almost 12 years ago Actions #8

  • Status changed from Assigned to Closed
  • % Done changed from 0 to 100

This issue was solved with changeset r44194.
Robert, thank you for reporting this issue.
Your contribution to Ruby is greatly appreciated.
May Ruby be with you.


object.c: nested path const_defined?

  • object.c (rb_mod_const_defined): support nested class path as
    well as const_get. [Feature #7414]
Actions

Also available in: PDF Atom