Forum: Ruby on Rails Is eval the only way?

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.
0430e2423183415d7688898faddb470f?d=identicon&s=25 Jason Tuttle (Guest)
on 2006-04-05 23:40
(Received via mailing list)
Hi,

I'm trying to write some methods which use a parameter to represent a
Class name.

A very simplistic example:

def foo(class_name, conditions)
   x = class_name.find(:all, :conditions => "#{conditions}")
end

Unfortunately, this does not work. The only way around this problem
that I can think of is to use eval:

def foo(class_name, conditions)
   x = eval "#{class_name}.find(:all, :conditions => \"#
{conditions}'\")"
end

There's got to be a better way. -- Please help!

: )

Jason
675475d0b65710be6d992eb5eb2c61c2?d=identicon&s=25 Gregory Seidman (Guest)
on 2006-04-06 00:23
(Received via mailing list)
On Wed, Apr 05, 2006 at 05:39:35PM -0400, Jason Tuttle wrote:
} I'm trying to write some methods which use a parameter to represent a
} Class name.
}
} A very simplistic example:
}
} def foo(class_name, conditions)
}   x = class_name.find(:all, :conditions => "#{conditions}")
} end
}
} Unfortunately, this does not work. The only way around this problem
} that I can think of is to use eval:
[...]
} There's got to be a better way. -- Please help!

def foo(class_name, conditions)
  x = Kernel.const_get(class_name).find(:all, :conditions =>
"#{conditions}")
end

} : )
} Jason
--Greg
A90204c955db033cd975f7bb0ec9600b?d=identicon&s=25 Ashley Moran (Guest)
on 2006-04-06 00:26
(Received via mailing list)
On Apr 05, 2006, at 10:39 pm, Jason Tuttle wrote:

>
> : )
>
> Jason

Jason

Try this:

   def foo(class_name, conditions)
     ObjectSpace.each_object(Class) do |c|
       return c.find(:all, :conditions => conditions) if c.name ==
class_name
     end
   end

You'd have to benchmark it to see which performs better, but it looks
more "Ruby-like" like that.

Ashley
A90204c955db033cd975f7bb0ec9600b?d=identicon&s=25 Ashley Moran (Guest)
on 2006-04-06 00:26
(Received via mailing list)
On Apr 05, 2006, at 11:23 pm, Gregory Seidman wrote:

> def foo(class_name, conditions)
>   x = Kernel.const_get(class_name).find(:all, :conditions => "#
> {conditions}")
> end


Meh... that beats mine :P

Ashley
25e11a00a89683f7e01e425a1a6e305c?d=identicon&s=25 Wilson Bilkovich (Guest)
on 2006-04-06 00:56
(Received via mailing list)
On 4/5/06, Jason Tuttle <jason@gothamcitymacs.com> wrote:
>
> Unfortunately, this does not work. The only way around this problem
> that I can think of is to use eval:
>
> def foo(class_name, conditions)
>    x = eval "#{class_name}.find(:all, :conditions => \"#
> {conditions}'\")"
> end
>
> There's got to be a better way. -- Please help!
>

Inside Rails, you can do:
x = class_name.constantize.find(:all, :conditions => "#{conditions}")

constantize is a more-readable shortcut for the const_get business.
6f7c877de704c7cc03c8a3b2dc52df92?d=identicon&s=25 Carmen --- (carmen)
on 2006-04-06 02:08
>> def foo(class_name, conditions)
>>   x = Kernel.const_get(class_name).find(:all, :conditions => "#

ive been using Object's const_get instead of Kernel's. is there a
difference? and why is there an object and a kernel if not?
675475d0b65710be6d992eb5eb2c61c2?d=identicon&s=25 Gregory Seidman (Guest)
on 2006-04-06 02:30
(Received via mailing list)
On Thu, Apr 06, 2006 at 02:08:21AM +0200, carmen wrote:
}
} >> def foo(class_name, conditions)
} >>   x = Kernel.const_get(class_name).find(:all, :conditions => "#
}
} ive been using Object's const_get instead of Kernel's. is there a
} difference? and why is there an object and a kernel if not?

No, there isn't:

irb(main):001:0> Object.included_modules
=> [Kernel]
irb(main):002:0>

--Greg
This topic is locked and can not be replied to.