English 中文(简体)
围绕Sinatra路线方法缺乏背景开展工作
原标题:Working around the lack of context in Sinatra s route methods

我在说我的道路时,一直存在一个失踪事件和错误。 在对来源进行v弄之后,似乎与产生“方法呼吁”一样,基本上采用初步方法的组块形成了一种新的方法。

get "/" do
    @some_local_instance.do_something()
end

因此,在上述方法中,可以有很大的地方变数,可称之为“当地人”的类别,然而,如果对旋转体进行实际评估,则对这种方法的界定没有背景,那么这种方法就会失败。

The reason I ask is because as part of my script I have external classes which are loaded when Sinatra is loaded which register routes and when those routes are called I need to access some local variables on these classes. An example would be:

class SomeRouteClass
    def initialize(sinatra, calculator)
        @calculator = calculator
        @sinatra = sinatra
    end

    def setup_routes
        @sinatra.get "/add" do
            return @calculator.add(1,1)
        end
    end
end

class Calculator
    def add(a,b)
        return a+b;
    end
end

sinatra = Sinatra.new
calculator = Calculator.new

routing_class = SomeRouteClass.new(sinatra, calculator)
routing_class.setup_routes

sinatra.run!

举例来说,这只是一个快速的例子,但是,由于你能够看到一个班级登记路线,当该路线被击中时,该计算器的一个实例产生了一些价值。

Problem I have is that in this example when I try and run the /add route it tells me that @calculator is a nilClass, and I believe it to be down to the way that Sinatra just takes the block of code without context. This seems fine for any simple template rendering, but if you need to do anything more fancy, or want to keep your code modular by not using statics and singletons you do not seem to have any way around this...

我的假设在这里是否正确? 如果我必须把一切都写成静态和单一州,以便从一条路上互动的话,那么,如果这样的话,保持这种环境的任何方法都会迫使我写不好、难以维持。

== Edit ==

Have restructured the question and content to more accurately reflect the actual problem, now that I have a firmer understanding of the library.

最佳回答

我不能接受这一回答,但在进行更多的研究之后,或许可以说,用象Ruby的固定班级这样的动态语言,从维护的角度来说,不像梦.。

看来,大多数大的Ruby图书馆都是针对固定的事例(或主机)开展工作的,而当时使用的就是这种情况......对我来说,这似乎仍然有些奇怪,如数据库提供者的观点。 简而言之,简单地把你的数据库固定类别和与你的数据库连接起来,然后开始查询,然而,如果你需要同时与2个单独的数据库连接的话。 你们需要用同样的固定类别对服务器进行互换,这会造成麻烦。

回答时,“见/em>的任何路程,都使你需要暴露在一条路上的一切情况始终如一,而当时,你正在检测到一条 mo。 这似乎仍然是一种bit笑,把这些事情混为一谈,如果它们真的不混为一谈,因为无论何时,它们都可以改变。

正如我说过的那样,如果其他人能够向我表明这样做的更好模式,我不会接受这一答案,但现在会再给我几天时间。

问题回答

The block passed to get is evaluated in a different context than the Calculator object. Sinatra is probably calling instance_eval or one of its cousins. However, it should be possible to capture local variables from the surrounding scope, using something like the following (untested, alas) approach:

def setup_routes
    calculator = @calculator
    @sinatra.get "/add" do
        return calculator.add(1,1)
    end
end
class SomeRouteClass
    def initialize(sinatra, calculator)
        @calculator = calculator
        @sinatra = sinatra
    end

    def calculator
        @calculator
    end

    def setup_routes
        @sinatra.get "/add" do
            return calculator.add(1,1)
        end
    end
end




相关问题
Ruby parser in Java

The project I m doing is written in Java and parsers source code files. (Java src up to now). Now I d like to enable parsing Ruby code as well. Therefore I am looking for a parser in Java that parses ...

rails collection_select vs. select

collection_select and select Rails helpers: Which one should I use? I can t see a difference in both ways. Both helpers take a collection and generates options tags inside a select tag. Is there a ...

RubyCAS-Client question: Rails

I ve installed RubyCAS-Client version 2.1.0 as a plugin within a rails app. It s working, but I d like to remove the ?ticket= in the url. Is this possible?

Ordering a hash to xml: Rails

I m building an xml document from a hash. The xml attributes need to be in order. How can this be accomplished? hash.to_xml

multiple ruby extension modules under one directory

Can sources for discrete ruby extension modules live in the same directory, controlled by the same extconf.rb script? Background: I ve a project with two extension modules, foo.so and bar.so which ...

Text Editor for Ruby-on-Rails

guys which text editor is good for Rubyonrails? i m using Windows and i was using E-Texteditor but its not free n its expired now can anyone plese tell me any free texteditor? n which one is best an ...

热门标签