Passing an Object Class from a method to a caller

R

RLMuller

This is a multi-part message in MIME format.

------=_NextPart_000_004C_01C37882.24993D90
Content-Type: text/plain;
charset="Windows-1252"
Content-Transfer-Encoding: quoted-printable

Hi All,

I can't seem to pass an Object Class from a method to a calling routine

Here's what I wrote:

def getTypeOf(o)
aTypes =3D [Array, String, Numeric, Fixnum, Struct, MatchData]
result =3D Object
i =3D 0
for t in aTypes
i +=3D 1
if o.is_a?t then=20
result=3Dt
print "Index =3D #{i.to_s}: Type =3D '#{t.to_s}'\n"
break
end
result
end
end

obj =3D "abc"
print "Type of object '#{obj.to_s}' is '#{getTypeOf(obj).to_s}'\n\n}"

=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D

Here's what I get when running under SciTE:

ndex =3D 2: Type =3D 'String'
Type of object 'abc' is ''

=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D

I originally tried to place the .to_s after "result" in the subroutine, =
instead of after the invocation of getTypeOf, to no avail.

Any ideas?

TIA,
Regards,
Richard

A programmer is a device for turning coffee into code.
Jeff Prosise (with an assist from Paul Erdos)

------=_NextPart_000_004C_01C37882.24993D90
Content-Type: text/html;
charset="Windows-1252"
Content-Transfer-Encoding: quoted-printable

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML><HEAD>
<META http-equiv=3DContent-Type content=3D"text/html; =
charset=3Dwindows-1252">
<META content=3D"MSHTML 6.00.2800.1226" name=3DGENERATOR>
<STYLE></STYLE>
</HEAD>
<BODY bgColor=3D#ffffff>
<DIV><FONT size=3D2>Hi All,</FONT></DIV>
<DIV><FONT size=3D2></FONT>&nbsp;</DIV>
<DIV><FONT size=3D2>I can't seem to pass an Object Class from a method =
to a=20
calling routine</FONT></DIV>
<DIV><FONT size=3D2></FONT>&nbsp;</DIV>
<DIV><FONT size=3D2>Here's what I wrote:</FONT></DIV>
<DIV><FONT size=3D2></FONT>&nbsp;</DIV>
<DIV><FONT size=3D2>def =
getTypeOf(o)<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;=20
aTypes =3D [Array, String, Numeric, Fixnum, Struct,=20
MatchData]<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; result =3D=20
Object<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; i =3D=20
0<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; for t in=20
aTypes<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nb=
sp; i=20
+=3D =
1<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; =
if=20
o.is_a?t then=20
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nb=
sp;&nbsp;&nbsp;&nbsp;=20
result=3Dt<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp=
;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;=20
print "Index =3D #{i.to_s}: Type =3D=20
'#{t.to_s}'\n"<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&=
nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;=20
break<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbs=
p;=20
end<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;=20
result<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; =
end<BR>end</FONT></DIV>
<DIV>&nbsp;</DIV>
<DIV><FONT size=3D2>obj =3D "abc"<BR>print "Type of object '#{obj.to_s}' =
is=20
'#{getTypeOf(obj).to_s}'\n\n}"<BR></FONT></DIV>
<DIV><FONT =
size=3D2>=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D</FONT></DIV>
<DIV><FONT size=3D2></FONT>&nbsp;</DIV>
<DIV><FONT size=3D2>Here's what I get when running under =
SciTE:</FONT></DIV>
<DIV><FONT size=3D2></FONT>&nbsp;</DIV>
<DIV><FONT size=3D2>ndex =3D 2: Type =3D 'String'<BR>Type of object =
'abc' is=20
''<BR></FONT></DIV>
<DIV><FONT =
size=3D2>&nbsp;=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D</FONT></DIV>
<DIV><FONT size=3D2></FONT>&nbsp;</DIV>
<DIV><FONT size=3D2>I originally tried to place the .to_s after "result" =
in the=20
subroutine, instead of after the invocation of getTypeOf, to no=20
avail.</FONT></DIV>
<DIV><FONT size=3D2></FONT>&nbsp;</DIV>
<DIV><FONT size=3D2>Any ideas?</FONT></DIV>
<DIV><FONT size=3D2></FONT>&nbsp;</DIV>
<DIV><FONT size=3D2>TIA,</FONT></DIV>
<DIV><FONT size=3D2>Regards,<BR>Richard</FONT></DIV>
<DIV>&nbsp;</DIV>
<DIV><FONT size=3D2>A programmer is a device for turning coffee into =
code.<BR>Jeff=20
Prosise (with an assist from Paul Erdos)<BR></FONT></DIV></BODY></HTML>

------=_NextPart_000_004C_01C37882.24993D90--
 
M

Michael Garriss

RLMuller said:
Hi All,

I can't seem to pass an Object Class from a method to a calling routine

Here's what I wrote:

def getTypeOf(o)
aTypes = [Array, String, Numeric, Fixnum, Struct, MatchData]
result = Object
i = 0
for t in aTypes
i += 1
if o.is_a?t then
result=t
print "Index = #{i.to_s}: Type = '#{t.to_s}'\n"
break
end
result
end
end


Looks like your 'break' is passing the 'result' line and thus returning
the result of 'print'.

Michael
 
E

Eric Hodel

--Sw7tCqrGA+HQ0/zt
Content-Type: text/plain; charset=us-ascii
Content-Disposition: inline
Content-Transfer-Encoding: quoted-printable
obj =3D "abc"
print "Type of object '#{obj.to_s}' is '#{getTypeOf(obj).to_s}'\n\n}"
=20
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D
=20
Here's what I get when running under SciTE:
=20
ndex =3D 2: Type =3D 'String'
Type of object 'abc' is ''
=20
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D
=20
I originally tried to place the .to_s after "result" in the
subroutine, instead of after the invocation of getTypeOf, to no
avail.
=20
Any ideas?

