Stefano Crocco
1/9/2007 9:52:00 PM
Alle 22:35, martedì 9 gennaio 2007, Shea Martin ha scritto:
> def clone
> rslt = BuildConfig.new
>
> self.instance_variables.each do |member|
> rslt.instance_variable_set( member,
> self.instance_variable_get(member).clone )
> end
>
> return rslt
> end
>
> This clone method bombs if any of the instance variables are FixNums,
> Floats, boolean, etc. Is there a way in my loop to know if an instance
> variable supports 'clone'?
>
> ~S
I don't know whether there is a way to know in advance if a variable supports
clone, so I'd say: try it. If it works, good. If it doesn't, it'll raise an
exception, which you can rescue:
def clone
rslt = BuildConfig.new
self.instance_variables.each do |member|
begin rslt.instance_variable_set( member, self.instance_variable_get(member).clone )
rescue Exception: rslt.instance_variable_set(member, self.instance_variable_get(member))
end
end
return rslt
end
I'm not an expert, so there may be better ways to do this I don't know, but this should work.
Stefano