Forum: Ruby how to do: var1 = var2.new(.....) (var2 contains classname)

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.
Ad745a3a6bdc249a7b34a0b6f5c8d5ec?d=identicon&s=25 Luca Scaljery (jeanluca)
on 2007-02-14 21:36
Hi All

I have a situation in which I have the name of the class in a variable
So normally I would do
    song = Song.new("Bicylops", "Fleck", 260)

But I need to do something like
    var2 = "Song"
    var1 = var2.new("Bicylops", "Fleck", 260)

This doesn't work!
Any suggestions how to do this ?

Thnx
LuCa
0276239ca57aee241d4b41379587fa20?d=identicon&s=25 Lyle Johnson (Guest)
on 2007-02-14 21:45
(Received via mailing list)
On 2/14/07, Luca Scaljery <lcalje@gmail.com> wrote:

> I have a situation in which I have the name of the class in a variable
> So normally I would do
>     song = Song.new("Bicylops", "Fleck", 260)
>
> But I need to do something like
>     var2 = "Song"
>     var1 = var2.new("Bicylops", "Fleck", 260)
>
> This doesn't work!
> Any suggestions how to do this ?

var1 = Object.get_const(var2).new("Bicyclops", "Fleck", 260)
4d5b5dd4e263d780a5dfe7ac8b8ac98c?d=identicon&s=25 Tim Pease (Guest)
on 2007-02-14 21:46
(Received via mailing list)
On 2/14/07, Luca Scaljery <lcalje@gmail.com> wrote:
> This doesn't work!
> Any suggestions how to do this ?
>

You need to convert your string "Song" into a Class object.

var2 = "Song"
var1 = Object.const_get(var2).new("Bicyclops", "Fleck", 260)

All class and module objects are stored as constants in Object.  It
gets a little tricky if you have a class withing a module --
"Music::Song".  Then you'll have to do something like this:

current = Object
var2 = "Music::Song"
var2.split("::").each do |str|
  current = current.const_get(str)
end
var1 = current.new( ... )


Hope this helps

TwP
59528506e6297141161afcde91d677c9?d=identicon&s=25 Nicolai Reuschling (codeblogger)
on 2007-02-14 21:47
(Received via mailing list)
Maybe you want to have a look at this:

var1 = Object.const_get(var2)

Kind regards
Nicolai

2007/2/14, Luca Scaljery <lcalje@gmail.com>:
0276239ca57aee241d4b41379587fa20?d=identicon&s=25 Lyle Johnson (Guest)
on 2007-02-14 21:48
(Received via mailing list)
On 2/14/07, Lyle Johnson <lyle.johnson@gmail.com> wrote:

> var1 = Object.get_const(var2).new("Bicyclops", "Fleck", 260)

Whoops, that should be "const_get(var2)", not "get_const(var2)".
Ad745a3a6bdc249a7b34a0b6f5c8d5ec?d=identicon&s=25 Luca Scaljery (jeanluca)
on 2007-02-14 21:50
Hi

Thanks for the solution!
It works!!

LuCa
24c6e4ed08f7a4bf9cfb7edc2dbd14b8?d=identicon&s=25 Patrick Spence (pkspence)
on 2007-02-14 21:51
Lyle Johnson wrote:
> var1 = Object.get_const(var2).new("Bicyclops", "Fleck", 260)

should be #const_get()
21053fc472ce35063e175e9cfbb78e6b?d=identicon&s=25 Gareth Adams (Guest)
on 2007-02-15 09:45
(Received via mailing list)
Luca Scaljery <lcalje <at> gmail.com> writes:

> Hi All
>
> I have a situation in which I have the name of the class in a variable
> So normally I would do
>     song = Song.new("Bicylops", "Fleck", 260)

I'm surprised no one else has mentioned this, but there's very little
need to
store a class name in a string. Usually if you're doing this then
there's
something wrong with your methodology and there's a much more efficient
way to
get the right results.

The only real reason I can think that you'd have that in a string and
not simple
as the class constant is if you took it from user input. Hopefully you
aren't
letting your users decide which classes get instantiated..!
This topic is locked and can not be replied to.