Problems with accessing directory defined in ENV variables

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?

Thomas L. [email protected] wrote:

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.

matt neuburg wrote:

Thomas L. [email protected] wrote:

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.

On Tue Aug 12 06:46:26 2008, Thomas L. wrote:

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'])

Fred P. wrote:

On Tue Aug 12 06:46:26 2008, Thomas L. wrote:

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??

On Tue Aug 12 08:06:45 2008, Thomas L. wrote:

back.

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

Your first example would expand to " /TEMP2002/t3905 " not
“/TEMP2002/t3905” like you wanted. I think in your second example you
still had the space immediately after the " (" #{ENV[‘TMPDIR’]}")

On 12 Aug., 01:06, Thomas L. [email protected] wrote:

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

On 12.08.2008 18:15, Thomas L. wrote:

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

Robert K. wrote:

On 12 Aug., 01:06, Thomas L. [email protected] wrote:

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.

On Tuesday 12 August 2008, Thomas L. wrote:

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

Here’s a short summary of how those things are used in ruby. I hope
you’ll
find it useful:

  • parentheses: they have two uses:
  • method calls: when you call a method, you put its arguments in
    parentheses,
    taking care not to put spaces between the method name and the
    parentheses. If
    you want, you can usually (but not always), omit parentheses in this
    case
  • grouping, like in 2 * (3 + 2 )
  • double quotes (""): they are the delimiters of double quoted strings.
    In
    this kind of string, string interpolation (that is, the #{} construct)
    happens
    and the backslash () character has a special meaning (see the table
    “Substitutions in double-quoted strings” in http://www.ruby-
    doc.org/docs/ProgrammingRuby/html/language.html). If you need to put a
    double
    quote character inside a double quoted string, you need to prefix it
    with a
    backslash, this way: “a “double quoted” string”
  • single quotes (’’): they are the delimiters of single quoted strings,
    where
    string interpolation doesn’t happen and the backslash has no special
    meaning,
    except when it precedes a single quote (in this case, it allows to
    insert a
    literal single quote into the string)
  • string interpolation (#{}): delimits a piece of ruby code embedded in
    a
    double-quoted string. The code is run and the value it returns is put in
    the
    string in place of the code itself. (Note that this happens as the
    string is
    created, it can’t be delayed)

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

If the summary above doesn’t help you, you can try looking at the
“Programming
ruby”, whose first edition is freely availlable online
(http://www.ruby-doc.org/docs/ProgrammingRuby/), expecially at the
chapter
“The Ruby Language”, and ask specific question on those things which
confuse
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

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