I can't quite figure out what I'm doing wrong here..
if @calc.docket_num =~ /DC-000044-10/ || @calc.docket_num =~ /DC-67-09/
@calc.lda = true
else
@calc.lda = false
end
But it seems that @calc.docket_num
can be any string whatsoever and it always returns as true
.
Am I not doing this right?
JavaScript RegExp test()If it finds a match, it returns true, otherwise it returns false.
Does re search return Boolean? Use bool() and re.search() to use regular expressions to return a boolean. Call re.search(pattern, string) to check if pattern occurs anywhere in string . Use bool() to convert the result to a boolean.
re. match(...) would return true if the string's beginning part match the regular pattern. While search will confirm the pattern anywhere in the string.
(? i) makes the regex case insensitive. (? c) makes the regex case sensitive.
This is a one-liner:
@calc.lda = !!(@calc.docket_num =~ /DC-000044-10|DC-67-09/)
The !!
forces the response to true/false, then you can assign your boolean variable directly.
Alternatively you could use the triple equals (===
) operator for the Regexp class which is used for determining equality when using case
syntax.
@calc.lda = /DC-000044-10|DC-67-09/ === @calc.docket_num
@calc.lda
=> true
BEWARE
/Regexp/ === String
is totally different than String === /Regexp/
!!!! The method is not commutative. Each class implements ===
differently. For the question above, the regular expression has to be to the left of ===
.
For the Regexp implementation, you can see more documentation on this (as of Ruby 2.2.1) here.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With