Ruby / Rails Syntax

How does this syntax work?

before_validation { |user| user.email = email.downcase } 

I would think it should be like this:

 before_validation { |user| user.email = user.email.downcase } 

Thank you for your help!

+4
source share
2 answers

It works because

 before_validation { |user| user.email = email.downcase } 

AS WELL AS

 before_validation { |user| user.email = self.email.downcase } 
+5
source

This works, but keep the DRY principle of Ruby. That would be better:

 before_validation { |user| user.email.downcase! } 

! reflects changes back to the receiving object, and also saves a few keystrokes.

-1
source

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


All Articles