Ruby class that validates a string I have this class that needs to do three things: 


Determine if a string is 8 characters or longer 
Determine if a string has a number in it
Determine if a string has a capital letter in it


NB: I don't want to use regex. Is there a better way of doing this?

class ValidateString
  def initialize(string)
    @string = string

    if valid 
      puts @string 
    end
  end

  def valid
    if has_number? and has_upper? and has_length?
      return @string
    end
  end

  def has_number?
    @string.count("0-9") &gt; 0
  end

  def has_upper?
    @string =~ /[A-Z]/
  end

  def has_length?
    @string.length &gt;= 8
  end
end
ValidateString is strange name for a class. StringValidator would be more apt.
Don't make your constructor print anything. Your class exists to validate a string, not print things to stdout. Leave that to some other piece of code.
valid should be named valid? and return a boolean. And you can omit the return keyword.
Add attr_reader :string, and replace your @string with just string
8 is a magic number, and should be replaced with constant (e.g. MIN_STRING_LENGTH)
Presumably, this exists to check for simple passwords. However, you're not checking that the string contains both upper- and lowercase letters. An all-uppercase string with a number would pass validation. For passwords, you'd want to enforce that it's mixed casing.


I'd just write a method:

def valid_string?(string)
  return false unless string.length &gt;= 8
  return false unless string =~ /[0-9]/
  return false unless string =~ /[[:upper:]]/ # allows non-Latin letters
  return false unless string =~ /[[:lower:]]/ # ditto
  true
end


Of course, you'd be better off using something like zxcvbn to check password entropy. Your rules allow for the good ol' Password1 string just fine.