hl

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.



Sunday, 12 March 2017

Ruby Programming Language Tutorials Lec 05

Tutorial 5

Load code form other file

     In ruby to load code from other file we just is to type load and then write name of the file then it will load commands from the file .The extension of the file should be .rb . For example - we crete a file of  name rubyOrNotRuby.rb and in the file we write puts "Hello From rubyOrNotRuby2" and save it and execute it it will print Hello From rubyOrNotRuby2

In file rubyOrNotRuby2.rb                         Output        rubyOrNotRuby.rb             




Operators

   
     Let's study about some logical operators. In ruby there are 3 logical operators and or not

     And there is also some comparison operators like == != < > <= >=


Simple program on condition

     So let's make a program of checking age of a child if it is between 5 and 6 .He should be in kindergarten . If the age is between 7 and 13. He is in middle School.else .Stay at home so let's code it.
1. First of all make a age variable and give the the value 13
2. Make a condition if(age>=5)&&(age<=6)
3. Write you're in kindergarten puts "you are in kindergarten"
4. Make another condition elseif (age>=7)&&(age<=13)
5. Write you're in Middle school puts "you're in Middle school "
6. Write Yeah puts "Yeah"
7. else
8. Write Stay Home puts "Stay Home"
9. end
end is must in if condition

                     Input                                                                 Output