这个ruby“方法”是如何工作的?

时间:2022-08-31 19:04:08

I am working with Sinatra but I am completely new to Ruby and confused about what the below code is actually doing.

我正在与Sinatra一起工作,但是我对Ruby是全新的,我不知道下面的代码实际上在做什么。

class Something < Sinatra::Base

   get '/' do
      'hello world'
   end

end

We don't seem to be defining a method. Are we calling the get method? If so, at what time is it called? I've not seen anything like this in other languages.

我们似乎没有定义一个方法。我们调用get方法了吗?如果是的话,它是什么时候打来的?我在其他语言中还没见过这种情况。

If we had 2 classes that extended Sinatra::Base how would Sinatra understand that the get applies to Something rather than the other class.

如果我们有两个扩展Sinatra的类::根据Sinatra如何理解get应用于某个东西而不是其他类。

1 个解决方案

#1


10  

As opposed to the way, e.g., Java functions, when you define classes in Ruby, Ruby is actually executing code. Kind of like Java's static blocks. So when you do e.g.

与Java函数相反,当您在Ruby中定义类时,Ruby实际上是在执行代码。有点像Java的静态块。所以当你这样做的时候。

class Foo
  puts(self)
end

you will open a class (i.e. change the current self to Foo), within its context do a puts (which will print out the Foo class object), and then close the class (returning self to what it was before).

您将打开一个类(即将当前的self更改为Foo),在其上下文中执行一个put(它将打印出Foo类对象),然后关闭这个类(返回self到它以前的值)。

get is a method defined on Sinatra::Base. Thus, your code is actually interpreted as if it were

get是在Sinatra::Base中定义的方法。因此,您的代码实际上被解释为它是

class Something < Sinatra::Base
  self.get('/') do
    'hello world'
  end
end

Because self (i.e. Foo) inherits from Sinatra::Base, that's a method we're invoking - and we're doing it as the Something class definition is being read.

因为self(即Foo)继承了Sinatra::Base,这是我们调用的方法——我们将它作为一个类定义被读取。

What that method does, roughly, is keep a table of "things to do when GET request comes in". It remembers that when it sees URL /, it should do the block do "hello world" end; more-or-less like this (example code; the original is a bit more complex):

该方法的作用大致是保存一个“在收到请求时要做的事情”的表。它记得当它看到URL /时,它应该做块做“hello world”结尾;差不多像这样(示例代码;原来的有点复杂):

class Sinatra::Base
  WHAT_TO_DO_ON_GET = {}

  def self.get(url, &thing_to_do)
    WHAT_TO_DO_ON_GET[url] = thing_to_do
  end
end

The Sinatra runtime is just a loop that, when a GET request comes in, looks up the URL against THINGS_TO_DO_ON_GET and executes what it finds there.

Sinatra运行时只是一个循环,当收到GET请求时,它会根据THINGS_TO_DO_ON_GET查找URL并执行在那里找到的内容。

#1


10  

As opposed to the way, e.g., Java functions, when you define classes in Ruby, Ruby is actually executing code. Kind of like Java's static blocks. So when you do e.g.

与Java函数相反,当您在Ruby中定义类时,Ruby实际上是在执行代码。有点像Java的静态块。所以当你这样做的时候。

class Foo
  puts(self)
end

you will open a class (i.e. change the current self to Foo), within its context do a puts (which will print out the Foo class object), and then close the class (returning self to what it was before).

您将打开一个类(即将当前的self更改为Foo),在其上下文中执行一个put(它将打印出Foo类对象),然后关闭这个类(返回self到它以前的值)。

get is a method defined on Sinatra::Base. Thus, your code is actually interpreted as if it were

get是在Sinatra::Base中定义的方法。因此,您的代码实际上被解释为它是

class Something < Sinatra::Base
  self.get('/') do
    'hello world'
  end
end

Because self (i.e. Foo) inherits from Sinatra::Base, that's a method we're invoking - and we're doing it as the Something class definition is being read.

因为self(即Foo)继承了Sinatra::Base,这是我们调用的方法——我们将它作为一个类定义被读取。

What that method does, roughly, is keep a table of "things to do when GET request comes in". It remembers that when it sees URL /, it should do the block do "hello world" end; more-or-less like this (example code; the original is a bit more complex):

该方法的作用大致是保存一个“在收到请求时要做的事情”的表。它记得当它看到URL /时,它应该做块做“hello world”结尾;差不多像这样(示例代码;原来的有点复杂):

class Sinatra::Base
  WHAT_TO_DO_ON_GET = {}

  def self.get(url, &thing_to_do)
    WHAT_TO_DO_ON_GET[url] = thing_to_do
  end
end

The Sinatra runtime is just a loop that, when a GET request comes in, looks up the URL against THINGS_TO_DO_ON_GET and executes what it finds there.

Sinatra运行时只是一个循环,当收到GET请求时,它会根据THINGS_TO_DO_ON_GET查找URL并执行在那里找到的内容。