Hello, I would like to know how to shorten these two lines
1) doit() if my_string && my_string.downcase == 'hello'
2) doit() if one_var && (my_string =~ /this_regexp/) == nil
Case 1) is a way to check for my_string beeing nil (would break on
downcase call). But it's verbose, nothing better ??
Case 2) I want doit beeing call if regexp don't match. would I use !
(my_string =~ /this_regexp/) but I don't like using ! because it's not
very readable and I can't use not keyword in this case
Case 2) I want doit beeing call if regexp don't match. would I use !
(my_string =~ /this_regexp/) but I don't like using ! because it's not
very readable and I can't use not keyword in this case
mystring !~ /this_regexp/
martin
···
On Wed, Sep 8, 2010 at 5:08 PM, Chris <christophe.gimenez@gmail.com> wrote:
>Hello, I would like to know how to shorten these two lines
>
>1) doit() if my_string && my_string.downcase == 'hello'
>2) doit() if one_var && (my_string =~ /this_regexp/) == nil
>
>Case 1) is a way to check for my_string beeing nil (would break on
>downcase call). But it's verbose, nothing better ??
You can do
if (my_string || '').downcase == 'hello'
This way, if my_string is nil, the parentheses will evaluate to '' which is a
string and will always be different from 'hello'
>Case 2) I want doit beeing call if regexp don't match. would I use !
>(my_string =~ /this_regexp/) but I don't like using ! because it's not
>very readable and I can't use not keyword in this case
Hello, I would like to know how to shorten these two lines
1) doit() if my_string && my_string.downcase == 'hello'
doit if /\Ahello\z/i =~ my_string
2) doit() if one_var && (my_string =~ /this_regexp/) == nil
doit if one_var && /rx/ !~ my_string
Case 1) is a way to check for my_string beeing nil (would break on
downcase call). But it's verbose, nothing better ??
Case 2) I want doit beeing call if regexp don't match. would I use !
(my_string =~ /this_regexp/) but I don't like using ! because it's not
very readable and I can't use not keyword in this case
Kind regards
robert
···
On Wed, Sep 8, 2010 at 1:38 PM, Chris <christophe.gimenez@gmail.com> wrote:
Hello, I would like to know how to shorten these two lines
1) doit() if my_string && my_string.downcase == 'hello'
doit if /\Ahello\z/i =~ my_string
2) doit() if one_var && (my_string =~ /this_regexp/) == nil
Case 2) I want doit beeing call if regexp don't match. would I use !
(my_string =~ /this_regexp/) but I don't like using ! because it's not
very readable and I can't use not keyword in this case
I'm not sure what "one_var" is. If you meant "my_string", then note that
you can use !~ as the inverse of =~