Wiki » 履歴 » バージョン 3
Yuumi Yoshida, 2015-08-03 22:24
1 | 3 | Yuumi Yoshida | # Wiki |
---|---|---|---|
2 | 1 | Yuumi Yoshida | |
3 | 3 | Yuumi Yoshida | ### CGI用サーバー |
4 | 1 | Yuumi Yoshida | |
5 | 3 | Yuumi Yoshida | ~~~ |
6 | 1 | Yuumi Yoshida | #!/usr/bin/env ruby |
7 | |||
8 | require 'webrick' |
||
9 | |||
10 | document_root = './' |
||
11 | rubybin = '~/.rbenv/shims/ruby' |
||
12 | |||
13 | server = WEBrick::HTTPServer.new({ |
||
14 | DocumentRoot: document_root, |
||
15 | BindAddress: '0.0.0.0', |
||
16 | CGIInterpreter: rubybin, |
||
17 | Port: 8080 |
||
18 | }) |
||
19 | |||
20 | |||
21 | ['INT', 'TERM'].each {|signal| |
||
22 | Signal.trap(signal){ server.shutdown } |
||
23 | } |
||
24 | |||
25 | server.start |
||
26 | 3 | Yuumi Yoshida | ~~~ |
27 | 2 | Yuumi Yoshida | |
28 | 3 | Yuumi Yoshida | ### 階上を計算するCGI |
29 | 2 | Yuumi Yoshida | |
30 | 3 | Yuumi Yoshida | ~~~ |
31 | 2 | Yuumi Yoshida | #!/usr/bin/env ruby |
32 | |||
33 | require 'cgi' |
||
34 | |||
35 | def fact(n) |
||
36 | n == 0 ? 1 : n * fact(n - 1) |
||
37 | end |
||
38 | |||
39 | cgi = CGI.new |
||
40 | n = cgi['n'].to_i |
||
41 | print cgi.header |
||
42 | puts "" |
||
43 | puts "" |
||
44 | puts "#{n} ! = #{fact(n)}" |
||
45 | puts "" |
||
46 | 3 | Yuumi Yoshida | ~~~ |