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