Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby/20.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何用ruby显示文件夹下的所有html文件?_Ruby_Static - Fatal编程技术网

如何用ruby显示文件夹下的所有html文件?

如何用ruby显示文件夹下的所有html文件?,ruby,static,Ruby,Static,如果使用这种方式仅显示公用文件夹下的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.re

如果使用这种方式仅显示公用文件夹下的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'。每个{}也可以工作,但每个都是多余的。

使用Dir.glob'*.html'。比如说,

Dir.glob('*.html'){ |f| File.read f }

注意:Dir.glob'*.html'。每个{}也可以工作,但每个都是多余的。

这个任务不需要使用sinatra,我认为您需要一些类似python的东西

请尝试以下近似方法:

在文件夹内的控制台中,尝试:

 ruby -run -e httpd -- -p 5000

╭─ ~/learn/ruby/ruby-way/stackoverflow/q-static-html-007/html 
╰─ tree
.
.
├── 1.html
└── 2.html

这项任务不需要使用sinatra,我认为您需要一些类似python的东西

请尝试以下近似方法:

在文件夹内的控制台中,尝试:

 ruby -run -e httpd -- -p 5000

╭─ ~/learn/ruby/ruby-way/stackoverflow/q-static-html-007/html 
╰─ tree
.
.
├── 1.html
└── 2.html

如何以这种方式排除某些文件或文件夹?如何以这种方式排除某些文件或文件夹?您不需要调用每个文件或文件夹,因为Dir.glob可以选择使用一个块。@cremno很好,谢谢!我已经相应地更新了答案。您不需要调用每个选项,因为Dir.glob可以选择性地占用一个块。@cremno说得好,谢谢!我已经相应地更新了答案。