Passing an Object Class from a method to a caller

Ask a Question related to Ruby, Design and Development.

  1. #1

    Default 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>&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;&nbs p;&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;&nbs p; i =3D=20
    0<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; for t in=20
    aTypes<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbs p;&nbsp;&nbsp;&nbsp;&nb=
    sp; i=20
    +=3D =
    1<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nb sp;&nbsp;&nbsp;&nbsp; =
    if=20
    o.is_a?t then=20
    <BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbs p;&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;&n bsp;&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;&nbs p; =
    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=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>&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=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>&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--


    RLMuller Guest

  2. Similar Questions and Discussions

    1. 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...
    2. 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 ...
    3. 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...
    4. 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...
    5. Passing a method block to a parent class
      If I have **** CODE **** class A def method1( argument ) yield( argument ) end end
  3. #2

    Default 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

  4. #3

    Default 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:
    > 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=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=
    =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 - [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

  5. #4

    Default Re: Passing an Object Class from a method to a caller

    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

    Sean O'Dell Guest

  6. #5

    Default Re: Passing an Object Class from a method to a caller

    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: [email]dblack@superlink.net[/email]
    work: [email]blackdav@shu.edu[/email]
    Web: [url]http://pirate.shu.edu/~blackdav[/url]


    dblack@superlink.net Guest

  7. #6

    Default Re: Passing an Object Class from a method to a caller

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


    Michael Granger Guest

  8. #7

    Default Re: Passing an Object Class from a method to a caller

    [email]dblack@superlink.net[/email] wrote:
    >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).
    >
    >
    >
    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



    Michael Garriss Guest

  9. #8

    Default 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

  10. #9

    Default 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:
    > 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
    Jason Creighton Guest

  11. #10

    Default 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

  12. #11

    Default 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

Posting Permissions

  • You may not post new threads
  • You may post replies
  • You may not post attachments
  • You may not edit your posts

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139