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
    • 349
    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.

     

    if condition1
    puts "Condition1 fulfilled"
    elsif condition2
    puts "Condition2 fulfilled"
    else
    puts "no condition fulfilled"
    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.

     

    user_status = "valid";
    puts "Welcome to Findnerd" if user_status=="valid"
    # 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.

     

    if !boolean
    puts "unconditional"

     

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

    unless boolean
    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.

     

    case text_value
     when value
     ....
    
     when value
     ...
    
     else
     ...
    
     end

     

     

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

     

    boolean ? code1 : code2
    
    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.

     

    if y
    x = y
    else
    x=3
    end 


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

     

    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.

     

    unless x
    x = 4
    end

     

    You can write the same code with OR_EQUALS as below.

     

    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
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: