I’m running Ruby 1.6.8 over Win2000SP3. Dir.getwd returns the current
directory with forward slash as the separator. I tried setting
SEPARATOR = “\” at the beginning of the script, to no avail. Is
there a way to get the correct form returned, or do I have to do
pattern substitution?
I’m running Ruby 1.6.8 over Win2000SP3. Dir.getwd returns the current
directory with forward slash as the separator. I tried setting
SEPARATOR = “\” at the beginning of the script, to no avail. Is
there a way to get the correct form returned …
Looking at the code, the file separator is wired as ‘/’. Ruby takes
this format as input but if you need Win format you’ll …
… have to do pattern substitution.
Erm, probably, yes.
Dir.getwd.gsub!(File::SEPARATOR, File::ALT_SEPARATOR)
→ E:\Gallery\JoGuest (YMMV;)
daz
···
“Richard” RLMuller@comcast.net wrote:
I don’t have Windows, so I can’t test this, but I think this can be
handled by the Shell class.
require ‘shell’
Shell.new.pwd
The Shell class (at $/lib/ruby/1.6.8/shell.rb) also has a method to set
the default record separator.
···
On Sunday, May 25, 2003, at 12:51 AM, Richard wrote:
I’m running Ruby 1.6.8 over Win2000SP3. Dir.getwd returns the current
directory with forward slash as the separator. I tried setting
SEPARATOR = “\” at the beginning of the script, to no avail. Is
there a way to get the correct form returned, or do I have to do
pattern substitution?
Hi daz,
Thanks for your succinct and apparently portable solution:
Dir.getwd.gsub!(File::SEPARATOR, File::ALT_SEPARATOR)
I had used:
sCurDir = Dir.getwd.gsub(/\//, '\\')
which appears to have worked fine.
I have two nit-picking questions, if you don’t mind.
-
Am I correct in assuming File::ALT_SEPARATOR ought to be set by the
group that ports Ruby to a platform? -
Am I also correct that the “!” operator is not significant because
you have to have a target variable to receive the transformed w.d.
string?
Again, despite my modest disappointment in the lack of a feature that
I recall being in Perl, I am nevertheless appreciative of a portable
solution.
Regards,
Richard
Hi Mark,
require ‘shell’
Shell.new.pwd
That worked great. Many thanks. That’s the simplest approach I’ve
seen so far, and I don’t see how it could be much simpler
I took a look at shell.rb to see how this command worked. I’m a Ruby
newbie, so if you have time, please identify what I’m missing in my
understanding (and if you don’t, no problem; thanks for what you’ve
done!):
Shell.new creates a shell object and invokes the initialize method on
that object. The first thing that method did is initialize the
instance variable @cmd to the result of Dir.pwd, which returns the
current working directory with forward-slash delimiters.
Then the command invokes the pwd method on that initialized Shell
object. That name is an alias for the cwd instance method. But I
don’t see any cwd method defined in the Shell class, nor in its
required modules:
shell/error
shell/command-processor
shell/process-controller
and it didn’t seem I’d find it in any of their required modules. (I
only checked the ftool.rb module)
So how do the separators get transformed? It looks like magic
Regards,
Richard
Hi Richard,
I’m a relative newbie too so I learned from your analysis. I simply
happen to have been exploring the Shell class because it is a
relatively undocumented part of the standard library. A good resource
is to run rdoc (available on the RAA) on particular Ruby files and view
the result in a browser.
I think the Shell object keeps track of the current directory as it
changes. See the the chdir method. On the separators, I just don’t
know. The default record separator is set to ‘/’ unless replaced, but I
don’t see where it gets the alternate record separator for a non-Unix
like system. Perhaps Cygwin supplies this for Ruby on Windows?
···
On Sunday, May 25, 2003, at 05:56 PM, Richard wrote:
[snip]
I don’t see any cwd method defined in the Shell class, nor in its
required modules:
shell/error
shell/command-processor
shell/process-controller
and it didn’t seem I’d find it in any of their required modules. (I
only checked the ftool.rb module)So how do the separators get transformed? It looks like magic
Regards,
Richard
I had used:
sCurDir = Dir.getwd.gsub(///, ‘\’)
- Am I correct in assuming File::ALT_SEPARATOR ought to be set by the
group that ports Ruby to a platform?
As a 1st-year Rubyist, I yield to the hackers.
At present, platforms that are not DOS-like have ALT_SEPARATOR
set to nil so my solution wasn’t portable.
separator = rb_obj_freeze(rb_str_new2("/"));
rb_define_const(rb_cFile, "SEPARATOR", separator);
#ifdef DOSISH
rb_define_const(rb_cFile, “ALT_SEPARATOR”, rb_obj_freeze(rb_str_new2(“\”)));
#else
rb_define_const(rb_cFile, “ALT_SEPARATOR”, Qnil);
#endif
</file.c>
- Am I also correct that the “!” operator is not significant because
you have to have a target variable to receive the transformed w.d.
string?
As I understand things;
Dir.getwd returns the w.d. as a value, say, aString1.
aString1.gsub!(…) returns aString1 (modified in place)
whereas
aString1.gsub(…) would return aString2, leaving aString1
as unreferenced garbage.
Only one string emerges as the value of the expression, whether you
explicitly assign it or use, for example,
next_ruby_method(Dir.getwd) -or-
puts Dir.getwd.gsub(…)
···
“Richard” RLMuller@comcast.net wrote:
To be portable, you need something like -
wd = Dir.getwd.gsub!(File::SEPARATOR, File::ALT_SEPARATOR || File::SEPARATOR)
-or-
wd = Dir.getwd
wd.gsub!(File::SEPARATOR, File::ALT_SEPARATOR) if File::ALT_SEPARATOR
-or your -
sCurDir = Dir.getwd.gsub(///, ‘\’)
which IMHO looks portable using non-! gsub (and not as ugly).
daz
[snip]
I don’t see any cwd method defined in the Shell class, nor in its
required modules:
shell/error
shell/command-processor
shell/process-controller
and it didn’t seem I’d find it in any of their required modules. (I
only checked the ftool.rb module)So how do the separators get transformed? It looks like magic
Regards,
Richard
attr :cwd
defines the method to read @cwd.
However, you would be using Shell to do a Dir#getwd instead of
doing it directly. It returns forward-slash separators (for me).
require ‘shell’
puts Shell.new.pwd # → D:/ruby/lib/ruby/1.8/shell
The default record separator in Shell is set to global variable $/
(which is usually newline). Nothing to do with the directory
component separator. Confusing, though.
daz
attr :cwd
defines the method to read @cwd.
However, you would be using Shell to do a Dir#getwd instead of
doing it directly. It returns forward-slash separators (for me).require ‘shell’
> The default record separator is set to '/' unless replaced [...]
puts Shell.new.pwd # → D:/ruby/lib/ruby/1.8/shellThe default record separator in Shell is set to global variable $/
(which is usually newline). Nothing to do with the directory
component separator. Confusing, though.
Thanks, daz. I’m going to have to cogitate this for a while.
···
Richard