【问题标题】:Ruby - change path with getsRuby - 使用获取更改路径
【发布时间】:2014-07-11 22:14:59
【问题描述】:

我对本准则有一些问题。它说,我使用了错误的目录。我想通过用户输入使用“gets”(e_path=gets ; subdirs(epath))更改目录路径。 有什么建议吗?

当我使用 subdirs("/home/etc/") 时,一切正常。

   def subdirs(path)
        subdirs =[]
        folderdirs=[]
        Dir.chdir(path) 
        Dir["*"].each do |x|
            if File.directory? File.join x
                subdirs << x
            else
                folderdirs << x
            end
        end
        puts "____________________________"
        puts "Projekte:\n\n"
        puts subdirs
        puts "____________________________"
        puts "Datein:\n\n"
        puts folderdirs
    end

。 . .

puts "\n\nGeben Sie bitte den Pfad ein um ein Workspace zu erstellen:"
e_path=gets
sleep(1)
subdirs(e_path) #Here is the mistake

【问题讨论】:

  • 哇,它有效!!!这是为什么?你能解释一下吗? thxx
  • Max 下面的回答是正确的,得到的字符串末尾包含一个换行符 '\n',chomp 方法将其删除。

标签: ruby string file path


【解决方案1】:

gets 包含行终止符!

> gets
Hello, world!
=> "Hello, world!\n"

所以Dir.chdir 正在尝试更改为名称中带有换行符的目录。 chomp 删除尾随行终止符(如果存在)。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-11-16
    • 2012-09-17
    • 2019-01-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多