Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Discussion on control structures in Ruby

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 357
    Comment on it

    Welcome to Findnerd. Today we are going to discuss control structures in Ruby. If we talk about the control structures then you have already use or study in other programming language like C, C++, Java etc. We do not want to repeat the same things here but we are going to explain how we can use control structures in Ruby. Please have a look.

     

    1. if condition1
    2. puts "Condition1 fulfilled"
    3. elsif condition2
    4. puts "Condition2 fulfilled"
    5. else
    6. puts "no condition fulfilled"
    7. end

     

     

    In above code we are checking two different conditions using if , elsif as well as else statements. If first condition satisfies then output will be "Condition1 fulfilled" and if second condition satisfies then output will be "Condition2 fulfilled" otherwise else portion will be executed. You can also write the if statement in different way. Please have a look.

     

    1. user_status = "valid";
    2. puts "Welcome to Findnerd" if user_status=="valid"
    3. # return Welcome to Findnerd

     

     

    If we talk about the ruby then there are different statements or operators available. Please have a look.

     

    unless : It is opposite to if statement but works as the same way. Please have a look.

     

    1. if !boolean
    2. puts "unconditional"

     

    We can write the same code with unless statement. Please have a look.

    1. unless boolean
    2. puts "Demo for unless"

     

     

    Case : This type of structure in other programming language is known as switch statement but here we call it as case operator. It works according to mention cases instead of multiple if statements. You can use this case operator like below.

     

    1. case text_value
    2. when value
    3. ....
    4.  
    5. when value
    6. ...
    7.  
    8. else
    9. ...
    10.  
    11. end

     

     

    Ternary Operator:  We have learned this concept in other programming languages but in ruby we use it as follows.

     

    1. boolean ? code1 : code2
    2.  
    3. puts x==2 ? "print double" : "print single"

     

    Finally this operator makes your code shorter and save your time.

     

    OR operator:  It works as the same way as if statement works. Please have a look.

     

    1. if y
    2. x = y
    3. else
    4. x=3
    5. end


    Above code with || operator can be written as below.

     

    1. x = y || 3

     

    If y does not exist then 3 will assign to x.

     

    OR-EQUALS : This works as the same as unless statement. Please have a look.

     

    1. unless x
    2. x = 4
    3. end

     

    You can write the same code with OR_EQUALS as below.

     

    1. x ||= 4

     

    If x does not exist then it will assign the 4 to x.

     

    Thank you for being with us!

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Reset Password
Fill out the form below and reset your password: