简体   繁体   中英

Ruby / Rails syntax

How does this syntax work?

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

I would think that it would need to be this:

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

Thanks for your help!

It work because

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

SAME AS

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

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

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

! reflects the changes back onto the receiving object and it also saves a few keystrokes.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM