Forum: Ruby on Rails dynamic constant assignment

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Ee0293412faca21e4ebf3326ed9d5f8c?d=identicon&s=25 Kad Kerforn (kadoudal)
on 2009-02-09 23:07
(Received via mailing list)
I have a constant set up like that :
ALL_LOCALES
=> {"it"=>"Italian", "fr"=>"Français", "de"=>"Deutsch", "en-
GB"=>"English (UK)", "es"=>"Español (España)", "pt-PT"=>"Português",
"pl"=>"Polski", "en-US"=>"English (US)",
"pt-BR"=>"Português(Brasil)"}

my locale is set :  I18n.locale
=> :"en-US"

based on the current locale , dynamically changed, I would like to
define a new constant,   AVAILABLE_LOCALES which would be the
ALL_LOCALES minus the current locale...

AVAILABLE_LOCALES
=> {"it"=>"Italian", "fr"=>"Français", "de"=>"Deutsch", "en-
GB"=>"English (UK)", "es"=>"Español (España)", "pt-PT"=>"Português",
"pl"=>"Polski", "pt-BR"=>"Português (Brasil)"}

I dont' see how todo that ?  any clue ?

thanks a lot fyh
erwin
Ee0293412faca21e4ebf3326ed9d5f8c?d=identicon&s=25 Kad Kerforn (kadoudal)
on 2009-02-10 00:17
(Received via mailing list)
I also tried :

          AVAILABLE_LOCALES = ALL_LOCALES.dup
         AVAILABLE_LOCALES.delete_if{|key, value| key == I18n.locale }

but get also an error ... dynamic constant assignment (SyntaxError)
with the dup ...
I don't understand as I can do it with irb ....
96146b7a23174e2e024c06a49f845bb8?d=identicon&s=25 Jeffrey L. Taylor (Guest)
on 2009-02-10 05:33
(Received via mailing list)
Quoting Erwin <yves_dufour@mac.com>:
>
> > based on the current locale , dynamically changed, I would like to
> > define a new constant,   AVAILABLE_LOCALES which would be the
> > ALL_LOCALES minus the current locale...
> >
> > AVAILABLE_LOCALES
> > => {"it"=>"Italian", "fr"=>"Français", "de"=>"Deutsch", "en-
> > GB"=>"English (UK)", "es"=>"Español (España)", "pt-PT"=>"Português",
> > "pl"=>"Polski", "pt-BR"=>"Português (Brasil)"}
> >
> > I dont' see how todo that ?  any clue ?

What is constant about a variable that changes everytime the locale
changes?
And if you have multiple users with different locales, potentially
changes
with every request?  Given how changeable it is, why do you want to
confuse
the future maintainer by implying it is constant when it isn't.

Or put another way, why can't the variable be named available_locales.

Shrug,
  Jeffrey
This topic is locked and can not be replied to.