0
給出一個字符串像[email protected],[email protected],[email protected]考慮到郵件列表時,如何確定是否電子郵件存在
我如何使用紅寶石,看是否該字符串包含[email protected]?
給出一個字符串像[email protected],[email protected],[email protected]考慮到郵件列表時,如何確定是否電子郵件存在
我如何使用紅寶石,看是否該字符串包含[email protected]?
emails = "[email protected], [email protected], etc..."
emails.include?('[email protected]')
$ true
emails.include?([email protected])
$false
如果你想傳遞多個電子郵件,包括你可以,如果你使用的紅寶石1.9.2使用下面的代碼:
emails.include?(email1 || email2)
至於低於紅寶石版本,可以這樣做:
emails = "[email protected], [email protected], etc..."
emails_to_check = [email1, email2]
emails_to_check.each do |email|
return true if emails.include(email)
end
由於電子郵件是一個字符串,它帶來了一些問題。你可以創建一個數組或電子郵件來檢查每個方法的字符串,但你的代碼似乎是一樣好,如果不是更好。