[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

Creating an instance from a variable

Peter Hickman

1/17/2005 10:55:00 AM

I have a class like this:

class Builder
def Builder.create( klass, data )
return klass.new(data)
end
end

I want to call it as:

x = Builder.create( "Fred", data )

and get a object of type Fred.

I'm sure you can see what I am trying to do but for the life of me I
can't get the syntax correct.



17 Answers

Matt Mower

1/17/2005 11:15:00 AM

0

Hi Peter,

On Mon, 17 Jan 2005 19:54:47 +0900, Peter Hickman <peter@semantico.com> wrote:
> I have a class like this:
>
> class Builder
> def Builder.create( klass, data )
> return klass.new(data)
> end
> end
>

how about:

Class Builder
def Builder.create( klass_sym, data )
klass = Class.const_get( klass_sym )
klass.new( data )
end
end

x = Builder.create( :Fred, data )

Regards,

Matt

--
Matt Mower :: http://matt...


lucsky

1/17/2005 11:17:00 AM

0

Peter Hickman <peter@semantico.com> wrote:

> return klass.new(data)

return Kernel.const_get(klass).new(data)

Note that const_get will throw an exception if the passed class name is
unknown, so be prepared to deal with that.

--
Luc Heinrich - lucsky@mac.com

Peter Hickman

1/17/2005 11:19:00 AM

0

Thanks for that it was just what I needed.

Thank you



Thomas Leitner

1/17/2005 11:19:00 AM

0

On Mon, 17 Jan 2005 19:54:47 +0900
Peter Hickman <peter@semantico.com> wrote:

| I have a class like this:
|
| class Builder
| def Builder.create( klass, data )
| return klass.new(data)
| end
| end

use (I have not tested this):

class Builder
def Builder.create( klass, data )
return Object.const_get(klass).new(data)
end
end

*hth*,
Thomas


--
|\ Thomas Leitner -- thomas [underscore] leitner [at] gmx [dot] at
|>
|/ "Life is what happens to you while you're busy making other plans"

Shajith

1/17/2005 11:23:00 AM

0

I'm not sure if this is what you want, but I think Kernel.const_get
can help you.
See changes below:

On Mon, 17 Jan 2005 19:54:47 +0900, Peter Hickman <peter@semantico.com> wrote:
> class Builder
> def Builder.create( klass, data )
return Kernel.const_get(klass).new(data) #can pass in a string
> end
> end
>

You might wanna do some error-catching too.

HTH!
CT


Peter Hickman

1/17/2005 11:42:00 AM

0

Luc Heinrich wrote:

>Peter Hickman <peter@semantico.com> wrote:
>
>
>
>> return klass.new(data)
>>
>>
>
>return Kernel.const_get(klass).new(data)
>
>Note that const_get will throw an exception if the passed class name is
>unknown, so be prepared to deal with that.
>
>
>
Actually, trying to catch the error seems to be harder than it should
be. For example:

class Builder
def Builder.create( klass_sym, data )
begin
return Kernel.const_get( klass_sym ).new( data )
rescue
raise "There was an error"
end
end
end

Should report "There was an error" when const_get fails. However this is
untrapped.

xx.rb:26:in `const_get': uninitialized constant Bernie at Kernel (NameError)
from xx.rb:26:in `create'
from xx.rb:43

I've tried splitting this up into individual steps:

x = Kernel.const_get( klass_sym )
x.new( data )

but this makes no difference.



T. Onoma

1/17/2005 12:05:00 PM

0

Or try Ruby Facets:

require 'facet/object/constant'

class Builder
def Builder.create( klass, *data )
constant( klass ).new( *data )
end
end

T.

P.S. I debate with myself that the method might be better place in Kerenl, so
it might move there in future.


On Monday 17 January 2005 06:42 am, Peter Hickman wrote:
| Luc Heinrich wrote:
| >Peter Hickman <peter@semantico.com> wrote:
| >> return klass.new(data)
| >
| >return Kernel.const_get(klass).new(data)
| >
| >Note that const_get will throw an exception if the passed class name is
| >unknown, so be prepared to deal with that.
|
| Actually, trying to catch the error seems to be harder than it should
| be. For example:
|
| class Builder
| def Builder.create( klass_sym, data )
| begin
| return Kernel.const_get( klass_sym ).new( data )
| rescue
| raise "There was an error"
| end
| end
| end
|
| Should report "There was an error" when const_get fails. However this is
| untrapped.
|
| xx.rb:26:in `const_get': uninitialized constant Bernie at Kernel
| (NameError) from xx.rb:26:in `create'
| from xx.rb:43
|
| I've tried splitting this up into individual steps:
|
| x = Kernel.const_get( klass_sym )
| x.new( data )
|
| but this makes no difference.


Robert Klemme

1/17/2005 12:51:00 PM

0


"Peter Hickman" <peter@semantico.com> schrieb im Newsbeitrag
news:41EBA48B.1030901@semantico.com...
> Luc Heinrich wrote:
>
> >Peter Hickman <peter@semantico.com> wrote:
> >
> >
> >
> >> return klass.new(data)
> >>
> >>
> >
> >return Kernel.const_get(klass).new(data)
> >
> >Note that const_get will throw an exception if the passed class name is
> >unknown, so be prepared to deal with that.
> >
> >
> >
> Actually, trying to catch the error seems to be harder than it should
> be. For example:
>
> class Builder
> def Builder.create( klass_sym, data )
> begin
> return Kernel.const_get( klass_sym ).new( data )
> rescue
> raise "There was an error"
> end
> end
> end
>
> Should report "There was an error" when const_get fails. However this is
> untrapped.
>
> xx.rb:26:in `const_get': uninitialized constant Bernie at Kernel
(NameError)
> from xx.rb:26:in `create'
> from xx.rb:43
>
> I've tried splitting this up into individual steps:
>
> x = Kernel.const_get( klass_sym )
> x.new( data )
>
> but this makes no difference.

You need to catch NameError.

class Builder
def Builder.create( klass_sym, data )
begin
return Kernel.const_get( klass_sym ).new( data )
rescue NameError => e
raise "There was an error: #{e}"
end
end
end


robert

Peter Hickman

1/17/2005 1:35:00 PM

0

Robert Klemme wrote:

>You need to catch NameError.
>
>class Builder
> def Builder.create( klass_sym, data )
> begin
> return Kernel.const_get( klass_sym ).new( data )
> rescue NameError => e
> raise "There was an error: #{e}"
> end
> end
>end
>
>
> robert
>
Thanks for that, I thought that an open rescue would have caught it.
Live and learn.

Thanks



Florian Gross

1/17/2005 1:59:00 PM

0

Peter Hickman wrote:

> I have a class like this:
>
> class Builder
> def Builder.create( klass, data )
> return klass.new(data)
> end
> end
>
> I want to call it as:
>
> x = Builder.create( "Fred", data )

Why don't just do x = Builder.create(Fred, data)?