Object in Ruby

Object in Ruby could be any value, even strings, numbers and a class itself. All objects in Ruby,  they have their own state and behavior. An object on the right side of “=” , and usually assign to a variable on the left side of “=”.

my_name = grace

In this case,  “grace” is an object, and it’s also a string.

An object can be number as well, such as

my_age = 28

28 in here is an object assign to the variable “my_age”.

However, we can have ONE string object to Two variable.

your_name = grace

“grace” over here is just ONE string object, but it assign to both variable “my_name” and “your_name” at the same time.

Object can also do things in Ruby. We tell an object to do something by calling a methods on that object. Sometime we refers to sending the methods to the object. Such as

my_name.capitalize

and it would show us the result of “Grace”, or

your_name.length

will show the result of “5”. Another example,

my_age * 2

the result will come out as “56”.

Since we tell the object “grace” to capitalize, both variable “my_name” and “your_name” get change to “Grace”, the same object they share.

You can change your name to another object, and not tie to me anymore. Re-assign another object to “your_name”

your_name = Laura

so “Laura” will be the new object for variable “your_name” currently, but variable “my_name” still has the same object “Grace”.

An object can be class itself, basically an object is created from a class, which a class provides the blueprints for objects. What an object of the class will consist of  and what operations can be performed on such an object, define as a class. The class object has to be capitalize, and it always come after the keyword class and followed by an end at the end. For example,

class Panda

end