Frage

I am trying to play with regexes and test my own code in ruby. Using the example below, I would anticipate the final puts to return true, but it does not. The check_password method does, however, return "Your pw does not work because it is too short" Why doesn't the true/false check return true?

def err_message(reason)
  puts "Your pw does not work because " + reason
end

def check_password(password)
    if password.length<6
        return err_message("it is too short")
    elsif password.index(/[A-Z]/)==nil
        return err_message("it does not contain a capital letter")
    elsif password.index(/\d|[!,@,#,$,%,&,*,+,:,?]/)==nil
        return err_message("it needs either a digit or special character")
    elsif password.index(/([!,@,#,$,%,&,*,+,:,?\w])/)>0
        return err_message("nope. !,@,#,$,%,&,*,+,:,? are the only special characters available")
    else
        return "Valid Password!"
    end
end

puts check_password("aaaaa")=="Your pw does not work because it is too short"
War es hilfreich?

Lösung

err_message would evaluate puts "Your pw does not work because " + reason and return nil since nil is the return value from puts, change the method definition to look like below:

def err_message(reason)
  "Your pw does not work because " + reason
end

then:

puts check_password("aaaaa")=="Your pw does not work because it is too short"
# => true

Andere Tipps

Here are some suggestions for improving your method. Normally I would make it with a comment, but formatting restrictions make that impractical in this case. Consider writing it like this:

SPECIAL_CHARS = '!@#$%&*+:?'

def check_password(password)
  "Your pw does not work because " +
  case
  when password.length < 6
    "it is too short"
  when password.match(/[A-Z]/).nil?
    "it does not contain a capital letter"
  when password.match(/\d|[{SPECIAL_CHARS}]/).nil?
    "it needs either a digit or special char"
  else
    return "Valid Password!"
  end
end  

check_password("abcde")
  #=> "Your pw does not work because it is too short" 
check_password("abcdef")
  #=> "Your pw does not work because it does not contain a capital letter"
check_password("abcdeF")
  #=> "Your pw does not work because it needs either a digit or special char"
check_password("abcdeF@")
  #=> "Valid Password!"
check_password("abcdeF1")
  #=> "Valid Password!"

I removed

  when password.index(/([!,@,#,$,%,&,*,+,:,?\w])/) > 0
    "nope. !,@,#,$,%,&,*,+,:,? are the only special characters available"    

because I didn't understand what it was checking for.

Notice that there is only one return, and that is when the password is valid. If the password is not valid, the result of the case statement (a string) is added to the string, "Your pw does not work because " and returned (since it would be the last statement executed by the method--no return needed).

I replaced index with match, though you could alternatively write:

when !(password =~ /[A-Z]/)

or

when !password[/[A-Z]/]
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top