Why not instead use obj.class.name:

obj =3D "abc"
puts "Type of object '#{obj}' is #{obj.class.name}"

--=20
Eric Hodel - (e-mail address removed) - http://segment7.net
All messages signed with fingerprint:
FEC2 57F1 D465 EB15 5D6E 7C11 332A 551C 796C 9F04


--Sw7tCqrGA+HQ0/zt
Content-Type: application/pgp-signature
Content-Disposition: inline

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.2 (FreeBSD)

iD8DBQE/YOIgMypVHHlsnwQRAgnsAJ9RrWzVkIWlU2TkjWe4ki3XIj9hoQCgkEjW
cuQ2fMGjFN87qFlwUaBBgkI=
=XYDL
-----END PGP SIGNATURE-----

--Sw7tCqrGA+HQ0/zt--
 
S

Sean O'Dell

Michael said:
RLMuller said:
Hi All,

I can't seem to pass an Object Class from a method to a calling routine

Here's what I wrote:

def getTypeOf(o)
aTypes = [Array, String, Numeric, Fixnum, Struct, MatchData]
result = Object
i = 0
for t in aTypes
i += 1
if o.is_a?t then
result=t
print "Index = #{i.to_s}: Type = '#{t.to_s}'\n"
break
end
result
end
end



Looks like your 'break' is passing the 'result' line and thus returning
the result of 'print'.

In other words, it looks like you have the last "result" expression
inside the for loop, which is probably not what you wanted.

Sean O'Dell
 
M

Michael Granger

RLMuller said:
I can't seem to pass an Object Class from a method to a calling routine

Here's what I wrote:

def getTypeOf(o)
aTypes = [Array, String, Numeric, Fixnum, Struct, MatchData]
result = Object
i = 0
for t in aTypes
i += 1
if o.is_a?t then
result=t
print "Index = #{i.to_s}: Type = '#{t.to_s}'\n"
break
end
result
end
end

obj = "abc"
print "Type of object '#{obj.to_s}' is '#{getTypeOf(obj).to_s}'\n\n}"

This happens because your break causes the value of the last evaluated
statement (i.e., the 'print') to be the result of the 'for' loop, which is
what it breaks out of. You could change it to 'break result' if you're using
1.8; for a way of doing the same thing in a more Ruby-ish manner (IMHO),
what about this:

Types = [Array, String, Numeric, Fixnum, Struct, MatchData]

def getTypeOf( o )
t = Types.find {|type| o.is_a?( type )} || Object
i = Types.index( t )

print "Index = #{i}: Type = '#{t}'\n"
return t
end

[ "abc", 14, 6.8, :foo, /\w/.match("foo") ].each {|obj|
print "Type of object %p is '%s'\n\n" %
[ obj, getTypeOf(obj) ]
}

# Results in:
#
# Index = 1: Type = 'String'
# Type of object "abc" is 'String'
#
# Index = 2: Type = 'Numeric'
# Type of object 14 is 'Numeric'
#
# Index = 2: Type = 'Numeric'
# Type of object 6.8 is 'Numeric'
#
# Index = : Type = 'Object'
# Type of object :foo is 'Object'
#
# Index = 5: Type = 'MatchData'
# Type of object #<MatchData:0xa9558> is 'MatchData'
#


Note also that because all 'Fixnum' objects are also 'Numeric' objects,
#getTypeOf will never return 'Fixnum'.
 
R

Richard

Thanks, Michael.

Obviously, I didn't look at my structure very carefully. Thanks for
prodding me in the right direction. Just in case your interested in
this trivial function, here's what I wound up with:

def getTypeOf(o)
aTypes = [Array, String, Numeric, Fixnum, Struct, MatchData]
result = Object.to_s
i = 0
for t in aTypes
i += 1
if o.is_a?t then
result=t.to_s
break
end
end
result
end

Regards,
Richard
 
J

Jason Creighton

On 11 Sep 2003 22:10:37 -0700
Thanks, Michael.

Obviously, I didn't look at my structure very carefully. Thanks for
prodding me in the right direction. Just in case your interested in
this trivial function, here's what I wound up with:

def getTypeOf(o)
aTypes = [Array, String, Numeric, Fixnum, Struct, MatchData]
result = Object.to_s
i = 0
for t in aTypes
i += 1
if o.is_a?t then
result=t.to_s
break
end
end
result
end

Why go to all this trouble when you can just do "obj.class.to_s"?

Jason Creighton
 
R

Richard

Hi Michael,

Thanks very much for your help, especially going the extra mile to
educate a Ruby newby (what an appelation :))

In addition, I particularly liked the %p formatter. It didn't work
for me, so I did in code (either quote or null delimter depending on
String or not). I just checked www.ruby-lang.org and see that my
1.6.8 version is out of date.

Finally, I put 'Fixnum' ahead of 'Numeric' so that the narrower class
would show up.

Oh yes, I looked at your FaerieMUD.org website. Looks neat, but my
plate's full just trying to do application programming.

Regards,
Richard
 
R

Richard

Thanks to all of the responders. Fundamentally, I had faulty logic
that, as a veteran programmer (C, C++, Cobol, Fortran, Perl, etc.) I
should have spotted myself. I apologize for having taken your time on
my trivial mistake.

However, I gained a lot from your responses which suggested other
features in Ruby and the fact that a later stable version than my
1.6.8 was available.

Above all, I got a tutorial from Michael Granger on how to write
*real* Ruby rather than C in Ruby syntax.

Again, thanks to all of you.
 

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,967
Messages
2,570,148
Members
46,694
Latest member
LetaCadwal

Latest Threads

Top