> class User < AR::B
> INVALID_EMAILS = %w(gmail.com hotmail.com)
> def self.valid_email?(email)
> reg = Regexp.new(INVALID_EMAILS*'|') # *'string' is an alias
> for .join('string') use whichever you preffer
> matches = reg.match(email)
> return matches == 0
> end
> end
>
A couple additional notes: The logical test ('matches == 0') should
actually be 'matches.size == 0'. Also, unless you are going to change
your INVALID_EMAILS array at runtime you could generate the regexp on
application boot preventing it from having to be created every time
you run the code. This is a premature optimization but it is also a
valid way of doing things. Also you can do the logical test on the
same line turning #valid_email? into a one liner.
So the user class would look something like this now:
class User < AR::B
INVALID_EMAILS = %w(gmail.com hotmail.com)
INVALID_EMAIL_REGEXP = Regexp.new(INVALID_EMAILS*'|')
def self.valid_email?(email)
return INVALID_EMAIL_REGEXP.match(email).size == 0
end
end
Another note (last one... maybe) you could use empty? instead of size
but you'd have to negate the result i.e.:
return !INVALID_EMAIL_REGEXP.match(email).empty?
--
You received this message because you are subscribed to the Google Groups "Ruby
on Rails: Talk" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to
[email protected].
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en.