Small improvements ideas ?

C

Chris

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

Thanks
 
M

Martin DeMello

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 ??

1. check out the andand library [http://andand.rubyforge.org/]

doit() if my_string.andand.downcase == "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

mystring !~ /this_regexp/

martin
 
S

Stefano Crocco

|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

I think you can:

if one_var && (not(my_string =~ /this_regexp/))

I hope this helps

Stefano
 
R

Robert Klemme

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
 
B

Brian Candler

Christophe said:
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 =~

doit if /this_regexp/ !~ my_string

or simply:

doit unless /this_regexp/ =~ my_string
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,022
Latest member
MaybelleMa

Latest Threads

Top