Accessing a Custom variable I put in development.rb - how ca

Hi,

I just quite seem to find out how to access a custom environment
variable I
put into “development.rb”, “test.rb” etc

Can someone give me a pointer for this please.

Tks
Greg

(bump)

Still trying to understand default Rails behavior here so would be great
if
someone can clarify. That is how is one supposed to put one’s own
custom
variables in the environment.rb/developmnet.rb type files and then get
access to them.

In the environment.rb file the last line is “# Include your application
configuration below”. However I’ve tried adding my own variables here
and
not been able to see them later within ENV. That is:

a) add the following to the bottom of environment.rb
gregs.test1=“1*************************************************”

b) go “ruby script/console”

c) issue “ENV” but I can’t see this custom environment variable being
loaded.

Can any clarify the default means of setting custom environment
variables in
rails (I’ve been googling but to no avail yet)

Tks
Greg

On 2/11/07, Greg H. [email protected] wrote:

a) add the following to the bottom of environment.rb

gregs.test1=“1*************************************************”

Hi Greg,

To define an environment variable, use ENV[‘NAME’] = value (where
value must be a string).

So doing ENV[‘test1’] = ‘blah’ works for me.

Regards,
George.

Tks George - but do you/someone else know whether it is possible to put
your
environment variables directly within the rails environment files?
(e.g.
environment.rb, development.rb etc)

Greg

On 2/11/07, Greg H. [email protected] wrote:

Tks George - but do you/someone else know whether it is possible to put your
environment variables directly within the rails environment files? (e.g.
environment.rb, development.rb etc)

Greg

Yes, it works for me if I stick it in environment.rb or
environments/development.rb, on a fresh rails app, in both rails 1.1.6
and 1.2.2. Note that you must assign a String. A Fixnum, etc. won’t
cut it (you should see an error when you start script/console if you
get this wrong).

Is there a reason you need environment variable, BTW? Because it
might be easier to just use a constant, e.g., “FOO = 1”, as well.

Cheers,
George.

On 2/11/07, George O. [email protected] wrote:

cut it (you should see an error when you start script/console if you
get this wrong).

Is there a reason you need environment variable, BTW? Because it
might be easier to just use a constant, e.g., “FOO = 1”, as well.

I should also add that my experiments were on Linux. I’m not sure if
other OSs might behave differently.

basically wanted a mechanism to have a external URL used by the
application
to be different in development George. Are you putting your entry in
environment.rb right at the bottom of the file, i.e. outside the Rails::
Initializer.run area?

I’m assuming that after I have put, myvar = ‘this is it’, at the bottom
of
the environment.rb file and restart, that if I go “ruby script/console”
then
ENV I should be able to see it, or ENV[‘myvar’]…but at this stage
this
isn’t working for me. I’m on Mac OS X.

Any ideas why this isn’t working George/anyone?

Thanks again
Greg

doh! saw the errors of my ways - had been treating the environment.rb
like
a java properties file - I see now it’s actually ruby and I had to put
the
“ENV[‘myvar’]=…” into the file, not “‘myvar’ = ‘…’” - thanks George

Greg H. wrote:

basically wanted a mechanism to have a external URL used by the
application
to be different in development George. Are you putting your entry in
environment.rb right at the bottom of the file, i.e. outside the Rails::
Initializer.run area?

I’m assuming that after I have put, myvar = ‘this is it’, at the bottom
of
the environment.rb file and restart, that if I go “ruby script/console”
then
ENV I should be able to see it, or ENV[‘myvar’]…but at this stage
this
isn’t working for me. I’m on Mac OS X.

Any ideas why this isn’t working George/anyone?

Thanks again
Greg

I’m glad you got it working, but I don’t think this is the best way to
handle this. As stated above, you should probably use a constant, or
perhaps a direct manipulation of the url to be changed.

Lets say you have some API class that you want to use a different URL in
development mode. The cleanest way to handle this is something like

#development.rb
MyApi.access_url = “http://foo.com

That section is for manipulating parts of your app more than setting ENV
variables.

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs