I'm trying to use regex as the conditional in a Ruby (1.9.2) if statement but it keeps returning true even when the regex evaluates to nil
if (params[:test] =~ /foo/)
return "match"
else
return "no match"
end
The above returns "match" even when Rails.logger.info(params[:test]) shows test as set to "bar"
params[:test] == "bar"is faulty."bar" =~ /foo/returns nil. Your issue is somewhere else...Rails.logger.info(params[:test].class). It just can't be thatparams[:text] == "bar", otherwise your Ruby is buggy, which seems very unlikely. Can you use pry to peek at the current execution index before theif? This will let you debug the current value ofparams[:test]better than a logging statement (and you can try the regex matching interactively).