Forum: Ruby how to define a method template

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
C91098dc76d7ad473165ef24fe805312?d=identicon&s=25 Nanyang Zhan (xain)
on 2007-05-06 10:36
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?
E0d864d9677f3c1482a20152b7cac0e2?d=identicon&s=25 Robert Klemme (Guest)
on 2007-05-06 12:50
(Received via mailing list)
On 06.05.2007 10:36, Nanyang Zhan wrote:
> end
> 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

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>
         from (irb):11
         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

  robert
E0d864d9677f3c1482a20152b7cac0e2?d=identicon&s=25 Robert Klemme (Guest)
on 2007-05-06 12:55
(Received via mailing list)
On 06.05.2007 12:49, Robert Klemme wrote:
>>  @apple = str
>> I know there is a way to put these code in to a module, then mixin the
>     cl = class <<self;self;end
> => #<Foo:0x7ff74f48>
> irb(main):015:0> f.apple
> => "foo bar"
> irb(main):016:0> f.apple_save
> saving apple
> => nil
> irb(main):017:0>
>
> Kind regards
>
>     robert

PS: of course you can define setup_methods in a module and mixin that
module.
703fbc991fd63e0e1db54dca9ea31b53?d=identicon&s=25 Robert Dober (Guest)
on 2007-05-06 13:14
(Received via mailing list)
On 5/6/07, Robert Klemme <shortcutter@googlemail.com> wrote:
> >  @apple = str
> > I know there is a way to put these code in to a module, then mixin the
>      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?

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

class Foo

   %w{ apple banana mango strawberry }.each do
        |fruit|
        define_method "#{fruit}_attr" do
            send(fruit.pluralize).map{|f|f.name}.join(",")
        end
        define_method "#{fruit}_attr=" do
           | str |
           instance_variable_set("@" << fruit, str)
        end
        define_method "save_" << fruit do
          # lots of code adapted to metaprogramming, which might be
possible :)
          # or not :(
        end
   end
end
> => nil
>
> Kind regards
>
>         robert
>
>

f = Foo.new # has methods like apples_attr, save_strawberry and
banana_attr= already, which might make sense or not

HTH
Robert
E0d864d9677f3c1482a20152b7cac0e2?d=identicon&s=25 Robert Klemme (Guest)
on 2007-05-06 15:26
(Received via mailing list)
On 06.05.2007 13:14, Robert Dober wrote:
>> > def apple_attr=(str)
>> >
>>    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? :-)

Kind regards

  robert
703fbc991fd63e0e1db54dca9ea31b53?d=identicon&s=25 Robert Dober (Guest)
on 2007-05-06 17:25
(Received via mailing list)
On 5/6/07, Robert Klemme <shortcutter@googlemail.com> wrote:
> >> >
> >> > "dog"....
> >> class Foo
>
> 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
C91098dc76d7ad473165ef24fe805312?d=identicon&s=25 Nanyang Zhan (xain)
on 2007-05-06 17:37
> OP, what do you want - Robert's solution or Robert's solution? :-)
>
> Kind regards
>
>   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.
C91098dc76d7ad473165ef24fe805312?d=identicon&s=25 Nanyang Zhan (xain)
on 2007-05-06 18:11
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
>       define_method "save_" << fruit do
>          #HERE
>        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.
E0d864d9677f3c1482a20152b7cac0e2?d=identicon&s=25 Robert Klemme (Guest)
on 2007-05-06 18:25
(Received via mailing list)
On 06.05.2007 17:37, Nanyang Zhan wrote:
>> OP, what do you want - Robert's solution or Robert's solution? :-)
>>
>> Kind regards
>>
>>   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. :-)

> # 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
   def setup_methods(sym)
     class_eval do
       attr_accessor sym
     end
     class_eval "def #{sym}_save() puts 'saving #{sym}' end"
   end
end

Kind regards

  robert
C91098dc76d7ad473165ef24fe805312?d=identicon&s=25 Nanyang Zhan (xain)
on 2007-05-06 19:30
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?
This topic is locked and can not be replied to.