Usage of constants


#1

I would like to define some constants that I can use across models and
views and I cannot figure out how to do that. Googling hasn’t turned up
anything useful and I’m looking at the PickAxe book, and I get the
impression that a constant must belong to a class.

If I just want a constant like…

CompanyPhoneNo = “(602) 999-9999”

and be able to use it in any view with any controller, how do I do that?

Craig


#2

I think you can put constants/configs in environments.rb at the bottom.
There’s a good explanation here:
http://wiki.rubyonrails.com/rails/pages/HowtoAddYourOwnConfigInfo


#3

On Wed, 2006-03-15 at 01:20 -0500, Dorian M. wrote:

CompanyPhoneNo = “(602) 999-9999”

and be able to use it in any view with any controller, how do I do that?

Craig


yeah - that’s what I was looking for. The funny thing was…“All Pages”
wiki.rubyonrails.org was where I started but I searched for ‘Constants’

  • duh

Craig


#4

where are you setting that in the controller file?

I think you should place:

include “…/config/defaults.rb”

before the class declaration.

Also, if you place it in the apllication.rb controller, it will be
accessible everywhere.

dorian


#5

On Wed, 2006-03-15 at 01:20 -0500, Dorian M. wrote:

CompanyPhoneNo = “(602) 999-9999”

and be able to use it in any view with any controller, how do I do that?


you know…this is one of the things that sort of sucks about
rails…that which should be ridiculously simple is poorly documented
and difficult to accomplish…

that pages says…

Just another ruby file
Place your configuration constants in a regular Ruby file. include the
file where you need the settings. For example, this is my
config/email.conf.rb

Email configuration

$SMTP_ADDRESS = “smtp.mac.com
$SMTP_PORT = 25
$SMTP_DOMAIN = “mail.mac.com
$SMTP_USER_NAME = “johnatl”
$SMTP_USER_PASSWORD = “secret”

so I made a file… config/defaults.rb

In the controller section, I try…

:include ../config/defaults.rb

SyntaxError in #
./script/…/config/…/app/controllers/reports_controller.rb:21: unknown
regexp options - dfalt

:include "../config/defaults.rb"

SyntaxError in #
./script/…/config/…/app/controllers/reports_controller.rb:21: syntax
error, unexpected tSTRING_BEG, expecting kEND
:include “…/config/defaults.rb”

:include => "../config/defaults.rb"

SyntaxError in #
./script/…/config/…/app/controllers/reports_controller.rb:21: syntax
error, unexpected tASSOC, expecting kEND
:include => “…/config/defaults.rb”

include "../config/defaults.rb"

