# How to define a method template

**URL:** <https://rubytalk.org/t/how-to-define-a-method-template/37423>\
**Category:** ruby-talk\
**Created:** [6 May 2007 08:36 UTC](https://rubytalk.org/t/how-to-define-a-method-template/37423 "2007-05-06T08:36:17Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![Nanyang\_Zhan1](https://avatars.discourse-cdn.com/v4/letter/n/94ad74/32.png) [@Nanyang\_Zhan1](https://rubytalk.org/u/Nanyang_Zhan1)\
**Post date:** [6 May 2007 08:36 UTC](https://rubytalk.org/t/how-to-define-a-method-template/37423/1 "2007-05-06T08:36:17Z")

</div>

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](http://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\_methods(:apple); setup\_methods(:dog)...), instead of typing  
the repeated code.

Would any one tell me how to do it?

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).

---

<div class="post-metadata">

**Author:** ![Robert\_K1](https://yyz1.discourse-cdn.com/flex029/user_avatar/rubytalk.org/robert_k1/32/1830_2.png) [@Robert\_K1](https://rubytalk.org/u/Robert_K1)\
**Post date:** [6 May 2007 10:50 UTC](https://rubytalk.org/t/how-to-define-a-method-template/37423/2 "2007-05-06T10:50:06Z")

</div>

> 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\_methods(:apple); setup\_methods(:dog)...), instead of typing  
> the repeated code.
> 
> Would any one tell me how to do it?

Why not create all the methods on the fly? Like

class Foo  
&nbsp;&nbsp;&nbsp;def setup\_methods(sym)  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;cl = class \<\<self;self;end  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;cl.class\_eval do  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;attr\_accessor sym  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;cl.class\_eval "def #{sym}\_save() puts 'saving #{sym}' end"  
&nbsp;&nbsp;&nbsp;end  
end

Now you can do

irb(main):010:0\> f=Foo.new  
=\> #\<Foo:0x7ff74f48\>  
irb(main):011:0\> f.apple  
NoMethodError: undefined method `apple' for #\<Foo:0x7ff74f48\>  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;from (irb):11  
irb(main):012:0\> f.setup\_methods :apple  
=\> nil  
irb(main):013:0\> f.apple  
=\> nil  
irb(main):014:0\> f.apple="foo bar"  
=\> "foo bar"  
irb(main):015:0\> f.apple  
=\> "foo bar"  
irb(main):016:0\> f.apple\_save  
saving apple  
=\> nil  
irb(main):017:0\>

Kind regards

&nbsp;&nbsp;robert

> **···**
>
> On 06.05.2007 10:36, Nanyang Zhan wrote:  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;from :0

---

<div class="post-metadata">

**Author:** ![Robert\_K1](https://yyz1.discourse-cdn.com/flex029/user_avatar/rubytalk.org/robert_k1/32/1830_2.png) [@Robert\_K1](https://rubytalk.org/u/Robert_K1)\
**Post date:** [6 May 2007 10:55 UTC](https://rubytalk.org/t/how-to-define-a-method-template/37423/3 "2007-05-06T10:55:04Z")

</div>

PS: of course you can define setup\_methods in a module and mixin that module.

> **···**
>
> On 06.05.2007 12:49, Robert Klemme wrote:
> 
> > On 06.05.2007 10:36, Nanyang Zhan wrote:
> > 
> > > 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\_methods(:apple); setup\_methods(:dog)...), instead of typing  
> > > the repeated code.
> > > 
> > > Would any one tell me how to do it?
> > 
> > Why not create all the methods on the fly? Like
> > 
> > class Foo  
> > &nbsp;&nbsp;def setup\_methods(sym)  
> > &nbsp;&nbsp;&nbsp;&nbsp;cl = class \<\<self;self;end  
> > &nbsp;&nbsp;&nbsp;&nbsp;cl.class\_eval do  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;attr\_accessor sym  
> > &nbsp;&nbsp;&nbsp;&nbsp;end  
> > &nbsp;&nbsp;&nbsp;&nbsp;cl.class\_eval "def #{sym}\_save() puts 'saving #{sym}' end"  
> > &nbsp;&nbsp;end  
> > end
> > 
> > Now you can do
> > 
> > irb(main):010:0\> f=Foo.new  
> > =\> #\<Foo:0x7ff74f48\>  
> > irb(main):011:0\> f.apple  
> > NoMethodError: undefined method `apple' for #\<Foo:0x7ff74f48\>  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;from (irb):11  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;from :0  
> > irb(main):012:0\> f.setup\_methods :apple  
> > =\> nil  
> > irb(main):013:0\> f.apple  
> > =\> nil  
> > irb(main):014:0\> f.apple="foo bar"  
> > =\> "foo bar"  
> > irb(main):015:0\> f.apple  
> > =\> "foo bar"  
> > irb(main):016:0\> f.apple\_save  
> > saving apple  
> > =\> nil  
> > irb(main):017:0\>
> > 
> > Kind regards
> > 
> > &nbsp;&nbsp;&nbsp;&nbsp;robert

---

<div class="post-metadata">

**Author:** ![Forum](https://avatars.discourse-cdn.com/v4/letter/f/43a26b/32.png) [@Forum](https://rubytalk.org/u/Forum)\
**Post date:** [6 May 2007 11:14 UTC](https://rubytalk.org/t/how-to-define-a-method-template/37423/4 "2007-05-06T11:14:17Z")

</div>

> \> 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\_methods(:apple); setup\_methods(:dog)...), instead of typing  
> \> the repeated code.  
> \>  
> \> Would any one tell me how to do it?
> 
> Why not create all the methods on the fly? Like
> 
> class Foo  
> &nbsp;&nbsp;&nbsp;def setup\_methods(sym)  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;cl = class \<\<self;self;end  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;cl.class\_eval do  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;attr\_accessor sym  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;cl.class\_eval "def #{sym}\_save() puts 'saving #{sym}' end"  
> &nbsp;&nbsp;&nbsp;end  
> end

Robert I have understood the setup a little bit differently, was it  
not on the class base that OP wanted his methods?

Your technique of course is correct, just that - if I were right - OP  
needed this

class Foo

&nbsp;&nbsp;&nbsp;%w{ apple banana mango strawberry }.each do  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\>fruit\>  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;define\_method "#{fruit}\_attr" do  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;send(fruit.pluralize).map{|f|f.name}.join(",")  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;define\_method "#{fruit}\_attr=" do  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\> str |  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;instance\_variable\_set("@" \<\< fruit, str)  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;define\_method "save\_" \<\< fruit do  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;# lots of code adapted to metaprogramming, which might be possible 🙂  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;# or not ☹  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end  
&nbsp;&nbsp;&nbsp;end  
end

> Now you can do
> 
> irb(main):010:0\> f=Foo.new  
> =\> #\<Foo:0x7ff74f48\>  
> irb(main):011:0\> f.apple  
> NoMethodError: undefined method `apple' for #\<Foo:0x7ff74f48\>  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;from (irb):11  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;from :0  
> irb(main):012:0\> f.setup\_methods :apple  
> =\> nil  
> irb(main):013:0\> f.apple  
> =\> nil  
> irb(main):014:0\> f.apple="foo bar"  
> =\> "foo bar"  
> irb(main):015:0\> f.apple  
> =\> "foo bar"  
> irb(main):016:0\> f.apple\_save  
> saving apple  
> =\> nil  
> irb(main):017:0\>
> 
> Kind regards
> 
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;robert

f = Foo.new # has methods like apples\_attr, save\_strawberry and  
banana\_attr= already, which might make sense or not

HTH  
Robert

> **···**
>
> On 5/6/07, Robert Klemme \<shortcutter@googlemail.com\> wrote:
> 
> > On 06.05.2007 10:36, Nanyang Zhan wrote:
> 
> --  
> You see things; and you say Why?  
> But I dream things that never were; and I say Why not?  
> -- George Bernard Shaw

---

<div class="post-metadata">

**Author:** ![Robert\_K1](https://yyz1.discourse-cdn.com/flex029/user_avatar/rubytalk.org/robert_k1/32/1830_2.png) [@Robert\_K1](https://rubytalk.org/u/Robert_K1)\
**Post date:** [6 May 2007 13:25 UTC](https://rubytalk.org/t/how-to-define-a-method-template/37423/5 "2007-05-06T13:25:05Z")

</div>

Maybe. His statement seems to be a bit contradictory. First he talks about all these methods being there. Then he mentions some method setup\_methods which I believe is to create those methods. But you're right, could also be a class method (like attr\_accessor). OP, what do you want - Robert's solution or Robert's solution? 🙂

Kind regards

&nbsp;&nbsp;robert

> **···**
>
> On 06.05.2007 13:14, Robert Dober wrote:
> 
> > On 5/6/07, Robert Klemme \<shortcutter@googlemail.com\> wrote:
> > 
> > > On 06.05.2007 10:36, Nanyang Zhan wrote:  
> > > \> 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\_methods(:apple); setup\_methods(:dog)...), instead of typing  
> > > \> the repeated code.  
> > > \>  
> > > \> Would any one tell me how to do it?
> > > 
> > > Why not create all the methods on the fly? Like
> > > 
> > > class Foo  
> > > &nbsp;&nbsp;&nbsp;def setup\_methods(sym)  
> > > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;cl = class \<\<self;self;end  
> > > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;cl.class\_eval do  
> > > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;attr\_accessor sym  
> > > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end  
> > > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;cl.class\_eval "def #{sym}\_save() puts 'saving #{sym}' end"  
> > > &nbsp;&nbsp;&nbsp;end  
> > > end
> > 
> > Robert I have understood the setup a little bit differently, was it  
> > not on the class base that OP wanted his methods?

---

<div class="post-metadata">

**Author:** ![Forum](https://avatars.discourse-cdn.com/v4/letter/f/43a26b/32.png) [@Forum](https://rubytalk.org/u/Forum)\
**Post date:** [6 May 2007 15:25 UTC](https://rubytalk.org/t/how-to-define-a-method-template/37423/6 "2007-05-06T15:25:15Z")

</div>

> \>\> \> 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\_methods(:apple); setup\_methods(:dog)...), instead of typing  
> \>\> \> the repeated code.  
> \>\> \>  
> \>\> \> Would any one tell me how to do it?  
> \>\>  
> \>\> Why not create all the methods on the fly? Like  
> \>\>  
> \>\> class Foo  
> \>\> def setup\_methods(sym)  
> \>\> cl = class \<\<self;self;end  
> \>\> cl.class\_eval do  
> \>\> attr\_accessor sym  
> \>\> end  
> \>\> cl.class\_eval "def #{sym}\_save() puts 'saving #{sym}' end"  
> \>\> end  
> \>\> end  
> \> Robert I have understood the setup a little bit differently, was it  
> \> not on the class base that OP wanted his methods?
> 
> Maybe. His statement seems to be a bit contradictory. First he talks  
> about all these methods being there. Then he mentions some method  
> setup\_methods which I believe is to create those methods. But you're  
> right, could also be a class method (like attr\_accessor). OP, what do  
> you want - Robert's solution or Robert's solution? 🙂

Difficult decision LOL

> **···**
>
> On 5/6/07, Robert Klemme \<shortcutter@googlemail.com\> wrote:
> 
> > On 06.05.2007 13:14, Robert Dober wrote:  
> > \> On 5/6/07, Robert Klemme \<shortcutter@googlemail.com\> wrote:  
> > \>\> On 06.05.2007 10:36, Nanyang Zhan wrote:
> > 
> > Kind regards
> > 
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;robert
> 
> --  
> You see things; and you say Why?  
> But I dream things that never were; and I say Why not?  
> -- George Bernard Shaw

---

<div class="post-metadata">

**Author:** ![Nanyang\_Zhan1](https://avatars.discourse-cdn.com/v4/letter/n/94ad74/32.png) [@Nanyang\_Zhan1](https://rubytalk.org/u/Nanyang_Zhan1)\
**Post date:** [6 May 2007 15:37 UTC](https://rubytalk.org/t/how-to-define-a-method-template/37423/7 "2007-05-06T15:37:13Z")

</div>

> OP, what do you want - Robert's solution or Robert's solution? 🙂
> 
> Kind regards
> 
> &nbsp;&nbsp;robert

Thanks RobertS!  
Now I'm pretty sure that people tend to using the programming languages  
that has the same character with their names, like Roberts will pick up  
Ruby while Johns prefer Java.

Robert Dober's solution offer me details on how to write the codes about  
defining those methods. It will be very useful, but still I have no idea  
where to put these definitions.  
Robert's solution(yes, that's Robert Klemme) needs to be adjusted to fit  
my requirement correctlly. But it's very interesting. Robert, Can you  
tell me how to make setup\_methods :apple can be called inside a class,  
like:

#magic codes put here, then  
Class Fruit \< ActiveRecord::Base  
# create all the methods on the fly  
setup\_methods :apple  
setup\_methods :banana  
....  
# call methods that are created on the fly inside a class  
after\_save :save\_apple  
after\_save :save\_banana  
end

That is, please tell me how to mixin.

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/\](http://www.ruby-forum.com/%5C).

---

<div class="post-metadata">

**Author:** ![Nanyang\_Zhan1](https://avatars.discourse-cdn.com/v4/letter/n/94ad74/32.png) [@Nanyang\_Zhan1](https://rubytalk.org/u/Nanyang_Zhan1)\
**Post date:** [6 May 2007 16:11 UTC](https://rubytalk.org/t/how-to-define-a-method-template/37423/8 "2007-05-06T16:11:15Z")

</div>

I just managed to apply Dober's codes into my app (I just copy the code  
into the class, and add some codes), It is just that simple, but works  
fine! Still, I get a problem, inside

> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;define\_method "save\_" \<\< fruit do  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;#HERE  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end

this method, I need to call a local method with the same name as string  
fruit contains. If it is a instance variable, I know there are  
instance\_variable\_set("@#{fruit}", str) and  
instance\_variable\_get("@#{fruit}") methods. But what about this local  
method? How can I read/set its value?

Though the problem inside the class is almost solved, but I still want  
to know the mixin solution, because with mixin solution, I can use these  
code almong other classes.

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/\](http://www.ruby-forum.com/%5C).

---

<div class="post-metadata">

**Author:** ![Robert\_K1](https://yyz1.discourse-cdn.com/flex029/user_avatar/rubytalk.org/robert_k1/32/1830_2.png) [@Robert\_K1](https://rubytalk.org/u/Robert_K1)\
**Post date:** [6 May 2007 16:25 UTC](https://rubytalk.org/t/how-to-define-a-method-template/37423/9 "2007-05-06T16:25:06Z")

</div>

> > OP, what do you want - Robert's solution or Robert's solution? 🙂
> > 
> > Kind regards
> > 
> > &nbsp;&nbsp;robert
> 
> Thanks RobertS!  
> Now I'm pretty sure that people tend to using the programming languages that has the same character with their names, like Roberts will pick up Ruby while Johns prefer Java.

Actually I do Java for a living - and I don't hate it. 🙂

> Robert Dober's solution offer me details on how to write the codes about defining those methods. It will be very useful, but still I have no idea where to put these definitions.  
> Robert's solution(yes, that's Robert Klemme) needs to be adjusted to fit my requirement correctlly. But it's very interesting. Robert, Can you tell me how to make setup\_methods :apple can be called inside a class, like:
> 
> #magic codes put here, then  
> Class Fruit \< ActiveRecord::Base  
> # create all the methods on the fly  
> setup\_methods :apple  
> setup\_methods :banana  
> ...  
> # call methods that are created on the fly inside a class  
> after\_save :save\_apple  
> after\_save :save\_banana  
> end
> 
> That is, please tell me how to mixin.

Basically you can define a method setup\_methods in class Class. Untested:

class Class  
&nbsp;&nbsp;&nbsp;def setup\_methods(sym)  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;class\_eval do  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;attr\_accessor sym  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;class\_eval "def #{sym}\_save() puts 'saving #{sym}' end"  
&nbsp;&nbsp;&nbsp;end  
end

Kind regards

&nbsp;&nbsp;robert

> **···**
>
> On 06.05.2007 17:37, Nanyang Zhan wrote:

---

<div class="post-metadata">

**Author:** ![Nanyang\_Zhan1](https://avatars.discourse-cdn.com/v4/letter/n/94ad74/32.png) [@Nanyang\_Zhan1](https://rubytalk.org/u/Nanyang_Zhan1)\
**Post date:** [6 May 2007 17:30 UTC](https://rubytalk.org/t/how-to-define-a-method-template/37423/10 "2007-05-06T17:30:58Z")

</div>

Robert Klemme wrote:

> Actually I do Java for a living - and I don't hate it. 🙂

why hate it? while it makes you living and others happy...

> Basically you can define a method setup\_methods in class Class.

Then I can call the methods it defines anywhere I like?

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/\](http://www.ruby-forum.com/%5C).
