I'm confused about $LOAD_PATH

T

tad.bochan

Hi,
I'm confused about how to update this $: variable in a ruby program.

In the Ruby book PPG (page 218 in my book) it says that this variable
is read-only , but in the same sentence,
it says
"This variable can be set from within a program to alter the
default search path; typically programs use $: << dir
to append dir to the path. [r/o]"

The book says in several places that you can update
this variable at runtime, BUT, when I try
$LOAD_PATH = $LOAD_PATH << "mydir"
or
$LOAD_PATH = $LOAD_PATH.unshift "mydir"
I get,
"$LOAD_PATH is a read-only variable (NameError)"

Regards,
Tad





This message and any attachments (the "message") is
intended solely for the addressees and is confidential.
If you receive this message in error, please delete it and
immediately notify the sender. Any use not in accord with
its purpose, any dissemination or disclosure, either whole
or partial, is prohibited except formal approval. The internet
can not guarantee the integrity of this message.
BNP PARIBAS (and its subsidiaries) shall (will) not
therefore be liable for the message if modified.

---------------------------------------------

Ce message et toutes les pieces jointes (ci-apres le
"message") sont etablis a l'intention exclusive de ses
destinataires et sont confidentiels. Si vous recevez ce
message par erreur, merci de le detruire et d'en avertir
immediatement l'expediteur. Toute utilisation de ce
message non conforme a sa destination, toute diffusion
ou toute publication, totale ou partielle, est interdite, sauf
autorisation expresse. L'internet ne permettant pas
d'assurer l'integrite de ce message, BNP PARIBAS (et ses
filiales) decline(nt) toute responsabilite au titre de ce
message, dans l'hypothese ou il aurait ete modifie.
 
R

Robert Klemme

Hi,
I'm confused about how to update this $: variable in a ruby program.

In the Ruby book PPG (page 218 in my book) it says that this variable
is read-only , but in the same sentence,
it says
"This variable can be set from within a program to alter the
default search path; typically programs use $: << dir
to append dir to the path. [r/o]"

The book says in several places that you can update
this variable at runtime, BUT, when I try
$LOAD_PATH = $LOAD_PATH << "mydir"
or
$LOAD_PATH = $LOAD_PATH.unshift "mydir"
I get,
"$LOAD_PATH is a read-only variable (NameError)"

You can't assign to $LOAD_PATH but you can change the elements contained in
the Array. Just do

$LOAD_PATH << "mydir"
$LOAD_PATH.unshift "mydir"

Regards

robert
 
T

ts

t> $LOAD_PATH = $LOAD_PATH << "mydir"
^^^^^^^^^^^^

the problem is here, just write

$LOAD_PATH << "mydir"

you modify the content of $LOAD_PATH, but not the variable $LOAD_PATH


Guy Decoux
 
H

Hal Fulton

$LOAD_PATH = $LOAD_PATH << "mydir"
$LOAD_PATH = $LOAD_PATH.unshift "mydir"

These above won't work, but the below will:

$LOAD_PATH << "mydir"
$LOAD_PATH.unshift "mydir"

Ruby distinguishes greatly between variables and the objects
they refer to.

In the former case, you're trying to make the variable refer
to a different object, which won't work for this variable.

In the latter case, you're altering the object to which the
variable refers.

Does that help?


Hal
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top