N
Nanyang Zhan
Forgive me for the nonstandard words. I will describe my problem in
details:
I am doing a rails app, and met a ruby problem.
Inside a model class, I will need to write these codes for "apple"
def apple_attr
apples.collect {|o| o.name}.join(" ")
end
def apple_attr=(str)
@apple = str
end
def save_apple
#lots of codes
end
then banana, cat, dog.. each will have a copy of above codes. They are
the same, except replacing the word "apple" with "banana", "cat",
"dog"....
I know there is a way to put these code in to a module, then mixin the
module with current class, after that, you just need to call a method
(like setup_methodsapple); setup_methodsdog)...), instead of typing
the repeated code.
Would any one tell me how to do it?
details:
I am doing a rails app, and met a ruby problem.
Inside a model class, I will need to write these codes for "apple"
def apple_attr
apples.collect {|o| o.name}.join(" ")
end
def apple_attr=(str)
@apple = str
end
def save_apple
#lots of codes
end
then banana, cat, dog.. each will have a copy of above codes. They are
the same, except replacing the word "apple" with "banana", "cat",
"dog"....
I know there is a way to put these code in to a module, then mixin the
module with current class, after that, you just need to call a method
(like setup_methodsapple); setup_methodsdog)...), instead of typing
the repeated code.
Would any one tell me how to do it?