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
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