to check for numericality

Discussion in 'Ruby' started by Sijo Kg, May 8, 2008.

  1. Sijo Kg

    Sijo Kg Guest

    Hi
    I have
    @val=SomeValue #for example 00,02,aa
    How can I check @val contains only numeriacal(including zero)
    values..I have to avoid aa above (for example )

    Sijo
     
    Sijo Kg, May 8, 2008
    #1
    1. Advertisements

  2. Sijo Kg

    7stud -- Guest

    data = ["00", "aa", "0a", "a0", "02"]

    data.each do |str|
    if str =~ /\D/
    print "bad data: ", str
    puts
    end
    end

    --output:--
    bad data: aa
    bad data: 0a
    bad data: a0
     
    7stud --, May 8, 2008
    #2
    1. Advertisements

  3. @val = someValue if ( Integer( someValue) rescue Float( someValue )
    rescue false)
     
    Farrel Lifson, May 8, 2008
    #3
  4. Sijo Kg

    Sijo Kg Guest

    Hi
    Thanks for your reply It worked.At first I tried like
    if /^(\d)(\d)$/.match(@val) ==nil #But not worked..Anyway thanks

    Sijo
     
    Sijo Kg, May 8, 2008
    #4
  5. [Note: parts of this message were removed to make it a legal post.]

     
    Colin Bartlett, May 8, 2008
    #5
  6. Sijo Kg

    Robert Dober Guest

    Sure is ;) you will know that it is an Integer. But I guess you do not
    care, in that case no, just go for the Float() rescue idiom.
    HTH
    Robert
     
    Robert Dober, May 8, 2008
    #6
  7. 'Float' is a class, ins't it? So how can you do "Float(whatever)" as if
    it were a method?

    Now, when I do "Kernel.methods" I _do_ see "Float", "Integer" etc in the
    list. Is it the method "Kernel.Float" that eventually gets called? How
    does ruby know to distinguish between the class Float and the method
    Kernel.Float?

    BTW, why, when I do "Object.methods" don't I see "Float", "Integer" etc
    in the list? Some more methods too are missing. Object is supposed to
    inherit them from Kernel, doesn't it?
     
    Albert Schlef, May 8, 2008
    #7
  8. Sijo Kg

    ts Guest

    Because you use it like a method, i.e. you have added () after it

    Float() ==> method
    Float ==> constant
    #Float is a global function, i.e. a Kernel private method and a
    Kernel singleton method


    vgs% ruby -e 'p Kernel.singleton_methods.include?("Float")'
    true
    vgs%

    vgs% ruby -e 'p Kernel.private_instance_methods.include?("Float")'
    true
    vgs%

    vgs% ruby -e 'p Object.private_methods.include?("Float")'
    true
    vgs%


    Guy Decoux
     
    ts, May 8, 2008
    #8
  9. I see.
    Thanks Guy.
     
    Albert Schlef, May 8, 2008
    #9
  10. How would you fix this so it would work for valid numbers such as
    "1,234.56"? I realize that the OP uses comma separators, but Excel
    would import the above number as "1,234.56" to a .csv file.

     
    Michael W. Ryder, May 8, 2008
    #10
  11. Sijo Kg

    ara.t.howard Guest

    that is a bug:

    cfp:~ > ruby -e' p Integer( bug = nil ) '
    0


    simply use 'Float(value) rescue false'



    a @ http://codeforpeople.com/
     
    ara.t.howard, May 8, 2008
    #11
  12. -----BEGIN PGP SIGNED MESSAGE-----
    Hash: SHA1

    Michael W. Ryder wrote:
    | Farrel Lifson wrote:
    |> @Val = someValue if ( Integer( someValue) rescue Float( someValue )
    |> rescue false)
    |>
    |
    | How would you fix this so it would work for valid numbers such as
    | "1,234.56"? I realize that the OP uses comma separators, but Excel
    | would import the above number as "1,234.56" to a .csv file.

    You just failed i18n. 1.234,56 is a Continental European way to express
    that, and Excel, for example, is aware of that distinction (so should
    the OS be). ;)


    And with that last thought: leverage the locale settings of the Os,
    somehow? Maybe via OpenOffice or Ruby bindings into this (if such a
    library is available)?

    - --
    Phillip Gawlowski
    Twitter: twitter.com/cynicalryan
    Blog: http://justarubyist.blogspot.com

    ~ Well, it just seemed wrong to cheat on an ethics test. -- Calvin
    -----BEGIN PGP SIGNATURE-----
    Version: GnuPG v1.4.8 (MingW32)
    Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org

    iEYEARECAAYFAkgjWm0ACgkQbtAgaoJTgL9lRgCdEWEeURvczOdkFjluzwUxmQiN
    BhMAoIlqJSJ/21OIb0oLZh5vPyApP/Cz
    =oO45
    -----END PGP SIGNATURE-----
     
    Phillip Gawlowski, May 8, 2008
    #12
    1. Advertisements

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.
Similar Threads
Loading...