1

For ex: a.text has name "capybara" and I need to check whether the string has characters "ba" in it.

if a.text.contains?("ba")
  # do something
else
  # do something
end
1
  • what does this have to do with Capybara? is this in a capybara rspec feature test? Or just plain ruby?
    – mswieboda
    Commented Jul 17, 2014 at 16:56

2 Answers 2

6
text = "capybara"
text.include?("ba")
=> true

so you could do:

if text.include?("ba")
  # do something
else
  # do something else
end
0
2

If you want more flexibility with regex, use match like:

if a.text.match(/ba/)
  # do something
end

Not the answer you're looking for? Browse other questions tagged or ask your own question.