在Ruby中使用foreach的奇怪问题

时间:2022-11-24 13:40:00

When I write below code in Ruby it gives me an error :

当我在Ruby中编写下面的代码时,它给了我一个错误:

x = "c:/"
y = "Users/Amiref"
z = "/"
h = "Downloads"
v= x+y+z+h
Dir.foreach("v") do |i|
  puts i
end

but when I use below code Ruby doesn't give me any error :

但是当我使用下面的代码时,Ruby不会给我任何错误:

Dir.foreach("c:/Users/Amiref/Downloads") do |i|
  puts i
end

I am really confused . please help me . thanks

我真的很困惑。请帮帮我 。谢谢

5 个解决方案

#1


4  

v shouldn't be quoted - it's being used as a variable and not a string.

v不应该被引用 - 它被用作变量而不是字符串。

#2


2  

It is because you quoted the v.

这是因为你引用了v。

This means you are going through a directory called v which probably does not exist. :)

这意味着您正在浏览一个名为v的目录,该目录可能不存在。 :)

Remove the quotes and it will be the directory you want.

删除引号,它将是您想要的目录。

#3


1  

You need Dir.foreach(v) without the quotes around v

你需要Dir.foreach(v)而没有v周围的引号

#4


1  

As already posted by the other answers: You may not quote 'v'. Another hint: Instead building the path by hand, you could use File#join:

正如其他答案已经公布的那样:你可能不引用'v'。另一个提示:您可以使用File#join来手动构建路径:

dir= File.join("c:", "Users", "Amiref", "Downloads")
Dir.foreach(dir) do |i|
  puts i
end

#5


0  

The code

Dir.foreach("v") do |i|
  puts i
end

tries to loop through a "directory" called "v" (most likely \v or c:\v) and print all files and directories below that one.

尝试循环遍历名为“v”的“目录”(很可能是\ v或c:\ v)并打印该目录下的所有文件和目录。

What you really meant was probably

你真正想要的可能是什么

x = "c:/"
y = "Users/Amiref"
z = "/"
h = "Downloads"
v= x+y+z+h
Dir.foreach(v) do |i|
  puts i
end

However I would recommend using File#join to join folders together, that way you become platform independent (somewhat anyhow) and don't have to fiddle with windows backslashes () and unix style slashes (/) like this:

但是我建议使用File#join将文件夹连接在一起,这样你就可以成为独立于平台的(无论如何)并且不必像这样调整windows backslashes()和unix样式斜杠(/):

dir_path = File.join("C:", "Users", "Amiret", "Downloads")
Dir.foreach(dirpath) do |dir|
  puts dir
end

#1


4  

v shouldn't be quoted - it's being used as a variable and not a string.

v不应该被引用 - 它被用作变量而不是字符串。

#2


2  

It is because you quoted the v.

这是因为你引用了v。

This means you are going through a directory called v which probably does not exist. :)

这意味着您正在浏览一个名为v的目录,该目录可能不存在。 :)

Remove the quotes and it will be the directory you want.

删除引号,它将是您想要的目录。

#3


1  

You need Dir.foreach(v) without the quotes around v

你需要Dir.foreach(v)而没有v周围的引号

#4


1  

As already posted by the other answers: You may not quote 'v'. Another hint: Instead building the path by hand, you could use File#join:

正如其他答案已经公布的那样:你可能不引用'v'。另一个提示:您可以使用File#join来手动构建路径:

dir= File.join("c:", "Users", "Amiref", "Downloads")
Dir.foreach(dir) do |i|
  puts i
end

#5


0  

The code

Dir.foreach("v") do |i|
  puts i
end

tries to loop through a "directory" called "v" (most likely \v or c:\v) and print all files and directories below that one.

尝试循环遍历名为“v”的“目录”(很可能是\ v或c:\ v)并打印该目录下的所有文件和目录。

What you really meant was probably

你真正想要的可能是什么

x = "c:/"
y = "Users/Amiref"
z = "/"
h = "Downloads"
v= x+y+z+h
Dir.foreach(v) do |i|
  puts i
end

However I would recommend using File#join to join folders together, that way you become platform independent (somewhat anyhow) and don't have to fiddle with windows backslashes () and unix style slashes (/) like this:

但是我建议使用File#join将文件夹连接在一起,这样你就可以成为独立于平台的(无论如何)并且不必像这样调整windows backslashes()和unix样式斜杠(/):

dir_path = File.join("C:", "Users", "Amiret", "Downloads")
Dir.foreach(dirpath) do |dir|
  puts dir
end