Declaring list of string constnats for app

Hi All,

I'm building an app that compares statistics from sports games. Each
statistic has an "action" that defines what happened at that point in
time, for example "player has possession" is one such action string.

I use these strings to pull out / sort the statistics into meaningful
information, so these strings are used throughout the app.

I want to know a good place to have a list of global variables like
this. I have tried :

creating file called action_string_constants.rb in /lib.
In this file I declare a module :
module ActionStringConstants

  MB_ACTION_STRING_PLAYER_HAS_POSSESSION = "player has posession"

end

I then try and use this in another class such as Player (model) with

include ActionStringConstants

However this returns a NameError (uninitialized constant
Player::ActionStringConstants):

I then thought of including this in application.rb, as I want to use
these constants in many files, but this returns a similar error when
trying to launch the app.

Where can I declare a string constant??!

This really should be simple!

Michael Baldock wrote in post #1061986:

I'm building an app that compares statistics from sports games. Each
statistic has an "action" that defines what happened at that point in
time, for example "player has possession" is one such action string.

I use these strings to pull out / sort the statistics into meaningful
information, so these strings are used throughout the app.

Where can I declare a string constant??!

I generally prefer keeping my constants close to context where they are
used. For example if you're using a set of constants related to Player
objects then I'd do something like:

class Player < ActiveRecord::Base
  HAS_POSSESSION = 'player has possession'
  LOST_POSSESSION = 'player has lost possession'

  # Rest of implementation
end

However, given that Ruby provides symbols, using string constants are
generally not necessary. Just use the symbol (e.g. :has_possession).

If you want to validate a symbol exists in a list then just make an
array constant containing the list of symbols (e.g STAT_ACTIONS = [
:has_possession, :lost_possession, ... ]); Put that in whatever class
makes sense and get it from there. (e.g. Player::STAT_ACTIONS).

Ok, thanks for the advice,

The problem i have is that I want to use these strings in the Player
model, but also many other places. There's a statistics_controller thats
going to need it, and potentially other controllers / models that are
going to use the database of stats to decide things.

Not being a super-fluent Ruby developer, how do these symbols work?

if I declare : HAS_POSSESSION = 'player has possession' somewhere, does
this enable the symbol :has_posession, if so what's the point of using
the symbol, why don't i just use HAS_POSSESSION?

or is it that I don't need to declare anything, and I just use
:has_possession, which equates to "has possession", which seems odd!

Just to be clear, I've got a database of statistics, with an attribute
called action. This action describes what happened, ie one stat.action =
"player has possession", whereas another stat.action = "player was
tackled". It's these strings ("player has possession" / "player was
tackled") that I want to store in a list.

Well this is what it says in config/application.rb

# Settings in config/environments/* take precedence over those specified here.
# Application configuration should go into files in config/initializers
# -- all .rb files in that directory are automatically loaded.

And this is what I’ve done… created a file in the initializers directory.

Michael Baldock wrote in post #1062126:

Just to be clear, I've got a database of statistics, with an attribute
called action. This action describes what happened, ie one stat.action =
"player has possession", whereas another stat.action = "player was
tackled". It's these strings ("player has possession" / "player was
tackled") that I want to store in a list.

I see. You're actually planning to use these string to present to the
user in a view. In that case I'd use the Internationalization (I18n)
strings file. That way it would be trivial to localize your application
strings to another language at some point in the future.

http://guides.rubyonrails.org/i18n.html

Cheers both,

I like the sound of the i18n method, for the localization potential,
that sounds very useful.

Dave - it's good to also know where to put files so that they're
automatically loaded as well, cheers for the tip.

MIke