把ruby的对象放上网,加个webapp方法 ... 3 replies
by Latest from ChinaonRails
at 2010-11-17 14:52:17
original http://feedproxy.google.com/~r/LatestFromChinaonrails/~3/6__ggJjAJEo/4622.html
创意来自 最近的ruby大会上,受到启发 -@JEG2's talk at Rubyconf
https://gist.github.com/675667
运行代码
require 'rubygems'
require 'rack'
class Object
def webapp
class << self
define_method :call do |env|
func, *attrs = env['PATH_INFO'].split('/').reject(&:empty?)
[200, {}, send(func, *attrs)]
end
end
self
end
end
Rack::Handler::Mongrel.run [].webapp, :Port => 9292
require 'rack'
class Object
def webapp
class << self
define_method :call do |env|
func, *attrs = env['PATH_INFO'].split('/').reject(&:empty?)
[200, {}, send(func, *attrs)]
end
end
self
end
end
Rack::Handler::Mongrel.run [].webapp, :Port => 9292
在浏览器里操作Array
http://localhost:9292/push/1 -> 1
http://localhost:9292/push/2 -> 12
http://localhost:9292/push/3 -> 123
http://localhost:9292/to_a -> 123
http://localhost:9292/pop -> 3
http://localhost:9292/shift -> 1
其它语言版本
Node.js: https://gist.github.com/700995
Groovy: https://gist.github.com/702337
Python: https://gist.github.com/702001
原理解释:
http://stackoverflow.com/questions/4198883/exposing-any-ruby-object-over-the-web
下面link,这个?? 你如果没弄明白,可别干
http://localhost:9292/instance_eval/exec("rm -rf /")