Ask a Question related to Ruby, Design and Development.
-
RLMuller #1
Passing an Object Class from a method to a caller
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=3 D=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=3 D=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> </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> </DIV>
<DIV><FONT size=3D2>Here's what I wrote:</FONT></DIV>
<DIV><FONT size=3D2></FONT> </DIV>
<DIV><FONT size=3D2>def =
getTypeOf(o)<BR> &nbs p; =20
aTypes =3D [Array, String, Numeric, Fixnum, Struct,=20
MatchData]<BR> result =3D=20
Object<BR> &nbs p; i =3D=20
0<BR> for t in=20
aTypes<BR> &nbs p; &nb=
sp; i=20
+=3D =
1<BR> &nb sp; =
if=20
o.is_a?t then=20
<BR> &nbs p; &nb=
sp; =20
result=3Dt<BR>  =
; =20
print "Index =3D #{i.to_s}: Type =3D=20
'#{t.to_s}'\n"<BR> &n bsp; &=
nbsp; =20
break<BR>   ; &nbs=
p;=20
end<BR> = 20
result<BR> &nbs p; =
end<BR>end</FONT></DIV>
<DIV> </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=3 D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3 D=3D</FONT></DIV>
<DIV><FONT size=3D2></FONT> </DIV>
<DIV><FONT size=3D2>Here's what I get when running under =
SciTE:</FONT></DIV>
<DIV><FONT size=3D2></FONT> </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> =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3 D=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> </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> </DIV>
<DIV><FONT size=3D2>Any ideas?</FONT></DIV>
<DIV><FONT size=3D2></FONT> </DIV>
<DIV><FONT size=3D2>TIA,</FONT></DIV>
<DIV><FONT size=3D2>Regards,<BR>Richard</FONT></DIV>
<DIV> </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--
RLMuller Guest
-
Pass Class Object To FMS Using NetConnection.call Method
Hello All, I have a custom class that defines several methods on itself to retrieve its data. This class object is then sent to FMS via the... -
How should I announce the list of delayed class/object/method loaders?
I've been maintaining a list of object loaders for quite some time now: http://metaperl.com/article-pod/Catalog-lazy_loaders/lazy_loaders.html ... -
defining a class/instantiating object inside a method of anotherclass
Ok, here's what I'm trying to do. I have a class. Inside the class I have a method that reads a text file. In the text file I have a class name and... -
Passing an Object Class from a method to a caller
Hi -- On Fri, 12 Sep 2003, Michael Granger wrote: I think break (pre-1.8.0 or without explicit value in 1.8.0) evaluates to nil: $ ruby... -
Passing a method block to a parent class
If I have **** CODE **** class A def method1( argument ) yield( argument ) end end -
Michael Garriss #2
Re: Passing an Object Class from a method to a caller
RLMuller wrote:
> 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
Michael Garriss Guest
-
Eric Hodel #3
Re: Passing an Object Class from a method to a caller
--Sw7tCqrGA+HQ0/zt
Content-Type: text/plain; charset=us-ascii
Content-Disposition: inline
Content-Transfer-Encoding: quoted-printable
RLMuller (RLMuller@comcast.net) wrote:
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D> 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=3 D=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=3 D=3D=3D=3D=3D=3D=3D=3D=Why not instead use obj.class.name:>=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?
obj =3D "abc"
puts "Type of object '#{obj}' is #{obj.class.name}"
--=20
Eric Hodel - [email]drbrain@segment7.net[/email] - [url]http://segment7.net[/url]
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/YOIgMypVHHlsnwQRAgnsAJ9RrWzVkIWlU2TkjWe4ki3XIj9hoQ CgkEjW
cuQ2fMGjFN87qFlwUaBBgkI=
=XYDL
-----END PGP SIGNATURE-----
--Sw7tCqrGA+HQ0/zt--
Eric Hodel Guest
-
Sean O'Dell #4
Re: Passing an Object Class from a method to a caller
Michael Garriss wrote:
In other words, it looks like you have the last "result" expression> RLMuller wrote:
>>>> 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'.
inside the for loop, which is probably not what you wanted.
Sean O'Dell
Sean O'Dell Guest
-
dblack@superlink.net #5
Re: Passing an Object Class from a method to a caller
Hi --
On Fri, 12 Sep 2003, Michael Garriss wrote:
I think it's actually returning the result of break, which is nil> RLMuller wrote:
>>> > 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'.
(but can be assigned with "break val" in 1.8.0).
David
--
David Alan Black
home: [email]dblack@superlink.net[/email]
work: [email]blackdav@shu.edu[/email]
Web: [url]http://pirate.shu.edu/~blackdav[/url]
dblack@superlink.net Guest
-
Michael Granger #6
Re: Passing an Object Class from a method to a caller
RLMuller writes:
This happens because your break causes the value of the last evaluated> 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}"
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'.
--
Michael Granger <ged@FaerieMUD.org>
Rubymage, Believer, Architect
The FaerieMUD Consortium <http://www.FaerieMUD.org/>
Michael Granger Guest
-
Michael Garriss #7
Re: Passing an Object Class from a method to a caller
[email]dblack@superlink.net[/email] wrote:
def this>Hi --
>
>On Fri, 12 Sep 2003, Michael Garriss wrote:
>
>
>>>>RLMuller wrote:
>>
>>
>>>>Looks like your 'break' is passing the 'result' line and thus returning>>>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
>>>
>>>
>>the result of 'print'.
>>
>>
>I think it's actually returning the result of break, which is nil
>(but can be assigned with "break val" in 1.8.0).
>
>
>
break "cool" while true
end
--> nil
this
--> "cool"
I can't remember the last day I went without learning something cool
about Ruby.
Michael
Michael Garriss Guest
-
Richard #8
Re: Passing an Object Class from a method to a caller
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
Richard Guest
-
Jason Creighton #9
Re: Passing an Object Class from a method to a caller
On 11 Sep 2003 22:10:37 -0700
[email]RLMuller@comcast.net[/email] (Richard) wrote:
Why go to all this trouble when you can just do "obj.class.to_s"?> 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
Jason Creighton
Jason Creighton Guest
-
Richard #10
Re: Passing an Object Class from a method to a caller
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 [url]www.ruby-lang.org[/url] 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
Richard Guest
-
Richard #11
Re: Passing an Object Class from a method to a caller
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.
--
Richard Muller
Richard Guest



Reply With Quote

