ruby on rails - What's the differences btw const_get and qualified_const_get? -


there series of methods, i.e., const_defined?, const_get, or const_set, in standard ruby library.

const_defined?, const_get, const_set

and also, in active support core extensions of rails, "qualified_" counterparts these individuals exist.

qualified_const_defined?, qualified_const_get, qualifeid_const_set

is there can explain explicitly differences between bare , qualified forms these methods?

thank in advance.

hyo

the qualified_ const helpers support interacting constants @ arbitrary depths (not children of subject).

i think example easiest way explain one. let's foo::bar::baz exists:

 > object::const_get "foo::bar::baz" nameerror: wrong constant name foo::bar::baz  > object::const_get "foo" => foo  > foo.const_get "bar" => foo::bar  > foo::bar.const_get "baz" => foo::bar::baz 

the qualified_ methods allow avoid walking module hierarchy directly:

 > object::qualified_const_get "foo::bar::baz" => foo::bar::baz  > foo.qualified_const_set "bar::fizz", 123 => 123  > foo::bar::fizz => 123 

i'd recommend poking around source, too. it's pretty clean.


Comments

Popular posts from this blog

Why does Ruby on Rails generate add a blank line to the end of a file? -

keyboard - Smiles and long press feature in Android -

node.js - Bad Request - node js ajax post -