WEBrick errors on ruby 1.8, works fine on ruby 1.9

I have the following script which runs a web server and executes a command when a request is received.

#!/usr/bin/env ruby
require 'webrick'

server = WEBrick::HTTPServer.new(port: ARGV.first)
server.mount_proc '/' do |req, res|
  r10kstatus = system( "sudo r10k deploy environment -pv warn" )
  puts r10kstatus
end

trap 'INT' do server.shutdown end
server.start

Unfortunatly, I need to backport this to work on ruby 1.8 (since scripting rvm to work on distributed systems is a pain).

When I try and run the script on ruby 1.8, I get the following error:

/usr/bin/r10k_gitlab_webhook:19:in `load': /usr/lib/ruby/gems/1.8/gems/r10k_gitlab_webhook-0.1.0/bin/r10k_gitlab_webhook:4: syntax error, unexpected ':', expecting ')' (SyntaxError) server = WEBrick::HTTPServer.new(Port: ARGV.first) ^ /usr/lib/ruby/gems/1.8/gems/r10k_gitlab_webhook-0.1.0/bin/r10k_gitlab_webhook:4: syntax error, unexpected ')', expecting $end from /usr/bin/r10k_gitlab_webhook:19

How could this script be modified to work on ruby 1.8?

Update

I've changed 'Port' to 'port'

Update2

Comparing the ruby 1.8 and ruby 1.9 documentation for webrick, shows that there isn't a port parameter for the new method in 1.8

http://www.ruby-doc.org/stdlib-1.8.7/libdoc/webrick/rdoc/WEBrick/HTTPServer.html#method-c-new

http://www.ruby-doc.org/stdlib-1.9.3/libdoc/webrick/rdoc/WEBrick/HTTPServer.html#method-c-new

I've tried changing (Port: ARGV.first) to (port: ARGV.first) , yet it still gives the same error:

server = WEBrick::HTTPServer.new(port: ARGV.first) ^ r10k_gitlab_webhook_old.rb:4: syntax error, unexpected ')', expecting $end


The key: value syntax for hashes (where key is a symbol) was introduced in Ruby 1.9. For Ruby 1.8 you need to use the :key => value syntax:

server = WEBrick::HTTPServer.new(:Port => ARGV.first)

(and I think it is :Port ).

链接地址: http://www.djcxy.com/p/59196.html

上一篇: 通过rvm安装Ruby的问题(​​运行configure时出错)

下一篇: Ruby ruby​​ 1.8上的WEBrick错误,在Ruby 1.9上正常工作