3

I often use long paths in my scripts and since i'm on windows i have to convert these long paths to nix style with slashes in stead of backslashes. Nothing difficult but annoying if thereafter you copy that path to go to that folder since in explorer you have to do the opposite again.

So i made a function that does the conversion, now i can use windowspaths that i can copy around and keep Ruby sattisfied.

Question: is there a more elegant solution here ? I don't like the second gsub to handle the double \ at he beginning and also would like to handle a \ at the end (currently not possible). The function should be able to handle network unc's (\..) and local drivepaths (c:..)

class String 
  def path
    self.gsub('\\','/').gsub(/^\//,'//')
  end
end

path = '\\server\share\folder'.path

Dir.glob(path+'**/*') do |file|
  puts file
end

#=>
#//server/share/folder/file1.txt
#//server/share/folder/file2.txt
peter
  • 41,770
  • 5
  • 64
  • 108
  • I'd look at Pathname (http://www.ruby-doc.org/stdlib-1.9.3/libdoc/pathname/rdoc/Pathname.html) or at least File#join (http://www.ruby-doc.org/core-1.9.3/File.html#method-c-join) instead of doing this manually. – Michael Kohl Jun 07 '12 at 11:38
  • Thnx Michael, Pathname seems interesting but i can't see how it helps me to do this, can you give an example in an answer ? If i use File.join i'm further from home than before, that is surely not copyable in exeplorer – peter Jun 07 '12 at 11:51
  • Possible duplicate of http://stackoverflow.com/questions/4862192/convert-windows-path-to-unc-in-ruby. However, the other question is much narrower. – Todd A. Jacobs Jun 07 '12 at 11:55
  • @peter See CodeGnome's answer for File#join. – Michael Kohl Jun 07 '12 at 11:57
  • added another version as answer, based on a split/join, hope someone comes up with something better – peter Jun 07 '12 at 12:35
  • What specifically makes you think Ruby is more "satisfied" with Unix-style paths? – Mark Thomas Jun 07 '12 at 13:13
  • Well the fact that she is responding with an error is you try to use windows style paths, so i guess she likes nix better than windows 8>) Kidding of course, i'm glad Ruby is used on all major platforms and that more and more windows users come to join our rangs – peter Jun 07 '12 at 15:50

2 Answers2

1

The suggestion to use File.join made me try a regular split & join and now i have this version, got rid of the ugly double gsub, now it's longer but can handle an ending slash. Has someone a better version ?

class String
  def to_path(end_slash=false)
    "#{'/' if self[0]=='\\'}#{self.split('\\').join('/')}#{'/' if end_slash}" 
  end 
end

puts '\\server\share\folder'.to_path(true) #//server/share/folder/
puts 'c:\folder'.to_path      #c:/folder
peter
  • 41,770
  • 5
  • 64
  • 108
-1

The portable way to write paths is with Ruby's File#join method. This will create OS-independent paths, using the right path separators.

For UNC paths, this previous answer addresses the creation of a custom File#to_unc method:

def File.to_unc( path, server="localhost", share=nil )
  parts = path.split(File::SEPARATOR)
  parts.shift while parts.first.empty?
  if share
    parts.unshift share
  else
    # Assumes the drive will always be a single letter up front
    parts[0] = "#{parts[0][0,1]}$" 
  end
  parts.unshift server
  "\\\\#{parts.join('\\')}"
end

I haven't tried it myself, but it would appear to be the result you're looking for.

Community
  • 1
  • 1
Todd A. Jacobs
  • 81,402
  • 15
  • 141
  • 199
  • it's the opposite of what i'm asking for, i need to start with a UNC path or local path that i can copy and use in explorer and is converted to a usable path in Ruby – peter Jun 07 '12 at 12:11
  • File.join always uses /, on all platforms. – Ákos Vandra-Meyer Nov 07 '16 at 16:05
  • I'm finding that `Dir.glob(File.join(__dir__, + '/foobar/*')` works on windows, but `Dir.glob(File.join(__dir__ + '\foobar\*')` does _not_ work. It seems File.join isn't creating OS-independent paths for me (ruby 3.1.2 on windows 10) – spuder Aug 16 '22 at 17:35