Professional Web Applications Themes

Checking the parameters "type" - Ruby

Hi all, I've trying to find out how to check the class of a parameter. Right, I talking about this: class TestClass # arg1 must be String instance def myMethod(arg1, arg2) # Code, code and more code. end end I think that I could solve in this way: class TestClass # arg1 must be a String instance def myMethod(arg1, arg2) if not arg1.kind_of?(String) # Error! Some code must be added! end end end Right, we are going to suppose now that I want to add an Integer as attribute (myAttribute): class TestClass attr_writer :myAttribute ... end This attribute must be ...

  1. #1

    Default Checking the parameters "type"

    Hi all,

    I've trying to find out how to check the class of a parameter. Right, I
    talking about this:

    class TestClass
    # arg1 must be String instance
    def myMethod(arg1, arg2)
    # Code, code and more code.
    end
    end

    I think that I could solve in this way:

    class TestClass
    # arg1 must be a String instance
    def myMethod(arg1, arg2)
    if not arg1.kind_of?(String)
    # Error! Some code must be added!
    end
    end
    end

    Right, we are going to suppose now that I want to add an Integer as attribute
    (myAttribute):

    class TestClass
    attr_writer :myAttribute
    ...
    end

    This attribute must be writable. But, there's a little trouble:
    How could I assure that the parameter passed to myAttribute= method is
    Integer? Must I implement the method by myself?

    class TestClass
    def myAttribute=(value)
    if not myAttribute
    end
    end

    Can it be automatized in some way?

    Thank you all very much!

    --
    Imobach González Sosa
    net
    org



    Imobach Guest

  2. #2

    Default Re: Checking the parameters "type"

    il Fri, 30 Jan 2004 05:44:26 +0900, "Imobach González Sosa"
    <net> ha scritto::
     

    look on raa for types.rb and strongtyping modules. These will make it
    easy for you to check types and will give you some more goodies ( like
    kind of MMD )
    gabriele Guest

  3. #3

    Default Re: Checking the parameters "type"

    Hi!

    * Imobach González Sosa: 

    Try this:

    class TestClass
    def myMethod(arg1, arg2)
    unless arg1.class == String
    raise ArgumentError.exception('first argument must be string')
    end
    puts arg1, arg2
    end
    end

    Test:

    test = TestClass.new
    test.myMethod('foo', 'bar')
    test.myMethod(1, 'bar')


     

    Why not? It is easy to define a setter:

    class TestClass
    def myAttribute=(value)
    if value.class <= Integer
    myAttribute = value
    else
    raise ArgumentError.exception('assignment requires integer')
    end
    end

    def myAttribute
    myAttribute
    end
    end

    I did add a getter so that testing is possible.

    test = TestClass.new
    test.myAttribute = 10
    puts test.myAttribute
    test.myAttribute = 'foo'
    puts test.myAttribute

    Question to community: Raise an ArgumentError or a TypeError?

    Josef 'Jupp' SCHUGT
    --
    http://oss.erdfunkstelle.de/ruby/ - German comp.lang.ruby-FAQ
    http://rubyforge.org/users/jupp/ - Ruby projects at Rubyforge
    -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
    Germany 2004: To boldly spy where no GESTAPO / STASI has spied before



    Josef Guest

  4. #4

    Default Re: Checking the parameters "type"

    El Jueves, 29 de Enero de 2004 22:44, gabriele renzi escribió: 
    >
    > look on raa for types.rb and strongtyping modules. These will make it
    > easy for you to check types and will give you some more goodies ( like
    > kind of MMD )[/ref]

    Thank you gabriele and Jupp for your answers. At this moment, I'm trying
    StrongTyping, and it seems to work just fine for my purposes.

    See ya!



    Imobach Guest

Similar Threads

  1. Distiller uses Finder creator/type "ORAC"/" FDP"
    By Yllä_Pito@adobeforums.com in forum Adobe Acrobat Macintosh
    Replies: 1
    Last Post: November 4th, 11:03 AM
  2. how i use <cfinput type="file" ...> in cfformtype="flash
    By Prashantgenial in forum Coldfusion Flash Integration
    Replies: 1
    Last Post: June 26th, 11:04 PM
  3. CFINPUT type="radio" w/ "value" requires "label"
    By Iceborer in forum Macromedia ColdFusion
    Replies: 2
    Last Post: February 21st, 06:16 PM
  4. #25366 [NEW]: form buttons of type "image" dont send "submit" $_POST variable in IE
    By jordanolsommer at imap dot cc in forum PHP Development
    Replies: 0
    Last Post: September 2nd, 07:54 PM
  5. Replies: 1
    Last Post: July 20th, 08:30 AM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not 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