Path problem on Windows: backslash vs forward slash

I can’t figure out why the below code works fine:

#encoding: utf-8
require ‘spreadsheet’

file_path =’\\server100\Folder1\Folder2\oléolé-file.xls’
Spreadsheet.client_encoding = ‘UTF-8’
Spreadsheet.open(file_path) do |book|
puts “sheets: #{book.worksheets.size}”
sheet = book.worksheet(0)
puts “First sheet name: #{sheet.name}”
end

But when I tried to do almost the same, just searching the most recent
file and pass it to the Spreadsheet like that, it doesn’t work. Ruby
replaces back slashes by forward ones what causes a problem on Windows
box:

#encoding: utf-8
require ‘spreadsheet’

folder = ‘’\\server100\Folder1\Folder2’
pattern = ‘oléolé-*.xls’
Dir.chdir(folder)
source_files = Dir.glob(pattern)
puts “source files: #{source_files.inspect}”
recent_file = source_files.max_by { |f1| File.mtime(f1) }
Spreadsheet.client_encoding = ‘UTF-8’
Spreadsheet.open(file_path) do |book|
puts “sheets: #{book.worksheets.size}”
sheet = book.worksheet(0)
puts “First sheet name: #{sheet.name}”
end

The error I get is:

spreadsheet-0.6.8/lib/spreadsheet/excel/reader.rb:1172:in setup': undefined methodread’ for false:FalseClass (NoMethodError)

I’m on Windows XP SP3, Ruby 1.9.3.
Thank you

Is it maybe to do with

‘’\\server100\Folder1\Folder2’
should be
‘\\server100\Folder1\Folder2’

without the double ‘’ at the start

No, I think there was a post format problem. Every path is between
DOUBLE quotes:

folder = “\\server100\Folder1\Folder2”

It looks like you’re not setting the file_path variable.

Or, you cut out some part of code before posting here. Please show us
a complete working (that is, not working :wink: ) example.

– Matma R.

Hate to break it to you, Jake:

https://github.com/ruby/ruby/blob/trunk/file.c#L5444

hi Serguei,

can you use File::Separator instead of either forward or backward
slashes?

  • j

Steve K. wrote in post #1053672:

Hate to break it to you, Jake:

https://github.com/ruby/ruby/blob/trunk/file.c#L5444

oops :wink:

what a drag…

  • j

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