hl

Wednesday, 5 April 2017

Ruby Programming Language Tutorials Lec 14

Tutorial 14 : Cont. to strings

Conversion of a string

     If you want to convert a string into integer so you can do is to write the name of the string and then write dot operator and the write to_i

     If you want to convert a string into float values so you can do is to write the name of the string and then write dot operator and the write to_f

     If you want to convert a string into symbols so you can do is to write the name of the string and then write dot operator and the write to_sym

     As if you want it to change it another way you can do is in the table :

 Examples :


Program in RUBY of objects

      
     Okay everything inside the ruby is an object and now we will talk about the objects. We are going to model an object using classes like everyday objects. Every object is going to have some attributes. Which we going to call instance variables as well as keep abilities which we going to call methods. Now let’s say we have a class called animal inside of it you can put an initialize function and this is called any time new animal objects in this you create default values let’s say creating a new animal and end it of course

     I am going to show you how to create setters and getters inside ruby but for now let’s create setter and getter. Let’s set by pass new_name. An instance variable inside the ruby by @ symbol and then name no need to do anything else and then equals operator and then write the name of the variable such as new_name it will assign the value to that. After that we going to put the end




     And then we define a getter by typing def keyword and then write get_name you just put @name it will return that. After that end it. After that you command there

     And another type you can make setter is to just def name = (new_name). Let’s make sure that we are passing name which is numeric. So make a condition of if the string is numeric write if new_name.is_a? (Numeric) puts “Name Can’t Be a Number” something like that. Else and then we going to assign the values that will passed in the name instance variable and then we end the else. And then we end the setter method and finally we going to end the class.

     



      As we create a class we is to create an object. So let’s create a cat name object by typing cat = Animal.new and then we set the name peekaboo to the cat and then we make a getter here by cat.get_name after that we going to print to out by puts cat.name. Let me show you some short cut to set some getter and setter. Within a second write cat.name = “sophie”
And for write puts cat.name

                                                            Output



Ruby Programming Language Tutorials Lec 13

Tutorial 13 : Cont. to strings


Use of CHOP and CHOMP

      To remove the last charecter from the string we use chop by typing string_name and then dot operator and then write chop. your last character is chopped out like if you want only first name in the full name so write full_name.chop the it will remove the last part from the full name 
     And the CHOMP work same as chop works but it delete multiple letters

                             Input                                                     Output








Delete

     There is a keyword in ruby to delete a letter in the whole string. Such as if you want to delete a letter from the string full name you is to type full_name.delete(“a”) it will do delete the letter a from the string. So let’s check it out

Example is below with split

Split

     To split every letter in the string first write name of the string and then write dot operator and the write split it will do split the string but it is not full for every letter pass // in the parenthesis.
     For split the words do the same but pass the / /with space between them by which it will split the words out

                       Input                                                               Output

























NOTE: It will also count white spaces as a letter

Thursday, 30 March 2017

Ruby Programming Language Tutorials Lec 12

Tutorial 12 : Cont. to strings

Equality in Strings 

     For equality in strings we are to simply write in double quote and we write a string and then write . operator and after that write equal and the put a question mark there and then write the equal string which you want to be equal in double quote. Let's check it out

                           Input                                                          Output
And if we write first_name.equal?first_name it will give result true because they are the exactly same objects.

Changing uppercase Lowercase and Swapcase

     To change a string in uppercase type the name of the string and write .upcase it will convert the whole string to the uppercase. Similarly for the lowercase and for the swapcase write downcase and swapcase. So let's check it out

             Input                                                            Output

 

Manipulation With Spaces in a String

If we added some extra spaces on the left side of a string or the right side of a string and it is taking some extra space in our memory so here we can swipe the white spaces by typing string name.lstrip for left removal of the white spaces .rstrip for right removal of blank spaces and .strip for clear all the blank spaces present in the string


Use of suffix and prefix

