Predefined method chains

Predefined method chains

Ruby core library has many methods that accept names of other methods to do their job. Examples are private, public, private_class_method, module_function etc. Many gem also incorporate this approach in their codebase. Memery example:


carbon.now.sh

See this method that deals with result. Let's try to do something similar.

The problem we are going to address is to run validations on new ActiveRecord object. We call it verify


carbon.now.sh

Then we need to add custom validator for that. It should accept multiple objects and copy errors.


carbon.now.sh

Then we can do the following


Looks a bit strange, but compact )))



要查看或添加评论,请登录

Andrii Baran的更多文章

  • Ruby language: module_function

    Ruby language: module_function

    Часто ? так, що нам потр?бно к?лька функц?й а притулити ?х н?куди. Просте ? елегантне р?шення це модуль де функц?? - це…

社区洞察

其他会员也浏览了