【问题标题】:String "<<" operator producing warnings and unexpected result in Ruby1.9.3字符串“<<”运算符在 Ruby1.9.3 中产生警告和意外结果
【发布时间】:2013-01-24 11:36:34
【问题描述】:

请找到我从IRB 终端运行的代码:

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>

为什么会出现这样的\\?如何解决?

当我从脚本中运行相同的部分时,会收到以下警告:

代码

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)

警告:

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

您能说说为什么会出现这样的警告吗?

当从IRB 再次尝试以下不同的输出时:

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"

编辑:

错误

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

来自脚本代码部分:

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)

我希望 src and dest 目录作为真正的目录路径。如何获得?

谢谢。

【问题讨论】:

  • “downloadv1.rb”中有什么内容?
  • 为什么你认为是&lt;&lt; 操作符引起了警告?
  • downloadv1.rb 是一个脚本,它正在下载src 目录中的文件,当关闭时,我试图将文件夹内容移动到dest 目录。但我的坏事出了问题。

标签: ruby ruby-1.9.3


【解决方案1】:

从广义上讲,Ruby 有两种类型的字符串。在双引号字符串中,反斜杠“转义”字符 - 反斜杠后跟另一个字母会产生特殊字符。例如,"\n" 为您提供换行符。在单引号字符串中,反斜杠不会转义字符 - '\n' 只是一个反斜杠,后跟字母 n。 (实际上这不是 100% 正确的,例外是 '\'',它是单引号 - 否则将无法在单引号字符串中嵌入单引号。

这就是为什么你的单引号 src = 'E:\WIPData\Ruby\Scripts\TaxDocumentDownload' 可以工作,而双引号 src = "E:\WIPData\Ruby\Scripts\TaxDocumentDownload" 不行。

双反斜杠打印在那里,因为irb 在结果输出中使用inspect,它以双引号形式返回字符串(特殊字符转义):

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

它们实际上并不在字符串中,正如您在 puts 中看到的那样:

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

您遇到的错误是因为使用了双引号字符串,反斜杠被视为转义字符,因此您的字符串未终止:

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

解析方式与

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

这是一个语法错误。

你应该去阅读单引号和双引号字符串之间的区别。 Here's one resource.


一个快速的谷歌建议你可能在做require 'FileUtils'而不是require 'fileutils'This post 表示相同的警告一旦更改为后者就会消失。这是因为 Windows 的文件系统不区分大小写——对于 Ruby,FileUtils.rb 和 fileutils.rb 是两个不同的文件,但对于 Windows,它们是相同的。

【讨论】:

  • 我很困惑,你能建议用它src = "E:\WIPData\Ruby\Scripts\TaxDocumentDownload" - 怎么写,这样我就可以得到` not \`?
  • 正如我所说,它们实际上并不在字符串中。之所以会显示它们,是因为 irb 打印了 src.inspect,它转义了特殊字符,例如 \ "
  • 请查看我的EDIT 并提出建议。
  • 你投票给我Andy了吗?我现在有了概念,我之前有点困惑。接受和理解!
  • @RubyTheBang,你为什么要关注选票?提出好的问题,您将获得积分。通过创建正确的答案,您将获得更多。 Stack Overflow 是关于问答的,而不是关于积分的。
【解决方案2】:

FileUtils 警告是因为您必须更改所需的 gem,如下所示:

require 'FileUtils' WRONG
require 'fileutils' OKAY

这将解决您的警告:)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-06-18
    • 1970-01-01
    • 1970-01-01
    • 2015-12-11
    • 1970-01-01
    • 1970-01-01
    • 2019-08-02
    • 2020-11-08
    相关资源
    最近更新 更多