奇怪的ruby语法

Ant*_*nAL 4 ruby syntax

Action Mailer Basics rails guide中的语法是什么?

class UserMailer < ActionMailer::Base
   def welcome_email(user)
      recipients    user.email
      from          "My Awesome Site Notifications <notifications@example.com>"
      subject       "Welcome to My Awesome Site"
      sent_on       Time.now
      body          {:user => user, :url => "http://example.com/login"}
   end
end
Run Code Online (Sandbox Code Playgroud)

我应该如何理解建筑,比如

from "Some text for this field"
Run Code Online (Sandbox Code Playgroud)

它是赋值变量的值,称为"from"吗?

mip*_*adi 15

不,这是方法调用.方法的名称是from,参数是一个字符串.在Ruby中,方法调用的括号是可选的,所以

from "Some text for this field"
Run Code Online (Sandbox Code Playgroud)

是相同的

from("Some text for this field")
Run Code Online (Sandbox Code Playgroud)

Rails(以及许多Ruby库)喜欢以自然语言风格表达代码,因此无括号的版本读取更好,因此在示例中使用它的原因.