2012-01-07 39 views
1

我用屈,并有一个问题,我解决不了,这里是代码:Ruby方法范围如何工作?

require 'sinatra' 

def url(s) 
    get s do yield end 
    post s do yield end 
end 

url '/' do 
    erb :index 
end 

然后,程序提示,:未定义的方法`再培训局”主:对象

我该怎么办?

回答

1

你可以尝试这样的事:

require 'rubygems' 
require 'sinatra' 

def map_url(url, options={}, &block) 
    get(url, options, &block) 
    post(url, options, &block) 
end 

map_url '/' do 
    erb :index 
end 
+0

甚至:'def map_url(* args,&block); get(* args,&block); post(* args,&block); end'。这应该抓住一切。 – 2012-01-07 20:13:51