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