Newbie: if / elseif

Discussion in 'Ruby' started by planetthoughtful, Mar 14, 2007.

  1. Hi All,

    I'm a little confused about why the following two pieces of code seem
    to behave differently:

    if $val =~ /this/i
    puts "this"
    elseif $val =~ /that/i
    puts "that"
    elseif $val =~ /the other/i
    puts "the other"
    end

    if $val contains "this is the other value", for some reason "the
    other" is not put by the last elseif statement. However, the following
    code works as expected:

    if $val =~ /this/i
    puts "this"
    end
    if $val =~ /that/i
    puts "that"
    end
    if $val =~ /the other/i
    puts "the other"
    end

    Am I missing something about if / elseif in ruby?

    Any help appreciated!

    pt
     
    planetthoughtful, Mar 14, 2007
    #1
    1. Advertising

  2. On Mar 15, 9:59 am, "planetthoughtful" <>
    wrote:

    > Am I missing something about if / elseif in ruby?


    Obviously I was missing something - the correct syntax. I'm now aware
    that the syntax should be "elsif" rather than "elseif".

    Thanks all,

    pt
     
    planetthoughtful, Mar 15, 2007
    #2
    1. Advertising

  3. planetthoughtful

    7stud 7stud Guest

    planetthoughtful wrote:
    > On Mar 15, 9:59 am, "planetthoughtful" <>
    > wrote:
    >
    >> Am I missing something about if / elseif in ruby?

    >
    > Obviously I was missing something - the correct syntax. I'm now aware
    > that the syntax should be "elsif" rather than "elseif".
    >
    > Thanks all,
    >
    > pt


    You're not the only one. I wasn't quite so generous:

    http://www.ruby-forum.com/topic/100350#new


    --
    Posted via http://www.ruby-forum.com/.
     
    7stud 7stud, Mar 15, 2007
    #3
  4. > if $val =~ /this/i
    > puts "this"
    > elseif $val =~ /that/i
    > puts "that"
    > elseif $val =~ /the other/i
    > puts "the other"
    > end


    Just remember that even when you spell it right, it's going to drop
    out of the whole construct just as soon as it fulfills a RegEx. After
    it prints "this," it stops looking at any of the other "elsif"
    statements.

    That short circuiting is a feature. =)

    -Augie
     
    Augie De Blieck Jr., Mar 15, 2007
    #4
  5. planetthoughtful

    Chad Perrin Guest

    On Thu, Mar 15, 2007 at 12:12:17PM +0900, Augie De Blieck Jr. wrote:
    > >if $val =~ /this/i
    > > puts "this"
    > >elseif $val =~ /that/i
    > > puts "that"
    > >elseif $val =~ /the other/i
    > > puts "the other"
    > >end

    >
    > Just remember that even when you spell it right, it's going to drop
    > out of the whole construct just as soon as it fulfills a RegEx. After
    > it prints "this," it stops looking at any of the other "elsif"
    > statements.
    >
    > That short circuiting is a feature. =)


    Indeed -- that's the whole point of using elsif rather than just a list
    of separate if statements. The if/elsif/else construct is used for
    mutually exclusive flow control, where only one of several is to be
    executed. Using a series of if statements considers each in a vacuum,
    in order, while if/elsif/else considers them all as part of a greater
    whole.

    --
    CCD CopyWrite Chad Perrin [ http://ccd.apotheon.org ]
    "Real ugliness is not harsh-looking syntax, but having to
    build programs out of the wrong concepts." - Paul Graham
     
    Chad Perrin, Mar 15, 2007
    #5
  6. On Mar 15, 1:12 pm, "Augie De Blieck Jr." <> wrote:
    > > if $val =~ /this/i
    > > puts "this"
    > > elseif $val =~ /that/i
    > > puts "that"
    > > elseif $val =~ /the other/i
    > > puts "the other"
    > > end

    >
    > Just remember that even when you spell it right, it's going to drop
    > out of the whole construct just as soon as it fulfills a RegEx. After
    > it prints "this," it stops looking at any of the other "elsif"
    > statements.


    Yes, that was the behaviour I was looking for. I noticed I provided an
    ambiguous example in that in another construct it would possibly have
    matched two different conditions. However, in my actual script I am
    looking for values that are mutually exclusive, so exiting on finding
    a value is fine.

    All the best,

    pt
     
    planetthoughtful, Mar 15, 2007
    #6
  7. planetthoughtful

    John Joyce Guest

    So it doesn't work like the C variety?

    if ( condition )
    statement // maybe done
    else if ( condition )
    statement // or maybe done
    else
    statement // done if the 1st and 2nd are not done


    Honestly, my biggest difficulties so far are that I think of code
    somewhere between C and PHP (I like to call it a nicer C)
    On Mar 15, 2007, at 12:20 PM, Chad Perrin wrote:

    > On Thu, Mar 15, 2007 at 12:12:17PM +0900, Augie De Blieck Jr. wrote:
    >>> if $val =~ /this/i
    >>> puts "this"
    >>> elseif $val =~ /that/i
    >>> puts "that"
    >>> elseif $val =~ /the other/i
    >>> puts "the other"
    >>> end

    >>
    >> Just remember that even when you spell it right, it's going to drop
    >> out of the whole construct just as soon as it fulfills a RegEx.
    >> After
    >> it prints "this," it stops looking at any of the other "elsif"
    >> statements.
    >>
    >> That short circuiting is a feature. =)

    >
    > Indeed -- that's the whole point of using elsif rather than just a
    > list
    > of separate if statements. The if/elsif/else construct is used for
    > mutually exclusive flow control, where only one of several is to be
    > executed. Using a series of if statements considers each in a vacuum,
    > in order, while if/elsif/else considers them all as part of a greater
    > whole.
    >
    > --
    > CCD CopyWrite Chad Perrin [ http://ccd.apotheon.org ]
    > "Real ugliness is not harsh-looking syntax, but having to
    > build programs out of the wrong concepts." - Paul Graham
    >
     
    John Joyce, Mar 15, 2007
    #7
  8. planetthoughtful

    Chad Perrin Guest

    On Thu, Mar 15, 2007 at 02:41:53PM +0900, John Joyce wrote:
    > So it doesn't work like the C variety?
    >
    > if ( condition )
    > statement // maybe done
    > else if ( condition )
    > statement // or maybe done
    > else
    > statement // done if the 1st and 2nd are not done


    Err . . . I'm pretty sure if, elsif, else works exactly like the C if,
    else if, else.

    --
    CCD CopyWrite Chad Perrin [ http://ccd.apotheon.org ]
    "The first rule of magic is simple. Don't waste your time waving your
    hands and hopping when a rock or a club will do." - McCloctnick the Lucid
     
    Chad Perrin, Mar 15, 2007
    #8
  9. planetthoughtful

    Phrogz Guest

    On Mar 14, 5:59 pm, "planetthoughtful" <>
    wrote:
    > I'm a little confused about why the following two pieces of code seem
    > to behave differently:
    >
    > if $val =~ /this/i
    > puts "this"
    > elseif $val =~ /that/i
    > puts "that"
    > elseif $val =~ /the other/i
    > puts "the other"
    > end


    Thought I'd point out that Ruby's case statement is more powerful than
    many other languages' switch statements:

    %w| pthisic thatch mother armadillo |.each{ |val|
    case val
    when /this/i
    puts "'#{val}' contains 'this'"
    when /that/i
    puts "'#{val}' contains 'that'"
    when /other/i
    puts "'#{val}' contains 'other'"
    else
    puts "'#{val}' does not contain 'this', 'that', or 'other'"
    end
    }

    #=> 'pthisic' contains 'this'
    #=> 'thatch' contains 'that'
    #=> 'mother' contains 'other'
    #=> 'armadillo' does not contain 'this', 'that', or 'other'


    Also, note (as seen above) that the regexp you supplied as example
    match substrings even inside words. Just in case you wanted exact case-
    insenstive string matching, perhaps /\Athis\Z/i would be more
    appropriate. If you wanted exact word matching, perhaps /\bthis\b/i
    might be what you want.
     
    Phrogz, Mar 16, 2007
    #9
  10. Hi --

    On 3/15/07, Chad Perrin <> wrote:
    > On Thu, Mar 15, 2007 at 02:41:53PM +0900, John Joyce wrote:
    > > So it doesn't work like the C variety?
    > >
    > > if ( condition )
    > > statement // maybe done
    > > else if ( condition )
    > > statement // or maybe done
    > > else
    > > statement // done if the 1st and 2nd are not done

    >
    > Err . . . I'm pretty sure if, elsif, else works exactly like the C if,
    > else if, else.


    Pretty much, except in Ruby there's no need for a rule to resolve
    if/else ambiguity, because 'end' always makes it clear.


    David

    --
    Q. What is THE Ruby book for Rails developers?
    A. RUBY FOR RAILS by David A. Black (http://www.manning.com/black)
    (See what readers are saying! http://www.rubypal.com/r4rrevs.pdf)
    Q. Where can I get Ruby/Rails on-site training, consulting, coaching?
    A. Ruby Power and Light, LLC (http://www.rubypal.com)
     
    David A. Black, Mar 16, 2007
    #10
  11. planetthoughtful

    Chad Perrin Guest

    On Sat, Mar 17, 2007 at 07:20:42AM +0900, David A. Black wrote:
    > Hi --
    >
    > On 3/15/07, Chad Perrin <> wrote:
    > >On Thu, Mar 15, 2007 at 02:41:53PM +0900, John Joyce wrote:
    > >> So it doesn't work like the C variety?
    > >>
    > >> if ( condition )
    > >> statement // maybe done
    > >> else if ( condition )
    > >> statement // or maybe done
    > >> else
    > >> statement // done if the 1st and 2nd are not done

    > >
    > >Err . . . I'm pretty sure if, elsif, else works exactly like the C if,
    > >else if, else.

    >
    > Pretty much, except in Ruby there's no need for a rule to resolve
    > if/else ambiguity, because 'end' always makes it clear.


    Thanks for adding that. I had forgotten about that, largely because I
    haven't written a line of C/C++ in about five years.

    I imagine there are other differences as well, but for the general case
    I haven't run across any (yet). Still new to Ruby, may just not have
    encountered further differences yet.

    --
    CCD CopyWrite Chad Perrin [ http://ccd.apotheon.org ]
    "Real ugliness is not harsh-looking syntax, but having to
    build programs out of the wrong concepts." - Paul Graham
     
    Chad Perrin, Mar 16, 2007
    #11
    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. Leon

    ElseIF or Case Statement?

    Leon, Dec 16, 2004, in forum: ASP .Net
    Replies:
    5
    Views:
    494
    Dan Bass
    Dec 17, 2004
  2. Id0x
    Replies:
    4
    Views:
    1,221
    Erik Max Francis
    Jul 21, 2003
  3. simonced

    elseif problem

    simonced, Jun 2, 2006, in forum: Ruby
    Replies:
    7
    Views:
    153
    simonced
    Jun 3, 2006
  4. 7stud 7stud

    elseif v. elsif ??

    7stud 7stud, Mar 7, 2007, in forum: Ruby
    Replies:
    83
    Views:
    881
    Samantha
    Mar 13, 2007
  5. fl
    Replies:
    6
    Views:
    388
    HT-Lab
    Feb 28, 2013
Loading...

Share This Page