def class_by_name name
name.split("::").inject(Object){ |c,n|
c.const_get(n)
}
end
p class_by_name( 'Hash' )
p class_by_name( 'File::Stat' )
__END__
544> ruby tClassByName.rb
Hash
File::Stat
regards
Sergey
···
----- Original Message ----- From: "Daniel Harple" <dharple@generalconsumption.org>
To: "ruby-talk ML" <ruby-talk@ruby-lang.org>
Sent: Tuesday, May 02, 2006 11:06 AM
Subject: Re: How to get a new instance of a class given a String
On May 2, 2006, at 5:03 PM, David Clements wrote:
class_name = "UserPreference"
up = Class.class_for_name(class_name).new
def class_by_name name
name.split("::").inject(Object){ |c,n|
c.const_get(n)
}
end
p class_by_name( 'Hash' )
p class_by_name( 'File::Stat' )
__END__
544> ruby tClassByName.rb
Hash
File::Stat
regards
Sergey
Thanks so much that has me going again.
Dave
···
On 5/2/06, Sergey Volkov <gm.vlkv@gmail.com> wrote:
----- Original Message -----
From: "Daniel Harple" <dharple@generalconsumption.org>
To: "ruby-talk ML" <ruby-talk@ruby-lang.org>
Sent: Tuesday, May 02, 2006 11:06 AM
Subject: Re: How to get a new instance of a class given a String
> On May 2, 2006, at 5:03 PM, David Clements wrote:
>
>> class_name = "UserPreference"
>> up = Class.class_for_name(class_name).new
>
> up = Object.const_get(class_name).new
>
> -- Daniel