If you want to use some prefix you can do it with the use of just operator . I am telling you how to do it. It is quite simple by looking at this you will know how to do this.

                   Input                                  Output

Thursday, 23 March 2017

Ruby Programming Language Tutorials Lec 11

Tutorial 11 : Strings

Multi line String

     We make multi line string very easily. Just by typing "multiline_string" and it is end with = and two less than sign and after that type in capital EOM. Now your multiine string is start and inside it you can write very long string and in this we can do interpolation also like we can write # {4 + 5} and also give new line command by using \n. Let's do practically what I said to you. Check it from the below diagrams 

                       Input                                                         Output

Manipulation with strings

     Now I will tell you about the several type of string printing. Such as addition of 2 strings by + symbol and by interpolation and also learn about a new type of keyword include by which we can search a string into another string. This language is also very good for string purposes.

     

In this we are making a variable name First_name and give it a value Derek

and make another variable name last_name and give the value Banas.
In third line we make another variable name full_name and give them the value first_name + last_name. After that make another variable of name middle_name and its value Justin. Than we add all three strings into one string name full_name. At last we search Justin into full_string. and than it will give the result true
     For taking the size of the string just write string_name.size and it will return the size of the string you can print it with the help of print or puts. 
    for the count of vowels we is to simply write the string_name,count("aeiou") it will return the count of the vowels present in the string . 
     For the count of consonants  we is to simply write string_name.count("^aeiou"). It will return the count of the consonants present in the string.
     For the string string start with in the "Banas" Just type string_name.start_with?("Banas")
 will return in true or false.
     For the indexing of the string just write string_name.index

                       Input                                                                  Output















Tuesday, 21 March 2017

Ruby Programming Language Tutorials Lec 10

Tutorial 10 : Exceptions

Make your exceptions

      Here we are creating our new variable name age whose value is give 12 and then we define a function of name “change_age” in which we passing the value of age raise argument error the value of age is greater than zero than until it will print “Enter Positive number” Then we will end the function.
     

And then we create a loop which will take the values of the age. If it’s negative value it will go under the argument error will print “That is an impossible age”. And then we end the loop of the begin then  program will execute and givers the result “That is an impossible age”.

                             Input                                                           Output



Difference between double quotes and single quote in the string function in puts

      Alright let’s start by typing an line of the puts in First one we write in double quotes write “Add Them #{4+5}   \n \n” . and in the Second line we will write puts ‘Add Them #{4+5}   \n \n’. and the execute it so lets see what will happen.

                             Input                                                           Output




Monday, 20 March 2017

Ruby Programming Language Tutorials Lec 09

Tutorial 9 : Functions

Definition of the functions

To define functions we is to type def to define function and the give a space and write the name of the functions then in braces pass the parameters if any. If no so give only open and close braces and then if you want to return the value or not. If you want to return a value so type return and then type to which is to be return if it is string type .to_s and if it is integer so type .to_i
  For example of a function of addition of two numbers we can write it like this

                        Input                                             Output

In this the function is called by value 3 and 4 by which it return and give the output 7

Change the value under a function

This part of tutorial is to understand that when the value of an integer get changes and when it takes new values. So check this program and try to understand.

       Input                                                           Output





In first line we give the value of x to 1 and after that we create function of change_x and pass the value of x and third line and we change thw value of x from 1 to 4. and in 5th line we end the function and in 6th line we call the function by the name and in 7th line we print the value of x

     Any value change inside the function will remain in the function it will not affect the outside. It is so important when you work on the functions.

Exceptions

We can catch exception by both catch and rescue 

         Input                              Output


     In this program we are catching the exceptions by rescue function here we are saying that rescue all the conditions when user want to divide his number by zero.

Saturday, 18 March 2017

Ruby Programming Language Tutorials Lec 08

Tutorial 8 : Cont. Looping

