# Small improvements ideas?

**URL:** <https://www.ruby-forum.com/t/small-improvements-ideas/194643>\
**Category:** Ruby\
**Created:** [September 8, 2010, 1:41pm UTC](https://www.ruby-forum.com/t/small-improvements-ideas/194643 "2010-09-08T13:41:42Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![christophe1](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/b3f665/32.png) [@christophe1](https://www.ruby-forum.com/u/christophe1)\
**Post date:** [September 8, 2010, 1:41pm UTC](https://www.ruby-forum.com/t/small-improvements-ideas/194643/1 "2010-09-08T13:41:42Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![christophe1](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/b3f665/32.png) [@christophe1](https://www.ruby-forum.com/u/christophe1)\
**Post date:** [September 8, 2010, 2:06pm UTC](https://www.ruby-forum.com/t/small-improvements-ideas/194643/2 "2010-09-08T14:06:53Z")

</div>

On Wed, Sep 8, 2010 at 5:08 PM, Chris [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
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 ??

1. check out the andand library [[http://andand.rubyforge.org/](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

---

<div class="post-metadata">

**Author:** ![christophe1](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/b3f665/32.png) [@christophe1](https://www.ruby-forum.com/u/christophe1)\
**Post date:** [September 8, 2010, 2:09pm UTC](https://www.ruby-forum.com/t/small-improvements-ideas/194643/3 "2010-09-08T14:09:13Z")

</div>

On Wednesday 08 September 2010, Chris 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

I think you can:

if one\_var && (not(my\_string =~ /this\_regexp/))

> |Thanks

I hope this helps

Stefano

---

<div class="post-metadata">

**Author:** ![christophe1](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/b3f665/32.png) [@christophe1](https://www.ruby-forum.com/u/christophe1)\
**Post date:** [September 8, 2010, 2:25pm UTC](https://www.ruby-forum.com/t/small-improvements-ideas/194643/4 "2010-09-08T14:25:22Z")

</div>

Christophe G. 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

> 1. 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

---

<div class="post-metadata">

**Author:** ![christophe1](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/b3f665/32.png) [@christophe1](https://www.ruby-forum.com/u/christophe1)\
**Post date:** [September 8, 2010, 2:31pm UTC](https://www.ruby-forum.com/t/small-improvements-ideas/194643/5 "2010-09-08T14:31:28Z")

</div>

On Wed, Sep 8, 2010 at 5:50 PM, Robert K.  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> On Wed, Sep 8, 2010 at 1:38 PM, Chris [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) 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

ruby-1.9.2-p0 \> /hello/ =~ nil  
=\> nil

wow, learnt something today!

martin

---

<div class="post-metadata">

**Author:** ![christophe1](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/b3f665/32.png) [@christophe1](https://www.ruby-forum.com/u/christophe1)\
**Post date:** [September 8, 2010, 2:24pm UTC](https://www.ruby-forum.com/t/small-improvements-ideas/194643/6 "2010-09-08T14:24:27Z")

</div>

On Wed, Sep 8, 2010 at 1:38 PM, Chris [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
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

> 1. 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

---

<div class="post-metadata">

**Author:** ![christophe1](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/b3f665/32.png) [@christophe1](https://www.ruby-forum.com/u/christophe1)\
**Post date:** [September 9, 2010, 8:15am UTC](https://www.ruby-forum.com/t/small-improvements-ideas/194643/7 "2010-09-09T08:15:49Z")

</div>

Thanks for your replies, I was totally unware of !~ !!!
