Ruby on rails跨平台win / linux脚本

我想使用Ruby on Rails应用程序列出子目录中的文件/目录,但我不知道该脚本是否可以在Linux或Windows下运行。

在Linux上这很容易,我可以做一个

`find my_path`.split("\n").each{|line| do_sthing_with_line} 

在Windows上,相当于使用dir命令。 但是即使在阅读了很多文章之后,我仍然无法find使其正确运行的方法

 `dir my_path` will output a string that is recognized as ruby as utf-8, but which in reality isn't. 

什么是正确的方式,使其在Windows上工作? 有没有一个快速的方法来检查我是否在窗户上? on_windows? dir_command : find_command

编辑:有没有一种快速的方式来生成这样的目录的内容树视图?

 -- rails_app -- -- app/ -- -- -- models/ -- -- -- controllers/ -- -- -- views/ -- -- bin/ -- -- config/ etc. 

我根本不会使用子命令。 内置的Dir.glob应该在这种情况下工作。

就像是:

 Dir.glob("#{path}/**/**").each { |fileOrDir| do_something } 

在任何情况下,你的find是递归的,但你的dir不会。

如果你真的想知道是否在Windows上运行,有一些以Ruby为中心的方法,但多年来我一直在检查环境中的WINDIR变量,例如

 if ENV["WINDIR"] puts "On Windows" else puts "Not Windows *probably*" end 

编辑:这是我几年前写的一个工具,生成一个节点树,然后显示它们以各种方式排序。

 def usage puts <<END Recursive listing of files or dirs, sortable by date or size or count. rl [-cdfnrsuv] [-p pathname] [pathname ...] Where: pathname = Dir or file to process; default is ".". -c = Sort by file count; default is sort by date. -d = List dirs only, with their contents sizes and counts. -f = List files only, no dirs or links. -n = Sort by name; default is sort by date. -p = Add pathname even if it starts with "-". -r = Reverse sort order; default order is desc, except by name is asc. -s = Sort by size; default is sort by date. -u = Unsorted; default is sort by date. -v = Verbose, including type, perms, and owner. END exit(1) end # usage class Node attr_reader :path, :stat def load(path) @path, @stat, @children = path, File.lstat(path), [] @stat.directory? and Dir.glob("#{path}/*", File::FNM_DOTMATCH).each { |sub_path| sub_path[-2,2] != "/." && sub_path[-3,3] != "/.." and @children << Node.new.load(sub_path) } self end def size @size or @size = self.stat.directory? ? (@children.inject(0) { |acc, child| acc + child.size }) : @stat.size end def count @count or @count = self.stat.directory? ? (@children.inject(0) { |acc, child| acc + child.count }) : 1 end def to_a @children.map { |child| child.to_a }.flatten + [self] end end # Node only_dirs = only_files = by_count = by_name = by_sz = verbose = false; sort = 1; paths = [] while (arg = ARGV.shift) arg =~ /^-[^-]*[h?]/ and usage arg =~ /^-[^-]*c/ and by_count = true arg =~ /^-[^-]*d/ and only_dirs = true arg =~ /^-[^-]*f/ and only_files = true arg =~ /^-[^-]*n/ and by_name = true arg =~ /^-[^-]*r/ and sort *= -1 arg =~ /^-[^-]*s/ and by_sz = true arg =~ /^-[^-]*u/ and sort = 0 arg =~ /^-[^-]*v/ and verbose = true arg =~ /^-[^-]*p/ and paths << ARGV.shift arg !~ /^-/ and paths << arg end nodes = (paths.empty? ? ["."] : paths).map { |path| Node.new.load(path).to_a }.flatten if sort != 0 if by_sz then nodes.sort! { |a, b| sort * (2 * (b.size <=> a.size) + (a.path <=> b.path)) } elsif by_count then nodes.sort! { |a, b| sort * (2 * (b.count <=> a.count) + (a.path <=> b.path)) } elsif by_name then nodes.sort! { |a, b| sort * (a.path <=> b.path) } else nodes.sort! { |a, b| sort * (2 * (b.stat.mtime <=> a.stat.mtime) + (a.path <=> b.path)) } end end for node in nodes next if only_dirs && ! node.stat.directory? next if only_files && ! node.stat.file? puts "%s %11s %6s %s%s" % [ node.stat.mtime.strftime("%Y-%m-%d %H:%M:%S"), node.size.to_s.reverse.gsub(/(\d{3})(?=\d)(?!\d*\.)/, "\\1,").reverse, node.count.to_s.reverse.gsub(/(\d{3})(?=\d)(?!\d*\.)/, "\\1,").reverse, verbose ? "%-9s %6o %4d %4d " % [:ftype, :mode, :uid, :gid].map { |v| node.stat.send(v) } : "", node.path] end