Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Uses of booleans, ranges and constants in Ruby

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 417
    Comment on it

    Welcome to Findnerd. Today we are going to discuss booleans, ranges and constants in Ruby. Boolean is nothing but true or false. We use boolean for comparison. There are different type of comparison  and logical operators. Please have a look.

     

    A) Equal(==) : This operator is useful  to comparer two values.

     

    x = 2
    
    x==2
    # return true
    
    x==4
    # return false
    
    true.class
    
    # return TrueClass
    
    false.class
    # return FalseClass

     

    It means true and false both are the objects of TrueClass and FalseClass respectively.

     

    B) Not Equal (!=) :  This is common operator to check that value is not equal to specific value.

     

    x!=2
    return false
    
    x!=3
    return false
    

     

     

    C) Greater than (>) : This operator is useful to check that value is greater than or not.

     

    x>3
    # return false
    
    x>1
    # return true

     

     

    D) Less than (<) :  This operator is useful to check that value is less than or not.

     

    x<1
    # return false
    
    x<3
    # return true

     

     

    E) Less than or equalto (<=) :  This operator is useful to check that value is less than or equal.

     

    x<=1
    # return false
    
    x<=2
    # return true

     

     

    F) Greater than or equalto (>=) : This operator is useful to check that value is greater than or equal.

     

    x>=3
    # return false
    
    x>=2
    # return true

     

     

    G) Not (!) :  It is logical operator that is useful to check variable has any value or not.

     

    !x
    # return false
    
    !y
    # return error undefined variable
    
    y = null
    
    !y
    # return true

     

     

    H) AND (&&) :  This operator checks all cases combine with AND operator.

     

    x<3 && x>1
     # return true because both cases are satisfied

     

     

    I) OR (||) :  This operator checks only one case to satisfy the condition.

     

    x>3 || x>1
    # return true because only need to satisfy one case.

     

     

    There are different method available in Ruby which can check the value. Please have a look.

     

    1) nil? : You can check that value is null or not.

     

    x.nil?
    # return false 
    y.nil?
    # return true

     

     

    2) between?(arg1,arg2) : Useful to check the value between mentioned range.

     

    x.between?(1,2)
    # return true
    

     

     

    3) empty? : Useful to check that value is empty or not.

     

    arr = [1,3,4]
    
    arr.empty?
    #return false
    
    other_arr = []
    other_arr.empty?
    # return true

     

     

    4) include?(arg) : Useful to check the value is included in array/hash or not.


     

    arr.include?(3)
    
    # return true

     

     

    5) has_key?(arg) : Useful to check hash is included the passed key or not.

     

    hash = { 'main' => 'Findnerd' , 'primary' => 'evon'};
    hash.has_key?('main')
    # return true
    hash.has_key?(:main)
    # return false

     

     

    6) has_value?(arg): Useful to check whether mentioned value is exist in hash or not.

     

    hash.has_value?('evon')
    # return true

     

    Now we are going discuss ranges. Range is nothing but a series which mention the beginning as well as end. There are two type of ranges. One is inclusive and other is exclusive range.

     

    Inclusive range : [1..10]
    [1,2,3,4,5,6,7,8,9,10]
    
    Exclusive range [1...10]
    [1,2,3,4,5,6,7,8,9]

     

     

    You can see in above code we can create inclusive range with double dots which contains end number/character as well. Exclusive range can be created with triple dots which does not contain end number/character. Generally we use inclusive ranges.

     

    x= 1..10
    x.class
    # return Range
    
    1..20.class
    # return error of bad value
    
    (1..20).class
    # return Range
    
    x.begin
    # return 1
    
    x.end
    # return 10
    
    x.first
    # return 1
    
    x.last
    # return 10
    
    x.include?(10)
    return true
    
    y = 1...10
    
    y.include?(10)
    # return false
    
    [*x]
    # return [1,2,3,4,5,6,7,8,9,10]
    
    [*y]
    # return [1,2,3,4,5,6,7,8,9]
    
    alphabet = 'r' .. 'y'
    aplhabet.include?('t')
    # return true
    alphabet.include?('e')
    # return false

     

     

     

    Now we will start the discussion on constants. Constants are similar to variables but variables can change their values as per the requirement. Constants will change the value but it will return the warning that constant already initialized by other value. We create constant with block letters but if we make only first letter capital then it will be also used as constant. Please have a look.

     

    MASTER = 'Findnerd'
    Docx = 'Evon'  
    Docx = 'Doon'
    return error already initialize other value

     

     

    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: