dry-rb / dry-inflector

Inflector for Ruby
https://dry-rb.org/gems/dry-inflector
MIT License
95 stars 15 forks source link

Add #constantize and #foreign_key #13

Closed abinoam closed 6 years ago

abinoam commented 6 years ago

I have simplified constantize to just a Object.const_get.

Related: https://github.com/dry-rb/dry-inflector/issues/5#issuecomment-344602652

Note: #constantize is here just for back compatibility with other libraries. It's much better to use the plain #const_get because it will use the current context.

module Dry
  class Inflector
    VERSION = "0.1.0"

    const_get("VERSION") #=> "0.1.0"
    Object.const_get("VERSION") #=> NameError: uninitialized constant VERSION
    inflector.constantize("VERSION") #=> Don't work either, because uses Object.const_get
  end
end
abinoam commented 6 years ago

For Symbols as input, const_get already takes Symbols foreign_key uses demodulize that already does the to_s conversion.