Basics of Object Oriented Programming

- Class is a structure to characterize objects. This structure comes with a set of methods defined under it to operate on the objects.

- Object is an instance of a class.

- Methods are the functions defined to operate on the structure. Only through these functions the structure could be accessed and changed.

- Inheritance allows one class to be derived from another class when the former is a special type of the latter.

- everything is an object including simple scalar quantities, integers

- data representation as objects
example: take filenames in windows; if they are merely filenames, they are data-strings and nothing should happen if you click on them. Instead, the data-strings are encapsulated as an object with associated behaviour - commands and options to open the file. Thus clickable desktop filenames in XP can be considered to be "objects".

Using interactive ruby shell (irb in Linux or fxri in windows)

Number and String Handling

Exercises

	add  2 + 3           >> 2 + 3 (answer is 5)
	add "2" + "3"        >> "2" + "3" (answer is 23)
	multiply 2*3         >> 2 * 3 (answer is 6)
	multiply "2" * 3     >> "2" * 3 (answer is 222)
Handle big numbers? Type and see >> number = 1_000_000_000

Numerical operators

Multiply = * ... 2 * 3
Divide = / ... 3.0/2
Add = + ... 4.0+ 5
Subtract = - ... 6 - 2.2
Reminder = % ... 5 % 2
Power = ** ... 2 ** 9

Variables

A variable is a name associated with a particular object

>> city = "Miami" {variable names must begin with a lowercase}

Once defined, you can handle a variable like other objects.
>> city.downcase (changes the value to "miami")

Shortcuts while handling variables

>> var = 2 { assign a value to var}
>> var +=2 { add 2 to var }
>> var -= 3 { substract 3 from var}
>> var *=6 {multiply var by 6 }
>> var /=2 {divide var by 2 }
>> var **=3 {cube var }
>> var %=4 { var modulo 4 }

String Handling

>>"Hello" + "World"
HelloWorld {leaves no space}
>>"Hello " + "World"
Hello World {includes space}

>>"Ding " * 12
Ding Ding Ding Ding Ding Ding Ding Ding Ding Ding Ding Ding
Also Try and see what happens with this
>> 12 * "Ding "

String Property Handling

>>"hello".capitalize ...... capitalizes the first letter
Hello

>>"Hello".reverse ........reverses the string
olleH

>>"Hello".next ....... alphabetically the next char
Hellp

>>"Hello".upcase ...... upper case
HELLO

>>"HELLO".downcase ...... lowercase
hello

>>"Hello".swapcase ...... exchange case
hELLO

>>"Hello".length ....... try it!

Classes vs Objects

Integer ----- 25
Float ----- 1.732
String ----- Hello

An object is just any piece of data with associated properties.

A class can be thought of as a "group of objects" or a "set".

A method is an operation on an object.

Classes vs Methods

Integer - +, - , / , * , %
Float - +, - , / , * , %
String - capitalize, reverse, upcase

Converting between Classes

String.to_i ........ converts string to integer
String.to_f ......... converts string to float
Float.to_i ......... converts float to integer
Float.to_s .......... converts a float to string
Integer.to_f .......... converts integer to float
Integer.to_s .......... converts integer to strong

Examples
>> 12 * 2
>> 12.to_s * 2

Finding the Class of an object

	>> var ="Miley"  { defining a variable "var" }
	>> var.is_a?(Integer)  { Logical }
	>> var.is_a?(Float)
	>> var.is_a?(String)
	>> 'var'.is_a?(Integer)
	>> 'var'.is_a?(Float)
	>> 'var'.is_a?(String)
	>> var = 12.0
	>> var.is_a?(Integer)
	>> var.is_a?(Float)
	>> var.is_a?(String) 

Go to ruby-page 5 ... Back to ruby-page 3 ... Starting page 1