[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

Passing an Object Class from a method to a caller

RLMuller

9/11/2003 8:33:00 PM

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

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

========================================

Here's what I get when running under SciTE:

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

===================================

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)
7 Answers

mgarriss

9/11/2003 8:51:00 PM

0

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


Eric Hodel

9/11/2003 8:59:00 PM

0

RLMuller (RLMuller@comcast.net) wrote:

> obj = "abc"
> print "Type of object ''#{obj.to_s}'' is ''#{getTypeOf(obj).to_s}''\n\n}"
>
> ========================================
>
> Here''s what I get when running under SciTE:
>
> ndex = 2: Type = ''String''
> Type of object ''abc'' is ''''
>
> ===================================
>
> 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?

Why not instead use obj.class.name:

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

--
Eric Hodel - drbrain@segment7.net - http://se...
All messages signed with fingerprint:
FEC2 57F1 D465 EB15 5D6E 7C11 332A 551C 796C 9F04

Sean O'Dell

9/11/2003 9:05:00 PM

0

Michael Garriss wrote:

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

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

Michael Granger

9/11/2003 9:38:00 PM

0

RLMuller writes:

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

--
Michael Granger <ged@FaerieMUD.org>
Rubymage, Believer, Architect
The FaerieMUD Consortium <http://www.FaerieMU...


Jason Creighton

9/12/2003 2:18:00 PM

0

On 11 Sep 2003 22:10:37 -0700
RLMuller@comcast.net (Richard) wrote:

> 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

RLMuller

9/12/2003 3:29:00 PM

0

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

RLMuller

9/12/2003 3:49:00 PM

0

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