Object Oriented Programming
Ruby OOP
Very Important concept used in:
- web applications
- ios apps
- android apps
OOP uses objects and their interactions to design and program applications.
Think of trying to organize records. It could be accessing a specific student, book. article etc. All are objects and when you have the ability to specifically limit, restrict, and add to that specific object then it helps to limit bugs within the program.
What do we have?
- A Browser
- A table w/ list of product names, price and availability.
- Products that can be part of a store.
- Product Reviews that have Title of Review and Body of Review.
- Option to submit the review.
- Customers associated with the reviews
- The reviews themselves
If you think of an object in programming you can think of a “thing”.
“Thing”
An object created from a class like a “copy or blueprint” lets you interact with
features.
Features of the “Thing” also known as Attributes of the object that is specified class.
For Example;
Take student “thing” take a copy and that would be the class.
- Take this copy and create an object of an individual student & then work with that students attributes.
- First name
- Last name
- Username
- Password
The details of a specific student are not in the class itself it would be in the DB (database), when the university’s admin tries to work with the details. The system would create an object of that student using the copy, then work with the details and perform whatever function that is needed.
class Student
attributes: First_name. Last_name, Email, Username, Password
How to create a class in Ruby?
I currently use VSCode to do my coding. So, you would want to create the project folder. You would want to create a file like: product.rb, book.rb, student.rb, or whatever you want to call it .rb.
Inside that file, you type the word “class” space then the name of the class Capitalized. Then immediately add an end.
class Productend
Whatever within it would be the products copy or “blueprint”.
In ruby, working with an attribute of a class. It is called an “Instance variable”. Starts with an “@”.
class Product (copy or blueprint)
product_name
product_description
product_size
end #you want to work with an "Instance of that class" so you use the @ symbol.It then looks like this:class Product
@product_name
@product_size
@product_description
end
How do you create an Object of this class?
you can use .new
# Say I want to create a new product called Brightening cleanser.class Product
@product_name
@product_size
@product_description
endbrightening_cleanser = Product.new
In the terminal you can cd into the folder and see the file you listed it in.
for example product.rb
then in your terminal type ruby product.rb
If nothing happens then it’s good b/c that means there are no errors.
Puts
Now you can print out the product.
class Product
@product_name
@product_size
@product_description
endbrightening cleanser = Product.new
puts brightening cleanser
if you go back to terminal and run it again by typing ruby product.rb.
You’ll see that it prints out something.
#<Product:0x0003738eth466sj8> (something similar to this)
#Product — that prints out the product object, product object was created.
Hopefully this helps a little with understanding the beginning of the OOP. Thanks for reading!
Happy Coding!
#CodingEsty