-1

Please find the code which I ran from the IRB terminal:

Microsoft Windows XP [Version 5.1.2600]
(C) Copyright 1985-2001 Microsoft Corp.

C:\Documents and Settings\rakshiar>irb
irb(main):001:0> src = 'E:\WIPData\Ruby\Scripts\TaxDocumentDownload'
=> "E:\\WIPData\\Ruby\\Scripts\\TaxDocumentDownload"
irb(main):002:0> dest = 'E:\WIPData\Ruby\Scripts'
=> "E:\\WIPData\\Ruby\\Scripts"
irb(main):003:0> dest<<'H00371101'
=> "E:\\WIPData\\Ruby\\ScriptsH00371101"
irb(main):004:0>

Why here such \\ is coming? How to fix that?

When i am running the same part from the script getting the below warnings:

CODE

src = 'E:\WIPData\Ruby\Scripts\TaxDocumentDownload'
dest = 'E:\WIPData\Ruby\Scripts'
dest<<'H00371101'
FileUtils.copy_entry(src, dest, preserve = false, dereference_root = false, remove_destination = false)

Warning:

Microsoft Windows XP [Version 5.1.2600]
(C) Copyright 1985-2001 Microsoft Corp.

C:\Documents and Settings\rakshiar>cd..

C:\Documents and Settings>cd..

C:\>e:

E:\>cd E:\WIPData\Ruby\Scripts

E:\WIPData\Ruby\Scripts>downloadv1.rb
C:/Ruby193/lib/ruby/1.9.1/FileUtils.rb:93: warning: already initialized constant
 OPT_TABLE
C:/Ruby193/lib/ruby/1.9.1/FileUtils.rb:1268: warning: already initialized consta
nt S_IF_DOOR
C:/Ruby193/lib/ruby/1.9.1/FileUtils.rb:1496: warning: already initialized consta
nt DIRECTORY_TERM
C:/Ruby193/lib/ruby/1.9.1/FileUtils.rb:1500: warning: already initialized consta
nt SYSCASE
C:/Ruby193/lib/ruby/1.9.1/FileUtils.rb:1619: warning: already initialized consta
nt LOW_METHODS
C:/Ruby193/lib/ruby/1.9.1/FileUtils.rb:1625: warning: already initialized consta
nt METHODS

Could you please say why such warnings are coming?

When try the below from the IRB again different output:

C:\Documents and Settings\rakshiar>irb
irb(main):001:0> src = "E:\WIPData\Ruby\Scripts\TaxDocumentDownload"
=> "E:WIPDataRubyScriptsTaxDocumentDownload"
irb(main):002:0> est = "E:\WIPData\Ruby\Scripts"
=> "E:WIPDataRubyScripts"
irb(main):003:0> est<<"H00371101"
=> "E:WIPDataRubyScriptsH00371101"
irb(main):004:0> est<<"H00371101"

EDIT:

ERROR

E:\WIPData\Ruby\Scripts>downloadv1.rb
E:/WIPData/Ruby/Scripts/downloadv1.rb:87: syntax error, unexpected tCONSTANT, ex
pecting $end
dest<<"H00371101"
                ^

From the script code part:

src = "E:\WIPData\Ruby\Scripts\TaxDocumentDownload"
dest = "E:\WIPData\Ruby\Scripts\"
dest<<"H00371101"
FileUtils.copy_entry(src, dest, preserve = false, dereference_root = false, remove_destination = false)

I want that src and dest directory as the real directory path. How to get that?

Thanks.

Arup Rakshit
  • 116,827
  • 30
  • 260
  • 317

2 Answers2

3

Ruby has, broadly speaking, two types of strings. In a double-quoted string, backslashes "escape" characters - a backslash followed by another letter produces a special character. For example, "\n" gives you a newline. Inside single-quoted strings, the backslash doesn't escape characters - '\n' is just a backslash followed by the letter n. (Actually this isn't 100% true, the exception is '\'' which is a single quote - otherwise there would be no way to embed a single quote in a single-quoted string.

That's why your single-quoted src = 'E:\WIPData\Ruby\Scripts\TaxDocumentDownload' will work, and double-quoted src = "E:\WIPData\Ruby\Scripts\TaxDocumentDownload" will not.

The double-backslashes are printed there because irb uses inspect on the resulting output, which returns the string in double-quoted form (with special characters escaped):

'"Hello," said Andy'.inspect # => "\"Hello,\" said Andy"

They're not really in the string, as you can see with puts:

puts '"Hello," said Andy' # => "Hello," said Andy

The error you have is because of using a double-quoted string, the backslashes are treated as escape characters, so your string is unterminated:

src = "E:\WIPData\Ruby\Scripts\"
dest<<"H00371101"

is parsed the same as

src = 'E:WIPDataRubyScripts"dest<<'H00371101

which is a syntax error.

You should go and read about the difference between single- and double-quoted strings. Here's one resource.


A quick google suggests that you might be doing require 'FileUtils' not require 'fileutils'? This post said that the same warnings disappeared once they changed to the latter. It's because Windows' file system is not case-sensitive - to Ruby, FileUtils.rb and fileutils.rb are two different files, but to Windows, they're the same.

Andrew Haines
  • 6,574
  • 21
  • 34
  • I am totally confused that, Could you suggest with it `src = "E:\WIPData\Ruby\Scripts\TaxDocumentDownload"` - How to write it,so that I can get `\` not `\\`? – Arup Rakshit Jan 24 '13 at 11:50
  • As I said, they're not really in the string. They are only displayed because `irb` prints `src.inspect` which escapes special characters like `\ ` and `"`. – Andrew Haines Jan 24 '13 at 11:52
  • See my `EDIT` and suggest please. – Arup Rakshit Jan 24 '13 at 11:58
  • Did you down vote me `Andy`? I am now set with conception, I was bit confused earlier. Accepted and understood! – Arup Rakshit Jan 24 '13 at 12:11
  • @RubyTheBang, why do you fixate on votes? Ask good questions and you'll gain points. You'll gain even more by creating correct answers. Stack Overflow is about Q&A, not about points. – the Tin Man Jan 24 '13 at 13:28
  • @theTinMan this community also for helping people who might stuck, and the same happened with me. And accordingly @andy helped me out. Who is the `mad` gave me down-vote. Which he/she was not supposed to do. `-ve vote` causes stop new posts. I am not interested with votes ever,as I am not here to be selected as a `president`, rather here to learn more and update my knowledge. But someones `crazyness andmadness` seems gonna stop me to do that! – Arup Rakshit Jan 24 '13 at 13:40
  • @RubyTheBang I was totally happy to help you, but there's a number of reasons why your post might be downvoted. There are two or three unrelated questions in it, and none of them are particularly relevant to the title. You could have found the answer to the `FileUtils` question in first hit on Google searching for `fileutils warning`. Stack Overflow *is* here to help, but its purpose is also to produce questions and answers that will be useful to future visitors. This question isn't likely to fit that criteria, so there is no reason why someone is not "supposed" to downvote it. – Andrew Haines Jan 24 '13 at 17:48
0

The FileUtils warning it is because you have to change your required gem, like this:

require 'FileUtils' WRONG
require 'fileutils' OKAY

This will solve your warnings :)

facundofarias
  • 2,973
  • 28
  • 27