Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Basic concepts of Ruby

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 369
    Comment on it

    Welcome to Findnerd. Today we are going to discuss basic concepts of Ruby. In Ruby almost every thing is object. Objects deal as fundamental building blocks for Ruby.  Every thing which you manipulate or return after manipulation, is an object. Object is noting but an instance of a class. A class can contain different objects with common properties.

     

    If we talk about the variable in Ruby then we can say variables are not only objects. They can carry values as well as change the value if required. Variable allows easily reference objects in Ruby. By-default value of a variable is undefined.  There are different types of variables available as per their scopes. Please have a look.

     

    Local / Block variables can be written as variable


    Global variables can be written as $variable


    Class variables can be written as @@variable


    Instance variables can be written as @variable

     

    Before defining or assigning the value to a variable we need to assign  a proper name to it. Please have a look.

     

    # well naming for variables
    basic_setting = 12;
    $hub_query = 3;
    
    # incorrect way to write a variable naming
    
    basic-settings = 32;
    basicSettings = 22;

     

     

    You need to put underscore between two different words. It is the correct way to create variable.

     

    In Ruby we use numbers as two different categories. One is integer and other floats. Integer numbers can be positive or negative. There are different functions available which can be used with integers. Please have a look.

     

    .class : It will return integer type. It is Fixnum or Bignum.

     

    .abs : It will return absolute value of the integer

     

    .reverse : It will reverse the integer value.

     

    .next : It will increment the value by one.

     

    irb
    # open the irb
    
    334.class
    // FixNum
    
    4324830478032470324.class
    # BigNum
    -200.abs
    ## return 200
    232.next
    # return 233

     

     

    You can also use the same functions for floating numbers. There are other functions available which can be used with floating numbers. Please have a look.

     

    floor : Useful to round down the number.


    ceil  : Useful to round up the number.


    to_i : Covert the floating point number to integer number.

     

    Strings basically a sequence of characters. We can simply assign a string to a variable and perform different operations on it. Please have a look.

     

    # open irb
    irb
    "Findnerd"
    'Findnerd'
    'I \'m Nerd'

     

    You can create string using single quotes or double quotes. Ruby will return the string in double quotes. We can use the double quotes if we want some extra evolutions like below.

     

    puts "a\tb\nc/td/t"

     

     

    Above code will add the tab and newline in the result. There are different functions available for strings. Please have a look.

     

    .reverse : It will reverse the string.


    .upcase : It will change the string in uppercase letters.


    .downcase: It will change the string in small letters.


    .length : It will return the length of the string.

     

    we can also use multiple function at the same time on the string. Please have a look.

     

    "Findnerd".reverse.upcase
    # return "DRENDNIF"
    "Findnerd".reverse.upcase.length
    # return 8

     

    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: