SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Member
    Join Date
    Oct 2008
    Posts
    5
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Syntax question for Arrays

    I need help with determining whether a value in an array in non numeric. ere is the code

    class Array

    def sum
    raise "The mean could not be calculated for this array" if self.empty? or self.include?()
    inject{ |sum,x| sum ? sum + x.to_f : x.to_f }
    end

    Do I write self.include?(a-z)?

  2. #2
    Programming Team silver trophybronze trophy
    Mittineague's Avatar
    Join Date
    Jul 2005
    Location
    West Springfield, Massachusetts
    Posts
    17,147
    Mentioned
    190 Post(s)
    Tagged
    2 Thread(s)
    Hi lord12, welcome to the forums.
    I'm fairly new to Ruby, so I don't know if there is something like "is_numeric" to test data types, but maybe you could test against a regex something like
    Code Ruby:
    regex_pattern = /^\d+$/ #one or more digits only
    ...
     if self.empty? || self != regex_pattern
    *you may need to use
    self.to_s !=

  3. #3
    SitePoint Addict
    Join Date
    Feb 2007
    Posts
    270
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    try self.match(/\A\d+\Z/) and see if that works for you.


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
  •