Strange ruby ​​syntax

what syntax is in the basics of action for the mail client rails configuration 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 

How do I understand the design, for example

 from "Some text for this field" 

Is this assigning a value to a variable called "from"?

+4
source share
4 answers

No, this is a method call. The name of the from method, and the argument is a string. In Ruby, parentheses around method calls are optional, so

 from "Some text for this field" 

coincides with

 from("Some text for this field") 

Rails (and many Ruby libraries) like to express code in a natural language style, however, therefore, the version in parentheses is not read better, therefore it is used in examples.

+15
source

This is a call to from with "Some text for this field"

The method comes from the ActionMailer::Base class from which your UserMailer is distributed.

In Ruby, parentheses around a method call are optional unless something is ambiguous, so the statement is equivalent to a query ("Some text for this field")

Rails has a coding style that, if possible, prefers to be close to the natural language, therefore, without using parentheses, if necessary.

Calling this method sets the @from instance @from to the value you provide so that it can be used later when sending the message.

Usually, when you have access methods for getting and setting a variable, you have from= to set the value and from to return the value, however ActionMailer uses something called adv_attr_accessor to determine from so that if you call it with a parameter, it acts like a setter, but if you call it without parameters, it acts like a receiver.

This can be seen in the file actionmailer-2.xx / lib / action_mailer / base.rb and actionmailer-2.xx / lib / action_mailer / adv_attr_accessor.rb

+2
source

This is not a task. In Ruby, jobs are executed using the assignment operator = as follows:

 var = val 

You are probably thinking of some Lisp dialects where the assignment is as follows:

 (def var val) 

This is just a useless post message.

In Ruby, the general syntax for sending a message is

 receiver.selector(argument1, argument2) 

However, if receiver is self , you can leave receiver , therefore

 selector(argument1, argument2) 

coincides with

 self.selector(argument1, argument2) 

[Note: this is not entirely true. In Ruby, private methods can only be called when sending a message without a receiver, so if in this example self responds to a selector message by calling a private method, only the first option will work, the second will raise a NoMethodError exception.]

In addition, in cases where there is no ambiguity, you can leave parentheses around such arguments:

 receiver.selector argument1, argument2 

If you put two things together, you can now see that

 selector argument1, argument2 

equivalently

 self.selector(argument1, argument2) 

and therefore

 from "Some text for this field" 

equivalently

 self.from("Some text for this field") 

There is a third shortcut in the syntax for sending a Ruby message: if the most recent argument for sending the message is a Hash literal, then you can leave curly braces. Thus, the last line in the above example can also be written as

  body :user => user, :url => "http://example.com/login" 

In addition, in Ruby 1.9, the letter Hash , where all Symbol keys can be written using alternative Hash syntax:

  { key1: val1, key2: val2 } 

matches old syntax

 { :key1 => val1, :key2 => val2 } 

which means that, at least in Ruby 1.9, this last line can also be written as

  body user: user, url: "http://example.com/login" 
+1
source

You can also call the from attribute. This is an email property, but how it is implemented is hidden from you (encapsulation). This is a good thing. This means that if the Rails core decided to change @from to several variables, you would not need to change any of your codes.

0
source

Source: https://habr.com/ru/post/1312961/


All Articles