Problems with accessing directory defined in ENV variables

T

Thomas Luedeke

Ruby is giving me pure hell trying to access directories on a mounted
drive. Our network puts certain files on a temporary drive defined in
our .profile file I'm running HP UX/11).

If I ask for puts ENV['TMPDIR'], it knows what the pathway is (i.e. it
gives /TEMP2002/t3905).

If I issue a system command `echo $TMPDIR`, it also gives the right
answer.

However, if I try to change to that directory with a command such as
Dir.chdir(" #{ENV['TMPDIR']} " ), I always get errors like this:

/isf_spectrum.rb:1094:in `chdir': No such file or directory -
/TEMP2002/t3905 (Errno::ENOENT)
from ./isf_spectrum.rb:1094

I've also just tried to define a pathway explicitly as
"/TEMP2002/t3905", and the same error results. If I directly say "cd
$TMPDIR" in UNIX, I go right there. I've been trying everything for a
couple of hours, and I'm getting nowhere.

What is going on here? Why can't I change to a directory that Ruby can
point out clearly?
 
M

matt neuburg

Thomas Luedeke said:
However, if I try to change to that directory with a command such as
Dir.chdir(" #{ENV['TMPDIR']} " ),

There are extra spaces at the start and end of the double-quoted string.
Could that be the trouble? m.
 
T

Thomas Luedeke

matt said:
Thomas Luedeke said:
However, if I try to change to that directory with a command such as
Dir.chdir(" #{ENV['TMPDIR']} " ),

There are extra spaces at the start and end of the double-quoted string.
Could that be the trouble? m.


Well, I do get a different (if not less enigmatic) error:

test.rb:5: warning: Insecure world writable dir /TEMP2002 in PATH, mode
040777
/usr/bin/cd[7]: /TEMP2002/T3905/: not found.
 
T

Thomas Luedeke

Fred said:
However, if I try to change to that directory with a command such as
Dir.chdir(" #{ENV['TMPDIR']} " ), I always get errors like this:

You don’t need the quotes, you can just do:

Dir.chdir(ENV['TMPDIR'])

Whew - that did it. Thanks so much - that is a serious load off my
back.

Just for my newbie education, what was going wrong with what I was
doing? What was Ruby thinking I was asking for??
 
R

Robert Klemme

Fred said:
However, if I try to change to that directory with a command such as
Dir.chdir(" #{ENV['TMPDIR']} " ), I always get errors like this:
You don’t need the quotes, you can just do:
    Dir.chdir(ENV['TMPDIR'])

Whew - that did it.  Thanks so much - that is a serious load off my
back.

Just for my newbie education, what was going wrong with what I was
doing?  What was Ruby thinking I was asking for??

You made things more complicated than necessary - probably because of
a shell programming background. In a shell it is a good idea to
enclose every variable expansion in double quotes because this will
prevent issues with whitespace in variable values. Ruby works quite
differently and the expression ENV['TMPDIR'] yields a single string -
even if there are spaces in $TMPDIR.

But since you choose to employ string interpolation, the result of the
expression in #{} was incorporated into the string, which happened to
have whitespace at the front and rear. Thus the resulting string also
had whitespace at the front and read and that path does not exist on
your filesystem. You can easily see it by doing

ruby -e 'p " #{ENV['TMPDIR']} "'

HTH

Kind regards

robert
 
T

Thomas Luedeke

Robert said:
back.

Just for my newbie education, what was going wrong with what I was
doing? ?What was Ruby thinking I was asking for??

You made things more complicated than necessary - probably because of
a shell programming background. In a shell it is a good idea to
enclose every variable expansion in double quotes because this will
prevent issues with whitespace in variable values. Ruby works quite
differently and the expression ENV['TMPDIR'] yields a single string -
even if there are spaces in $TMPDIR.

But since you choose to employ string interpolation, the result of the
expression in #{} was incorporated into the string, which happened to
have whitespace at the front and rear. Thus the resulting string also
had whitespace at the front and read and that path does not exist on
your filesystem. You can easily see it by doing

ruby -e 'p " #{ENV['TMPDIR']} "'

HTH

Kind regards

robert


Admittedly, I'm having a terrible time understanding what Ruby wants
with respect to parentheses, #{} constructs, and the like. It's very
confusing.

My scripts are a horrible mess because I can't get things to work
consistently, and almost every time I refer to a variable, a directory,
or a file, I have to write a mini-script just to figure out what it
wants. It sucks.
 
R

Robert Klemme

Admittedly, I'm having a terrible time understanding what Ruby wants
with respect to parentheses, #{} constructs, and the like. It's very
confusing.

Maybe then you should read through some introductory material.
My scripts are a horrible mess because I can't get things to work
consistently, and almost every time I refer to a variable, a directory,
or a file, I have to write a mini-script just to figure out what it
wants. It sucks.

You can use IRB for that. And as I said: do not try to write Ruby
programs like shell scripts. These are two completely different pairs
of shoes.

Kind regards

robert
 
S

Stefano Crocco

Admittedly, I'm having a terrible time understanding what Ruby wants
with respect to parentheses, #{} constructs, and the like. =C2=A0It's very
confusing.

Here's a short summary of how those things are used in ruby. I hope you'll=
=20
find it useful:
=2D parentheses: they have two uses:
* method calls: when you call a method, you put its arguments in parenthes=
es,=20
taking care not to put spaces between the method name and the parentheses. =
If=20
you want, you can usually (but not always), omit parentheses in this case
* grouping, like in 2 * (3 + 2 )
=2D double quotes (""): they are the delimiters of double quoted strings. I=
n=20
this kind of string, string interpolation (that is, the #{} construct) happ=
ens=20
and the backslash (\) character has a special meaning (see the table=20
"Substitutions in double-quoted strings" in http://www.ruby-
doc.org/docs/ProgrammingRuby/html/language.html). If you need to put a doub=
le=20
quote character inside a double quoted string, you need to prefix it with a=
=20
backslash, this way: "a \"double quoted\" string"
=2D single quotes (''): they are the delimiters of single quoted strings, w=
here=20
string interpolation doesn't happen and the backslash has no special meanin=
g,=20
except when it precedes a single quote (in this case, it allows to insert a=
=20
literal single quote into the string)
=2D string interpolation (#{}): delimits a piece of ruby code embedded in a=
=20
double-quoted string. The code is run and the value it returns is put in th=
e=20
string in place of the code itself. (Note that this happens as the string i=
s=20
created, it can't be delayed)

NOTE: technically, single-quoted and double-quoted strings are not two=20
different strings, but different kinds of string literals, that is two=20
different ways of writing a string in your program.

If the summary above doesn't help you, you can try looking at the "Programm=
ing=20
ruby", whose first edition is freely availlable online
(http://www.ruby-doc.org/docs/ProgrammingRuby/), expecially at the chapter=
=20
"The Ruby Language", and ask specific question on those things which confus=
e=20
you here.
My scripts are a horrible mess because I can't get things to work
consistently, and almost every time I refer to a variable, a directory,
or a file, I have to write a mini-script just to figure out what it
wants.

If you need to test short pieces of code, you can use irb.

Stefano
 

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,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top