Feature #7414

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

Added by Robert Gleeson over 1 year ago. Updated 4 months ago.

[ruby-core:49770]
Status:Closed
Priority:Normal
Assignee:Aaron Patterson
Category:-
Target version:2.1.0

Description

class Foo
end
Object.constget "Object::Foo" => Foo
Object.const
defined? "Object::Foo" => NameError (expected true).

Associated revisions

Revision 44194
Added by Nobuyoshi Nakada 4 months ago

object.c: nested path const_defined?

  • object.c (rbmodconstdefined): support nested class path as well as constget. [Feature #7414]

History

#1 Updated by Robert Gleeson over 1 year ago

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

#2 Updated by Yusuke Endoh over 1 year ago

  • Status changed from Open to Assigned
  • Assignee set to Aaron Patterson

Aaron, can you fix it easily?

Yusuke Endoh mame@tsg.ne.jp

#3 Updated by Erik Michaels-Ober over 1 year ago

I also noticed this inconsistency. It seems very odd to have different behavior for Module#constget and Module#constdefined?. 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.

#4 Updated by Ben Holley over 1 year ago

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"?

#5 Updated by Nobuyoshi Nakada over 1 year ago

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

#6 Updated by Koichi Sasada about 1 year ago

aaron, could you continue this discussion?

#7 Updated by Koichi Sasada about 1 year ago

  • Target version changed from 2.0.0 to 2.1.0

#8 Updated by Nobuyoshi Nakada 4 months ago

  • 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 (rbmodconstdefined): support nested class path as well as constget. [Feature #7414]

Also available in: Atom PDF