[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Re: Working with classname that is stored in a string

Bryan Duxbury

8/13/2007 9:27:00 PM

Glen Holcomb wrote:
> Hi all,

One thing you can do is not use a string at all, but pass the class name
constant. If you've got a class like:

class MyClass
...
end

then you can do something like:

filtered_fields = filter_my_fields MyClass, [:field1, :field2]

and it works just fine.

Alternatively, you could use an AR extension like this:

class ActiveRecord::Base
def filtered_fields(*fields)
@attributes.reject{|name, value| fields.include?(name.intern)}
end
end

Haven't run that code, but that should let you do something like
my_ar_instance.filtered_fields(:field1, :field2)

--
Posted via http://www.ruby-....