Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Different Equality Operators in Rails

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 642
    Comment on it

    1) ==

    It is a simple equality operator, that only checks if the value of the left operand is equal to the right operand or not, so returns true or false respectively

    <pre>
    > x = 5
    > y = 5
    > x == y
    => true
    
    > y = 6
    > x == y
    => false
    </pre>

     

    2) ===

    This symbol is of little different use than other languages like Javascript. It is mostly used in when clause of when statement. So this is basically to check whether something comes up in the range or not

    <pre>
    > (5..10) === 7
    => true
    
    > (5..10) === 12
    => false
    </pre>

     

    3) .eql?

    This checks if the receiver and argument have same value and also same datatype.

    <pre>
    ## In case of == 
    > 5 == 5.0
    => true
    
    ## In case of .eql?
    > 5.eql? 5.0
    => false
    </pre>

     

    4) .equal?

    .equal? is used to check whether the object id of the two objects are equal or not. If two objects have same object id, even if two objects are identical, it will return false if there object id is different

    > a = "Hyundai"
    => "Hyundai" 
    > b = "Hyundai"
    => "Hyundai" 
    
    > a.equal? b
    => false
     
    > a == b
    => true
     
    > a.eql? b
    => true 
    

     Hope you liked this blog. Will be back soon with few more things.

     

 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: