From: Dhruva Sagar Date: 2010-05-20T12:44:37+09:00 Subject: Re: define_method inside instance_eval --0016367d6deca4e2650486fe6461 Content-Type: text/plain; charset=UTF-8 To define class methods from a class method : def status_field(*args) self.metaclass.send(:define_method, :status_all) { args } end OR def status_field(*args) (class << self; self; end).instance_eval { define_method(:status_all) { args } } end On Thu, May 20, 2010 at 08:54, Adriano Nagel wrote: > Dhruva Sagar wrote: > > Ok I reread your problem. > > > > In your status_all method you can simply return a *args.to_a*. That > > should > > be the simplest approach. > > > > def status_field(*args) > > instance_eval { define_method(:status_all) {|*args| args.to_a } } > > end > > Actually, args already is an array and you don't need it as an argument > for status_all (it was passed to status_field). > > But the problem here is that this will produce an instance method, not a > class method. > > > But I am not sure exactly why your taking this approach or what your > > intentions are. What should be the behavior on subsequent calls to > > status_field ? > > I'm extending ActiveRecord, and status_field does more than just define > status_all. For example: > > class User < ActiveRecord::Base > status_field :inactive, :active, :suspended > end > > will create scopes, instance methods like status_inactive, > status_inactive?, etc. > > User.status_all should return all the possible user status. I have it > working, but I would like to learn a better way to do it. > > Thanks, > > -- > Adriano > -- > Posted via http://www.ruby-forum.com/. > > -- Thanks & Regards, Dhruva Sagar. --0016367d6deca4e2650486fe6461--