S
S
Shaks2017-02-01 18:07:06
ruby
Shaks, 2017-02-01 18:07:06

How to work with windows from under nix to ruby?

I'm actually a bit in a stupor.
From under nix (ubuntu) the script works with different paths, the paths can be both nix and windows.
No problems with nicks

nix_path = Pathname.new '/nix/path/ok/file.txt'
nix_path.basename # => <Pathname:file.txt> 
nix_path.parent # => <Pathname:/nix/path/ok>

And the trouble with Windows..
win_path = Pathname.new 'C:\win\path\file.txt'
win_path.basename # => <Pathname:C:\win\path\file.txt> 
win_path.parent # => <Pathname:.>

The same goes for using File
File.basename 'C:\win\path\file.txt' # => "C:\\win\\path\\file.txt"

How to be?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
vsuhachev, 2017-02-01
@shaks

win_path = Pathname.new 'C:\win\path\file.txt'.tr('\\', '/')

And in general, conceptually, you are using Pathname incorrectly. its implementation is system dependent. Therefore, it is better to use it for native paths for the current OS. And for paths from your subject area (Unix / Win / etc), write separate classes that do everything as you need

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question