21

I want to create a test that returns either true or false for email handling.

For now, if the email address starts with r+ then it's true otherwise it's false. This will help our server ignore a lot of the SPAM we are getting hit with.

Examples:

r+kldslkadslkadslk@site.com .. true
r+123123312@site.com .. true
vigraaaa@site.com .. FALSE

What's the most efficient way to handle this with Rails/ruby/regex?

Thanks

GOAL

Is a one liner in rails/ruby with:

ABORT if XXXXX == 0
AnApprentice
  • 108,152
  • 195
  • 629
  • 1,012

5 Answers5

37

This will match:

/^r\+.*@site.com$/

Examples:

>> 'r+kldslkadslkadslk@site.com' =~ /^r\+.*@site.com$/ #=> 0
>> 'vigraaaa@site.com' =~ /^r\+.*@site.com$/ #=> nil

Since everything that isn't nil or false is truthy in Ruby, you can use this regex in a condition. If you really want a boolean you can use the !! idiom:

>> !!('vigraaaa@site.com' =~ /^r\+.*@site.com$/) #=> false
>> !!('r+kldslkadslkadslk@site.com' =~ /^r\+.*@site.com$/) #=> true
Michael Kohl
  • 66,324
  • 14
  • 138
  • 158
11

If you're in Rails, there's a starts_with? method on strings:

"foo".starts_with?('f') # => true
"foo".starts_with?('g') # => false

Outside of Rails, regexes are a reasonable solution:

"foo" =~ /^f/ # => true
"foo" =~ /^g/ # => false

Because Ruby uses truthiness in if statements, if you do end up using regexes, you can just use the return value to switch:

if "foo" =~ /^f/
  puts "Was true!"
else
  puts "Was false!"
end

If you're writing a method and want to return a boolean result, you could always use the double bang trick:

def valid_email?
  !!("foo" =~ /^f/)
end

Rubular (rubular.com) is a good site for testing Ruby regexes pre-1.9. (1.9's regexes added things like lookahead.)

Kyle
  • 1,278
  • 6
  • 11
  • Outside of Rails there's also a String "start_with?" (note singular). – Dave Newton Sep 09 '11 at 18:24
  • Not sure I like the starts_with idea, it would only match r and not r+ which would be much more useful against spammers – AnApprentice Sep 09 '11 at 18:33
  • Why couldn't you do `start_with? "r+"`? I mean, I'd use a regex too so it could match anything before a `+`, but start_with? will match whatever you give it. – Dave Newton Sep 09 '11 at 19:05
  • Note: `start_with?` has been added to Ruby Core. See http://ruby-doc.org/core-2.1.2/String.html#method-i-start_with-3F – bryanbraun May 31 '14 at 02:20
  • 1
    I think the above answer is misleading because `"foo" =~ /^f/ #=> 0` and `"foo" =~ /^g/ #=> nil` – mbigras Dec 15 '16 at 06:43
10

If you don't want to use an "!!" operator:

!!("foo" =~ /^f/)

you could use a ternary operator (might look more obvious):

"foo" =~ /^f/ ? true : false
Evmorov
  • 1,134
  • 22
  • 28
7

You can use the '===' operator as well

/f/ === 'foo' #=> true

/f/ === 'bat' #=> false

Note: The regex part is on the left:

/YOUR_REGEX/ === 'YOUR_STRING'

Pascal
  • 1,158
  • 17
  • 20
1

Regexp#match? was added to Ruby-2.4.0. See Regexp#match documentation.

irb(main):001:0> "ack!".match? /a(b|c)/
=> true
irb(main):002:0> "add".match? /a(b|c)/
=> false
Pete
  • 10,310
  • 7
  • 53
  • 59