Ruby on rails跨平台win/linux脚本
我想使用RubyonRails应用程序列出子目录中的文件/目录,但我不知道脚本是在Linux还是Windows下运行 在linux上这很简单,我只需要做一个Ruby on rails跨平台win/linux脚本,ruby,linux,windows,Ruby,Linux,Windows,我想使用RubyonRails应用程序列出子目录中的文件/目录,但我不知道脚本是在Linux还是Windows下运行 在linux上这很简单,我只需要做一个 `find my_path`.split("\n").each{|line| do_sthing_with_line} 在windows上,等效的方法是使用dir命令。但是,即使在阅读了很多关于这方面的帖子之后,我也无法找到一种方法使它正确工作 `dir my_path` will output a string that is reco
`find my_path`.split("\n").each{|line| do_sthing_with_line}
在windows上,等效的方法是使用dir
命令。但是,即使在阅读了很多关于这方面的帖子之后,我也无法找到一种方法使它正确工作
`dir my_path` will output a string that is recognized as ruby as utf-8, but which in reality isn't.
让它在windows上工作的正确方法是什么?
有没有快速的方法来检查我是否在windows上<代码>在windows上?dir_命令:find_命令
编辑:有没有一种快速的方法可以生成这样一个目录内容的树状视图
-- 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
def使用
还有一点小意思,您的查找也会给出以
开头的名称,但是目录glob
不会,除非重写为:Dir.glob(“{path}/****”,File::FNM\u DOTMATCH)
,并且您需要忽略以“/。
或“/…”
结尾的路径。谢谢,它很有魅力。有没有一种快速生成树状视图的方法?(请参阅我的编辑)我添加了一些代码,它应该有一些部分可以在您自己的代码中用于目录树或文件节点。这是一种传统的Ruby思维方式,使用系统工具和功能而不是语言功能,当您尝试实现平台包容性时,这种思维方式有点落伍。这只是务实或彻底的权衡。