cast object to object

D

David E.

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...
 
J

Jeremy Bopp

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...

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
 
D

David E.

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()
 
P

Phillip Gawlowski

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 Gawlowski

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.
 
J

Jason Roelofs

Yeah maybe the string confused things. I don't want to even convert to=20=
string.
=20
The value in string is the "human name" of the class i want to convert=20=
it to.
=20
So i'm converting a string into an object of class type of the string=20=
value.
=20
So convert "Dave" into an object of type Dave()
=20
--=20
Posted via http://www.ruby-forum.com/.
=20


Use Ruby's reflectivity:

obj =3D const_get(string).new

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

Jason
 
I

Ian M. Asaff

[Note: parts of this message were removed to make it a legal post.]

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>
 
R

Robert Klemme

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
 
R

Rick DeNatale

That sounds more like it.

David, you could do

class_name =3D ... # fetch the name from somewhere
dave =3D 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)


--=20
Rick DeNatale

Blog: http://talklikeaduck.denhaven2.com/
Github: http://github.com/rubyredrick
Twitter: @RickDeNatale
WWR: http://www.workingwithrails.com/person/9021-rick-denatale
LinkedIn: http://www.linkedin.com/in/rickdenatale
 
D

David E.

Robert Klemme wrote in post #965992:
I don't think so.


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?
 
G

Gary Wright

=20
Robert, thanks. That is the concept. However, I am getting a weird = error
=20
EXCEPTION:wrong constant name FireWatir::Link
=20
after inputting
=20
class_name =3D 'FireWatir::Link'
dave =3D Object.const_get(class_name).new
=20
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 =3D Object.const_get('FireWatir').const_get('Link').new
 
R

Ryan Davis

Object.const_get doesn't parse composite names like "FireWatir::Link". =
You would have to handle that in two steps:
=20
dave =3D Object.const_get('FireWatir').const_get('Link').new

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

klass =3D "FireWatir::Link".split(/::/).inject(Object) { |k, n| =
k.const_get n }
 
J

Josh Cheek

[Note: parts of this message were removed to make it a legal post.]

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
https://github.com/rails/rails/blob...b/active_support/inflector/methods.rb#L89-128



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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,787
Messages
2,569,630
Members
45,338
Latest member
41Pearline46

Latest Threads

Top