Cast object to object

So I have an object of class (user defined) Dave() and Dave2()

This may seem totally assuming, but I have a string that I get.

It will either be “Dave” or “Dave2”.

Is there a way to

if string==“Dave”
(Dave)string.theMethod()
else
(Dave2)string.theMethod()
end

I know, again, I am taking a lot for granted, but in Python I was able
to do this sort of thing… And of course in Java…

On 12/3/2010 9:27 AM, David E. wrote:

else
(Dave2)string.theMethod()
end

I know, again, I am taking a lot for granted, but in Python I was able
to do this sort of thing… And of course in Java…

Ruby is not statically typed, so there is no such thing as casting.
Just assume that string.theMethod() returns an appropriate object for
the rest of the logic to consume. As long as the returned object
responds to the appropriate methods, there will be no problems with
types.

If that doesn’t help, can you describe your goal in a broader sense?

-Jeremy

On Fri, Dec 3, 2010 at 4:39 PM, David E. [email protected] wrote:

Yeah maybe the string confused things. I don’t want to even convert to
string.

The value in string is the “human name” of the class i want to convert
it to.

So i’m converting a string into an object of class type of the string
value.

So convert “Dave” into an object of type Dave()

So, you want to do something like:
name_of_my_class.gets
name_of_my_class.to_class # Create a class out of the user input

Correct?

And I assume a case or if statement doesn’t cover all the situations
you want to do that in, also correct?


Phillip G.

Though the folk I have met,
(Ah, how soon!) they forget
When I’ve moved on to some other place,
There may be one or two,
When I’ve played and passed through,
Who’ll remember my song or my face.

On Dec 3, 2010, at 10:39 AM, David E. wrote:


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

Use Ruby’s reflectivity:

obj = const_get(string).new

assuming #string has a value that’s the exact name of the const you’re
trying to find.

Jason

Yeah maybe the string confused things. I don’t want to even convert to
string.

The value in string is the “human name” of the class i want to convert
it to.

So i’m converting a string into an object of class type of the string
value.

So convert “Dave” into an object of type Dave()

Do you mean something like this?

irb(main):001:0> class A
irb(main):002:1> attr_accessor :a
irb(main):003:1> def initialize(s)
irb(main):004:2> @a = s
irb(main):005:2> end
irb(main):006:1> end
=> nil
irb(main):007:0> class B
irb(main):008:1> attr_accessor :b
irb(main):009:1> def initialize(s)
irb(main):010:2> @b = s
irb(main):011:2> end
irb(main):012:1> end
=> nil
irb(main):013:0> x = “B”
=> “B”
irb(main):014:0> def test(x)
irb(main):015:1> if x == “B”
irb(main):016:2> B.new(x)
irb(main):017:2> else
irb(main):018:2* A.new(x)
irb(main):019:2> end
irb(main):020:1> end
=> nil
irb(main):021:0> x = test(x)
=> #<B:0xee0bd8 @b=“B”>
irb(main):022:0> x
=> #<B:0xee0bd8 @b=“B”>
irb(main):023:0>

On Fri, Dec 3, 2010 at 11:35 AM, Robert K.
[email protected] wrote:

name_of_my_class.to_class # Create a class out of the user input

That sounds more like it.

David, you could do

class_name = … # fetch the name from somewhere
dave = Object.const_get(class_name).new

Or for a more robust solution which can also handle strings like
“Module::Class” have a look at the String#constantize method from
activesupport (part of Rails)


Rick DeNatale

Blog: http://talklikeaduck.denhaven2.com/
Github: rubyredrick (Rick DeNatale) · GitHub
Twitter: @RickDeNatale
WWR: http://www.workingwithrails.com/person/9021-rick-denatale
LinkedIn: http://www.linkedin.com/in/rickdenatale

Robert K. wrote in post #965992:

On 12/03/2010 04:44 PM, Phillip G. wrote:

So convert “Dave” into an object of type Dave()

So, you want to do something like:
name_of_my_class.gets

I don’t think so.

name_of_my_class.to_class # Create a class out of the user input

That sounds more like it.

David, you could do

class_name = … # fetch the name from somewhere
dave = Object.const_get(class_name).new

Kind regards

robert

Robert, thanks. That is the concept. However, I am getting a weird error

EXCEPTION:wrong constant name FireWatir::Link

after inputting

  class_name = 'FireWatir::Link'
  dave = Object.const_get(class_name).new

Is my “class_name” formatted correctly?

On 12/03/2010 04:44 PM, Phillip G. wrote:

So convert “Dave” into an object of type Dave()

So, you want to do something like:
name_of_my_class.gets

I don’t think so.

name_of_my_class.to_class # Create a class out of the user input

That sounds more like it.

David, you could do

class_name = … # fetch the name from somewhere
dave = Object.const_get(class_name).new

Kind regards

robert

hahah it worked great… Thanks all!

On Dec 3, 2010, at 12:07 , Gary W. wrote:

Object.const_get doesn’t parse composite names like “FireWatir::Link”. You
would have to handle that in two steps:

dave = Object.const_get(‘FireWatir’).const_get(‘Link’).new

This is one of the very few legitimate uses of inject in popular use:

klass = “FireWatir::Link”.split(/::/).inject(Object) { |k, n|
k.const_get n }

On Fri, Dec 3, 2010 at 11:01 AM, Rick DeNatale
[email protected]wrote:

dave = Object.const_get(class_name).new

Or for a more robust solution which can also handle strings like
“Module::Class” have a look at the String#constantize method from
activesupport (part of Rails)

Here is a link

If you wish to use it, you can copy it and any other methods it depends
on,
or you can use the gem itself:

require ‘active_support’
“FireWatir::Link”.constantize # => FireWatir::Link

On Dec 3, 2010, at 2:59 PM, David E. wrote:

Is my “class_name” formatted correctly?
Object.const_get doesn’t parse composite names like “FireWatir::Link”.
You would have to handle that in two steps:

dave = Object.const_get(‘FireWatir’).const_get(‘Link’).new