Parameter Type Checking in methods.Q?

Discussion in 'Ruby' started by John Pritchard-williams, Aug 29, 2008.

  1. Again...just wondering....(and you can probably tell I'm comparing Ruby
    with Java/C here again, following on from the "++" discussion...)

    def MyObject
    def my_method(obj)
    if obj.class!=String then raise "Wrong Type!"
    end
    ...stuff...
    end
    end

    Would it cause problems with the general syntax to include some sugar to
    rewrite this as:

    ...
    def my_method(String obj)
    end
    ...

    Or is there any quick easy meta-programming to label methods as
    expecting certain types?

    Or am I being a big-baby wanting this kind of check, and I should get on
    with the Dynamic Vibe of Ruby ? :)

    (I just noticed that sometimes I get lost in my own programs by not
    checking types thoroughly; yet Java would have forced me to stop and
    think...)

    John
    --
    Posted via http://www.ruby-forum.com/.
    John Pritchard-williams, Aug 29, 2008
    #1
    1. Advertising

  2. Hi --

    On Fri, 29 Aug 2008, John Pritchard-williams wrote:

    > Again...just wondering....(and you can probably tell I'm comparing Ruby
    > with Java/C here again, following on from the "++" discussion...)
    >
    > def MyObject
    > def my_method(obj)
    > if obj.class!=String then raise "Wrong Type!"
    > end
    > ...stuff...
    > end
    > end
    >
    > Would it cause problems with the general syntax to include some sugar to
    > rewrite this as:
    >
    > ...
    > def my_method(String obj)
    > end
    > ...
    >
    > Or is there any quick easy meta-programming to label methods as
    > expecting certain types?
    >
    > Or am I being a big-baby wanting this kind of check, and I should get on
    > with the Dynamic Vibe of Ruby ? :)
    >
    > (I just noticed that sometimes I get lost in my own programs by not
    > checking types thoroughly; yet Java would have forced me to stop and
    > think...)


    I hate to sound curmudgeonly but please check out the probably
    (literally) 2500 posts about this and related questions from the past.
    It's been discussed and explained mind-numbingly many times.


    David

    --
    Rails training from David A. Black and Ruby Power and Light:
    Intro to Ruby on Rails January 12-15 Fort Lauderdale, FL
    Advancing with Rails January 19-22 Fort Lauderdale, FL *
    * Co-taught with Patrick Ewing!
    See http://www.rubypal.com for details and updates!
    David A. Black, Aug 29, 2008
    #2
    1. Advertising

  3. > I hate to sound curmudgeonly but please check out the probably
    > (literally) 2500 posts about this and related questions from the past.
    > It's been discussed and explained mind-numbingly many times.
    >
    >
    > David


    Apologies. I did have a check on the forum beforehand but didn't find a
    match - perhaps I need to learn how to search properly... :)

    http://www.ruby-forum.com/forum/4?filter=parameter type method
    http://www.ruby-forum.com/forum/4?filter=parameter type check
    http://www.ruby-forum.com/forum/4?filter=parameter type
    http://www.ruby-forum.com/forum/4?filter=parameter type sugar

    (or else there is no simple answer I guess...)

    Cheers
    John


    --
    Posted via http://www.ruby-forum.com/.
    John Pritchard-williams, Aug 29, 2008
    #3
  4. 2008/8/29 David A. Black <>:

    > On Fri, 29 Aug 2008, John Pritchard-williams wrote:


    >> Or am I being a big-baby wanting this kind of check, and I should get on
    >> with the Dynamic Vibe of Ruby ? :)


    Exactly. :)

    >> (I just noticed that sometimes I get lost in my own programs by not
    >> checking types thoroughly; yet Java would have forced me to stop and
    >> think...)

    >
    > I hate to sound curmudgeonly but please check out the probably


    David, thanks for an English word I have never seen before! Now I
    just need to learn how to spell it without breaking my tongue... :)

    > (literally) 2500 posts about this and related questions from the past.
    > It's been discussed and explained mind-numbingly many times.


    You mean like http://www.lyricsfreak.com/p/pink floyd/comfortably numb_20108779.html
    ?

    John, suggested search terms to use "static typing", "dynamic typing",
    "duck typing".

    Cheers

    robert


    --
    use.inject do |as, often| as.you_can - without end
    Robert Klemme, Aug 29, 2008
    #4
  5. Hi --

    On Fri, 29 Aug 2008, John Pritchard-williams wrote:

    >> I hate to sound curmudgeonly but please check out the probably
    >> (literally) 2500 posts about this and related questions from the past.
    >> It's been discussed and explained mind-numbingly many times.
    >>
    >>
    >> David

    >
    > Apologies. I did have a check on the forum beforehand but didn't find a
    > match - perhaps I need to learn how to search properly... :)
    >
    > http://www.ruby-forum.com/forum/4?filter=parameter type method
    > http://www.ruby-forum.com/forum/4?filter=parameter type check
    > http://www.ruby-forum.com/forum/4?filter=parameter type
    > http://www.ruby-forum.com/forum/4?filter=parameter type sugar
    >
    > (or else there is no simple answer I guess...)


    Try googling for: "type checking" ruby (even though 99% of the time
    it's really class/module ancestry checking, it gets raised as type
    checking so that's a better bet for searching).


    David

    --
    Rails training from David A. Black and Ruby Power and Light:
    Intro to Ruby on Rails January 12-15 Fort Lauderdale, FL
    Advancing with Rails January 19-22 Fort Lauderdale, FL *
    * Co-taught with Patrick Ewing!
    See http://www.rubypal.com for details and updates!
    David A. Black, Aug 29, 2008
    #5
  6. > John, suggested search terms to use "static typing", "dynamic typing",
    > "duck typing".


    Thanks ! (and I'll stop spamming this subject now!)
    --
    Posted via http://www.ruby-forum.com/.
    John Pritchard-williams, Aug 29, 2008
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Tom Bradford
    Replies:
    4
    Views:
    624
    Florian Diesch
    Mar 10, 2006
  2. Delaney, Timothy (Tim)

    RE: Type Hinting vs Type Checking and Preconditions

    Delaney, Timothy (Tim), Mar 7, 2006, in forum: Python
    Replies:
    7
    Views:
    472
    Paul Boddie
    Mar 8, 2006
  3. jcpc
    Replies:
    2
    Views:
    1,474
  4. Kenneth McDonald
    Replies:
    5
    Views:
    313
    Kenneth McDonald
    Sep 26, 2008
  5. Tricky
    Replies:
    1
    Views:
    835
    JimLewis
    Feb 10, 2012
Loading...

Share This Page