如果使用这种方式只显示 public
文件夹下的 index.html
文件,它有效:
require 'rubygems'
require 'sinatra'
set :bind, '0.0.0.0'
get '/' do
File.read('index.html')
end
但是想显示当前文件夹下的所有文件,试了下面但不行。
require 'rubygems'
require 'sinatra'
set :bind, '0.0.0.0'
get '/' do
File.read('*.html')
end
出现错误:
Errno::ENOENT at /
No such file or directory @ rb_sysopen - *.html
最佳答案
使用 Dir.glob('*.html')
.例如,
Dir.glob('*.html'){ |f| File.read f }
注意:Dir.glob('*.html').each{ }
也可以,但是 each
是多余的。
关于ruby - 如何用 ruby 显示文件夹下的所有 html 文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52847549/