For Loop

     For the for loop creation I am not going to take your much time. watch in the picture and i am sure that you will understand it .



                           Input                                                 Output

As you can see there we take an variable of name numbers in which we store 5 variables 1,2,3,4,5 and after that in next line make another variable of name number in the for loop condition it is for taking temporary values of numbers in number and print it in next line we print number. Then we end the loop 

     Oh! sorry I forgot to tell you about the print statement so let's make difference between puts and print statement 

                    Input                   Output





      The puts give newline command itself but in print statement all will print in the same line.

      And in these pictures you will also know that how to print the value of any variables.

Another example of loop

     The loops are not only work with numbers they work with records too. For this let's take an example make a record of name groceries and give them values and fill every value of groceries in to food and print it as we can see in pictures

                            Input                                                                          Ouput

 

                 


Range in loops

     We can use ranges in the loops also by giving the lower limit and separate it by two dots

       Input                                                              Output





Here the first # will work as # and the other # will work for print the value of i.

Ruby Programming Language Tutorials Lec 07

Tutorial 7 : Looping

Loop do

      As you all know that looping is the most important part of programming. For the loop creation we is to simply write loop and followed by do. After that we is to create increment or decrements conditions for the loop. For ex. x += 1 (is a short cut of x = x + 1) after that we is to use the loop and then the breaking condition for the end of the loop. One thing most important in this  is never forget to end your loop if loop is there end it otherwise it will continuously work on your whole program. So let's check it out .

Input                                                                    Output


In this program we take an variable x and give it value 1. Then we start our loop by writing "loop do" and then increment by 1.  After that we create condition for every value of x is even. Then we printing value of x. After that break statement for stop the loop and in next line end is for ending the loop and the output will be 2 4 6 8 10.

Loop While

    For the while loop creation we is to simply write while. After that we is to create increment or decrements conditions for the loop. For ex. y += 1 (or we can write y++) no breaking condition is use for the ending of the loop. Just end it with end keyword and get the profit of the loop. So let's check it out .

Input                                                                    Output



In this program we take an variable y and give it value 1. Then we start our loop by writing "while" and in this loop we give condition with the while keyword  then increment by 1.  After that we create condition for every value of y is even. Then we printing value of y. After that we print the value of y and in next line end is for ending the loop and the output will be 2 4 6 8 10.

Until Loop

     For the until loop creation we is to simply write until. After that we is to create looping condition and increment or decrements conditions for the loop. For ex. a += 1 (or we can write a = +1 ) no breaking condition is use for the ending of the loop. Just end it with end keyword and get the profit of the loop. So let's check it out .

Input                                                                    Output


In this program we take an variable a and give it value 1. Then we start our loop by writing "until" and in this loop we give condition with the while keyword  then increment by 1.  After that we create condition for every value of a is even. Then we printing value of y. After that we print the value of a and in next line end is for ending the loop and the output will be 2 4 6 8 10.

Friday, 17 March 2017

Ruby Programming Language Tutorials Lec 06

Tutorial 6

Comparison Operators

     Alright let us check how the comparison operators work as we take a variable age whose value is 12 and passing and or not true and false operators let’s check it out


                                          Input                                                           Output


Another comparison operators

     So let’s check < = > operator  it compares the values of the left hand side value to the right hand side value if the left is greater so it will give the result 1 if both are equal so it return the value 0 and if the right value is greater so it will return -1 so let’s check by an example

                             Input                                                           Output


Unless

     Unless is another comparison operator if compare age of any student by 4 if greater go to school else stay home. It is also like if else statement so let’s check it out
       Input                                Output







Case – When Statement

     When - Case statement in ruby is just like switch case statement in C .As there is case in switch case statement in when case statement there when. So let’s check it out by making a simple program

                         Input                                                                    Output                                               

* If we don’t write anything or write anything which is not in when statement then else statement will run and give result Hello.

 * And if we match any matching in the when statement then it will return the respective result.