Passing an Object Class from a method to a caller

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)

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

Why not instead use obj.class.name:

obj = “abc”
puts “Type of object ‘#{obj}’ is #{obj.class.name}”

···

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?


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

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.FaerieMUD.org/

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

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

Hi –

···

On Fri, 12 Sep 2003, 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’.

I think it’s actually returning the result of break, which is nil
(but can be assigned with “break val” in 1.8.0).

David


David Alan Black
home: dblack@superlink.net
work: blackdav@shu.edu
Web: http://pirate.shu.edu/~blackdav

Hi –

···

On Fri, 12 Sep 2003, Michael Granger wrote:

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.

I think break (pre-1.8.0 or without explicit value in 1.8.0) evaluates
to nil:

$ ruby -ve ‘def x; for a in [1]; b=2; break; end; end; p x’
ruby 1.6.8 (2002-12-24) [powerpc-darwin6.3]
nil

David


David Alan Black
home: dblack@superlink.net
work: blackdav@shu.edu
Web: http://pirate.shu.edu/~blackdav

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

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

dblack@superlink.net wrote:

Hi –

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

I think it’s actually returning the result of break, which is nil
(but can be assigned with “break val” in 1.8.0).

def this
break “cool” while true
end
→ nil
this
→ “cool”

I can’t remember the last day I went without learning something cool
about Ruby.

Michael

···

On Fri, 12 Sep 2003, Michael Garriss wrote:

I stand corrected.

···

On Thursday, September 11, 2003, at 03:49 PM, dblack@superlink.net wrote:

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.

I think break (pre-1.8.0 or without explicit value in 1.8.0) evaluates
to nil:

$ ruby -ve ‘def x; for a in [1]; b=2; break; end; end; p x’
ruby 1.6.8 (2002-12-24) [powerpc-darwin6.3]
nil


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

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

Jason Creighton

···

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

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

You’re quite welcome.

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

I know the feeling all too well. =:)

···

On Friday, September 12, 2003, at 09:37 AM, Richard wrote:


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