Small improvements ideas ?

Discussion in 'Ruby' started by Chris, Sep 8, 2010.

  1. Chris

    Chris Guest

    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
     
    Chris, Sep 8, 2010
    #1
    1. Advertising

  2. On Wed, Sep 8, 2010 at 5:08 PM, 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 ??


    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
     
    Martin DeMello, Sep 8, 2010
    #2
    1. Advertising

  3. 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
     
    Stefano Crocco, Sep 8, 2010
    #3
  4. On Wed, Sep 8, 2010 at 1:38 PM, Chris <> 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


    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

    --
    remember.guy do |as, often| as.you_can - without end
    http://blog.rubybestpractices.com/
     
    Robert Klemme, Sep 8, 2010
    #4
  5. Christophe Gimenez 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 =~

    doit if /this_regexp/ !~ my_string

    or simply:

    doit unless /this_regexp/ =~ my_string
    --
    Posted via http://www.ruby-forum.com/.
     
    Brian Candler, Sep 8, 2010
    #5
  6. On Wed, Sep 8, 2010 at 5:50 PM, Robert Klemme
    <> wrote:
    > On Wed, Sep 8, 2010 at 1:38 PM, Chris <> 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
     
    Martin DeMello, Sep 8, 2010
    #6
  7. Chris

    Chris Guest

    Thanks for your replies, I was totally unware of !~ !!!
     
    Chris, Sep 9, 2010
    #7
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. David
    Replies:
    4
    Views:
    417
    David
    Nov 29, 2005
  2. Matthew Scott
    Replies:
    0
    Views:
    307
    Matthew Scott
    May 6, 2005
  3. Rotem
    Replies:
    23
    Views:
    633
    Trent Mick
    Sep 15, 2005
  4. Rodrigo Almeida

    Improvements to RDoc (ideas for GSoC)

    Rodrigo Almeida, Mar 29, 2008, in forum: Ruby
    Replies:
    9
    Views:
    79
    Eric Hodel
    Apr 1, 2008
  5. eli m

    Small program ideas

    eli m, Feb 16, 2013, in forum: Python
    Replies:
    20
    Views:
    365
    Neil Cerutti
    Mar 29, 2013
Loading...

Share This Page