NoMethodError in Reports#rfn2
undefined method `include’ for #ReportsController:0xb7d003d4

so that means in order to get a few stupid default values, I have to
create a table and keep hitting the table all the time.

Craig


#6

this was before the class declaration in application.rb
TypeError in #
wrong argument type String (expected Module)

RAILS_ROOT: script/…/config/…

Application Trace | Framework Trace | Full Trace
./script/…/config/…/app/controllers/application.rb:3:in include' ./script/../config/../app/controllers/application.rb:3 /usr/lib/ruby/gems/1.8/gems/activesupport-1.2.5/lib/active_support/dependencies.rb:207:inload’
/usr/lib/ruby/gems/1.8/gems/activesupport-1.2.5/lib/active_support/dependencies.rb:39:in
require_or_load' /usr/lib/ruby/gems/1.8/gems/activesupport-1.2.5/lib/active_support/dependencies.rb:22:independ_on’
/usr/lib/ruby/gems/1.8/gems/activesupport-1.2.5/lib/active_support/dependencies.rb:178:in
require_dependency' /usr/lib/ruby/gems/1.8/gems/activesupport-1.2.5/lib/active_support/dependencies.rb:134:inload_file!’
/usr/lib/ruby/gems/1.8/gems/activesupport-1.2.5/lib/active_support/dependencies.rb:97:in
const_load!' /usr/lib/ruby/gems/1.8/gems/activesupport-1.2.5/lib/active_support/dependencies.rb:80:inconst_load!’
/usr/lib/ruby/gems/1.8/gems/rails-1.0.0/lib/dispatcher.rb:71:in
prepare_application' /usr/lib/ruby/gems/1.8/gems/rails-1.0.0/lib/dispatcher.rb:37:indispatch’
/usr/lib/ruby/gems/1.8/gems/rails-1.0.0/lib/webrick_server.rb:117:in
handle_dispatch' /usr/lib/ruby/gems/1.8/gems/rails-1.0.0/lib/webrick_server.rb:83:inservice’
/usr/lib/ruby/1.8/webrick/httpserver.rb:104:in service' /usr/lib/ruby/1.8/webrick/httpserver.rb:65:inrun’
/usr/lib/ruby/1.8/webrick/server.rb:173:in start_thread' /usr/lib/ruby/1.8/webrick/server.rb:162:instart_thread’
/usr/lib/ruby/1.8/webrick/server.rb:95:in start' /usr/lib/ruby/1.8/webrick/server.rb:92:instart’
/usr/lib/ruby/1.8/webrick/server.rb:23:in start' /usr/lib/ruby/1.8/webrick/server.rb:82:instart’
/usr/lib/ruby/gems/1.8/gems/rails-1.0.0/lib/webrick_server.rb:69:in
dispatch' /usr/lib/ruby/gems/1.8/gems/rails-1.0.0/lib/commands/servers/webrick.rb:59 /usr/lib/site_ruby/1.8/rubygems/custom_require.rb:21:inrequire’
/usr/lib/ruby/gems/1.8/gems/activesupport-1.2.5/lib/active_support/dependencies.rb:214:in
require' /usr/lib/ruby/gems/1.8/gems/rails-1.0.0/lib/commands/server.rb:28 /usr/lib/site_ruby/1.8/rubygems/custom_require.rb:21:inrequire’
/usr/lib/ruby/gems/1.8/gems/activesupport-1.2.5/lib/active_support/dependencies.rb:214:in
require' script/server:3 ./script/../config/../app/controllers/application.rb:3:ininclude’
./script/…/config/…/app/controllers/application.rb:3
/usr/lib/ruby/gems/1.8/gems/activesupport-1.2.5/lib/active_support/dependencies.rb:207:in
load' /usr/lib/ruby/gems/1.8/gems/activesupport-1.2.5/lib/active_support/dependencies.rb:39:inrequire_or_load’
/usr/lib/ruby/gems/1.8/gems/activesupport-1.2.5/lib/active_support/dependencies.rb:22:in
depend_on' /usr/lib/ruby/gems/1.8/gems/activesupport-1.2.5/lib/active_support/dependencies.rb:178:inrequire_dependency’
/usr/lib/ruby/gems/1.8/gems/activesupport-1.2.5/lib/active_support/dependencies.rb:134:in
load_file!' /usr/lib/ruby/gems/1.8/gems/activesupport-1.2.5/lib/active_support/dependencies.rb:97:inconst_load!’
/usr/lib/ruby/gems/1.8/gems/activesupport-1.2.5/lib/active_support/dependencies.rb:80:in
const_load!' /usr/lib/ruby/gems/1.8/gems/rails-1.0.0/lib/dispatcher.rb:71:inprepare_application’
/usr/lib/ruby/gems/1.8/gems/rails-1.0.0/lib/dispatcher.rb:37:in
dispatch' /usr/lib/ruby/gems/1.8/gems/rails-1.0.0/lib/webrick_server.rb:117:inhandle_dispatch’
/usr/lib/ruby/gems/1.8/gems/rails-1.0.0/lib/webrick_server.rb:83:in
service' /usr/lib/ruby/1.8/webrick/httpserver.rb:104:inservice’
/usr/lib/ruby/1.8/webrick/httpserver.rb:65:in run' /usr/lib/ruby/1.8/webrick/server.rb:173:instart_thread’
/usr/lib/ruby/1.8/webrick/server.rb:162:in start_thread' /usr/lib/ruby/1.8/webrick/server.rb:95:instart’
/usr/lib/ruby/1.8/webrick/server.rb:92:in start' /usr/lib/ruby/1.8/webrick/server.rb:23:instart’
/usr/lib/ruby/1.8/webrick/server.rb:82:in start' /usr/lib/ruby/gems/1.8/gems/rails-1.0.0/lib/webrick_server.rb:69:indispatch’
/usr/lib/ruby/gems/1.8/gems/rails-1.0.0/lib/commands/servers/webrick.rb:59
/usr/lib/site_ruby/1.8/rubygems/custom_require.rb:21:in require' /usr/lib/ruby/gems/1.8/gems/activesupport-1.2.5/lib/active_support/dependencies.rb:214:inrequire’
/usr/lib/ruby/gems/1.8/gems/rails-1.0.0/lib/commands/server.rb:28
/usr/lib/site_ruby/1.8/rubygems/custom_require.rb:21:in require' /usr/lib/ruby/gems/1.8/gems/activesupport-1.2.5/lib/active_support/dependencies.rb:214:inrequire’
script/server:3
This error occured while loading the following files:
script/…/config/…/app/controllers/application.rb


#7

$SMTP_PORT = 25
$SMTP_DOMAIN = “mail.mac.com
$SMTP_USER_NAME = “johnatl”
$SMTP_USER_PASSWORD = “secret”

Looks like perl-code to me. Try changing the $-sign with a @ instead.

so I made a file… config/defaults.rb

In the controller section, I try…

:include ../config/defaults.rb

SyntaxError in #
./script/…/config/…/app/controllers/reports_controller.rb:21: unknown regexp options - dfalt

In ruby the include statement is called require or load. Try changing
the include statement.

so that means in order to get a few stupid default values, I have to create a table and keep hitting the table all the time.

Craig

regards
Claus


#8

I was under the impression that @ was an instance variable that wouldn’t
persist but $ was a constant.

You’re correct.

Turns out that ‘include’ wasn’t correct but ‘require’ was the juju
necessary and all I had to do was fix the path.

thanks

Thanks to you as well :slight_smile:

regards
Claus


#9

I was under the impression that @ was an instance variable that wouldn’t
persist but $ was a constant.

Turns out that ‘include’ wasn’t correct but ‘require’ was the juju
necessary and all I had to do was fix the path.

I will have to change the wiki

thanks

Craig


#10

On Mar 15, 2006, at 1:07 AM, Craig W. wrote:

I was under the impression that @ was an instance variable that
wouldn’t
persist but $ was a constant.

$ precedes global variables. Constants have their initial character
upper cased.


Jason P.
removed_email_address@domain.invalid

“The key to performance is elegance, not
battalions of special cases.”

  • Jon Bentley and Doug McIlroy

#11

Hi –

On Wed, 15 Mar 2006, Craig W. wrote:

I was under the impression that @ was an instance variable that wouldn’t
persist but $ was a constant.

Turns out that ‘include’ wasn’t correct but ‘require’ was the juju
necessary and all I had to do was fix the path.

I will have to change the wiki

While you’re there, please also change “configuration constants” to
“configuration global variables”, to suit the sample code.

David


David A. Black (removed_email_address@domain.invalid)
Ruby Power and Light, LLC (http://www.rubypowerandlight.com)

“Ruby for Rails” chapters now available
from Manning Early Access Program! http://www.manning.com/books/black


#12

On Mar 15, 2006, at 7:12 AM, removed_email_address@domain.invalid wrote:

I will have to change the wiki

While you’re there, please also change “configuration constants” to
“configuration global variables”, to suit the sample code.

In the provided code sample would it be more Rubyist to make those
globals into constants? Actually there are two cases there that
clarification would be good on with this scoping issue: the
environment.rb and the included config.rb files. Better to use
constants in each of those or one and not the other?

BTW, very much enjoying your book. New chapter(s) for us today? :wink:


Jason P.
removed_email_address@domain.invalid

“The key to performance is elegance, not
battalions of special cases.”

  • Jon Bentley and Doug McIlroy

#13

Hi –

On Tue, 14 Mar 2006, Craig W. wrote:

In the controller section, I try…

:include …/config/defaults.rb
SyntaxError in #
./script/…/config/…/app/controllers/reports_controller.rb:21: unknown regexp options - dfalt

You have to put your filename in quotation marks. Otherwise Ruby sees
forward slashes and thinks you’re trying to write a regular expression
literal.

David


David A. Black (removed_email_address@domain.invalid)
Ruby Power and Light, LLC (http://www.rubypowerandlight.com)

“Ruby for Rails” chapters now available
from Manning Early Access Program! http://www.manning.com/books/black


#14

On Wed, 2006-03-15 at 05:12 -0800, removed_email_address@domain.invalid wrote:

I will have to change the wiki

While you’re there, please also change “configuration constants” to
“configuration global variables”, to suit the sample code.


done - thanks

Craig