On Fri, Sep 18, 2009 at 12:10 PM, Lin Wj <mailbox.lwj / gmail.com> wrote:
> =A0def self.developer_options(*args)
> =A0 =A0fetch_options({'area'=3D>'developers','index' =3D> args.first})
> =A0end
>
> =A0def self.priority_options(*args)
> =A0 =A0fetch_options({'area'=3D>'priority','index' =3D> args.first})
> =A0end
>
> =A0def self.status_options(*args)
> =A0 =A0fetch_options({'area'=3D>'status','index' =3D> args.first})
> =A0end
>
> is there anyway to dynamically create methods with a specific name ?
>
> ie: create methods which are
>
> def self.??????_options(*args)
> =A0 =A0fetch_options({'area'=3D>'?????','index' =3D> args.first})
> =A0end

As Ralf said, you can use define_method. If you want to create them
up-front (untested):

%w{developer priority status}.each do |area|
  define_method "#{area}_options" do |*args|
    fetch_options({'area' =3D> area, 'index' =3D> args.first})
  end
end

Of course this needs to be done in the scope where self is the
singleton class of the object (see a recent post about this).

If you want to define them lazily, you can use method_missing (untested):

def method_missing (meth, *args, &blk)
  m =3D meth.match /(.*)_options$/
  super unless m
  define_method meth do |*arguments|
    fetch_options({'area' =3D> m[1], 'index' =3D> arguments.first})
  end
end

Hope this helps,

Jesus.