Ruby optimized inheritance class instances and ruby inheritance instances

Source: Internet
Author: User

Ruby optimized inheritance class instances and ruby inheritance instances

In the Rails principles, there are statements about simplifying the Controller and enriching the Model. The following example shows a simple inheritance optimization. Especially when there is a variable self written in your method.

Suppose we have the following Model:
Copy codeThe Code is as follows:
Class SubjectMatterExpert <User
Include HashCodeCreatorModule
Def make_activation_code
Self. deleted_at = nil
Self. activation_code | = make_hash_code
End
End

Class Administrator <User
Include HashCodeCreatorModule
Def make_activation_code
Self. deleted_at = nil
Self. activation_code | = make_hash_code
End
End

We can use the inherited simplified code as follows: SubjectMatterExpert Administrator

Copy codeThe Code is as follows:
Class SubjectMatterExpert <User
End

Class Administrator <User
End

Class User
Include HashCodeCreatorModule
Def make_activation_code
Self. deleted_at = nil
Self. activation_code | = make_hash_code
End

We can also use this in testing:

Copy codeThe Code is as follows:
It 'should create an activation Code' do
Admin = Factory (: administrator)
Admin. make_activation_code
Admin. activation_code.should_not be_empty
End

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.