Wiki » 履歴 » バージョン 2
Yuumi Yoshida, 2014-01-08 15:52
1 | 1 | Yuumi Yoshida | h1. Wiki |
---|---|---|---|
2 | |||
3 | h3. CGI用サーバー |
||
4 | |||
5 | <pre> |
||
6 | #!/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 | </pre> |
||
27 | 2 | Yuumi Yoshida | |
28 | h3. 階上を計算するCGI |
||
29 | |||
30 | <pre> |
||
31 | #!/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 | </pre> |