Core Ruby
Tags: webdev, programming, ruby, beginners
Published: 2023-08-17 01:37:26

Ruby is an object-oriented programming language, crafted by Yukihiro Matsumoto in the mid-1990s. Ruby is driven by a philosophy that prioritizes developer happiness and productivity. With its clean and intuitive syntax, Ruby enables developers to write code that reads almost like spoken language, making it an ideal choice for both beginners and experienced programmers. Known for its versatility, Ruby finds application in a multitude of domains, from web development and scripting to scientific computing and more. Whether you're a seasoned developer or just starting your coding journey, Ruby's welcoming and intuitive nature makes it an ideal choice for crafting powerful and elegant solutions.

Ruby involves classes & objects; In Ruby, everything is an object.

#Classes, Instances; Methods & Variables:
Ruby utilizes what are called ‘classes’. These classes are responsible for acting as a ‘factory’, providing a ‘blueprint’ to create objects (or instances). When creating a class, methods (like a function) are defined to perform a specific task on any instances (objects belonging to its pertaining class) and are respectively called ‘instance methods’. 
Like instance methods, ‘class methods’ may be declared to provide some functionality to the class as a whole. 

For example: we can create a Car class to create Car instances (objects) with instance variables such as make and model.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/bplklzq4axc0r148gdfx.png)

Let’s declare a class variable @@wheels to equal 4.
What this means is, any car instance created can have their own make and model when instantiated (and arguably, same makes and models if were so the case) but ALL car instances will have the wheels attribute already set to 4.

We can then add these setter and getter methods:
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/zgio1yavlpjhhhp2r82y.png)

Instance variables are variables(or attributes) within an instance that can contain different data values when instantiated. Of course, these values may be accessed and manipulated if need be with setter methods and getter methods. 

There is a useful shortcut for writing cleaner code:


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/mgzr9qvwl4d38e4ty0x1.png)


With this handy macro *attr_accessor*  :make, :model, Ruby automatically generates both getter and setter methods for the make and model instance variables. This simplifies the code and reduces redundancy. You can then use these generated methods to retrieve and update the values of the instance variables. If you only wish to read the data, then you must use *attr_reader* :attribute. The symbol to refer instance variable is @

Class variables, are like instance variables, with the exception that they are variables containing the same value across the whole class. Instance and class variables can be instantiated without a value, and that value can be added or removed at any time from within an individual instance, or the class itself; provided the methods to do so are defined. While that is possible, take caution manipulating class variables in individual instances for that may bear unwanted side effects. These side effects will only happen to parent or sub classes that may be inherited/inheriting.
The symbol to refer class variable is @@




#Active Record
There are powerful libraries that simplify the ruby experience. One such library is called Active Record .  

One can create an instance belonging to a class without having to hard code many of the the methods required to create it. While Active Record does a lot of the heavy lifting for you, it is still most important to know how to do it yourself and understand what processes are being performed under the hood! 

Active Record is compatible with SQLite3, a database engine. Active Record is an Object-Relational Mapping (ORM) layer responsible for managing the interaction between your Ruby code and the database, provides an elegant and intuitive way to interact with your database by representing database tables as Ruby classes and individual table rows as instances of those classes. This abstraction allows you to manipulate database records using familiar Ruby syntax, without having to write raw SQL queries.

Sinatra is a lightweight Ruby web framework.
This is the response cycle between React, Ruby, Active Record, and Sinatra, and how these technologies interact to build a web application:

User Interaction:
The process begins when a user interacts with the frontend, built using React. User interactions might include clicking buttons, submitting forms, or any action that triggers a request for data or changes in the application's state.

Frontend Sends Request:
When a user interacts with the frontend, React sends an HTTP request to the backend server. This request could be sent using a Fetch GET request to retrieve data or a POST/PUT/DELETE request to send data to the backend.

Sinatra Backend Receives Request:
The Sinatra backend receives the incoming HTTP request. Sinatra is a lightweight web framework that simplifies routing and handling requests in a Ruby application. It routes the request to the appropriate route handler based on the URL and HTTP method defined in the application_controller.

Route Handling in Sinatra:
The route handler in Sinatra processes the request. This might involve interacting with a database, performing logic, or fetching data from external APIs. If the request involves working with a database, this is where Active Record comes into play.

Active Record Interacts with the Database:
Active Record, as the ORM library, interacts with the database on behalf of the Sinatra application. It abstracts away the details of the database and provides a more Ruby-like interface to work with the data. Active Record retrieves or modifies the required data from the database using models and associations.

Sinatra Sends Response:
Once Sinatra and Active Record have processed the request and gathered the necessary data, the Sinatra route handler generates an HTTP response. This response contains the data or information that needs to be sent back to the frontend.

Frontend (React) Receives Response:
The React frontend receives the HTTP response from the Sinatra backend. The response often includes JSON data or other relevant information that the frontend can use to update the user interface.

Updating the UI:
Based on the data received in the response, React updates the user interface. This might involve rendering new components, updating state, or modifying the DOM to reflect changes.

User Interaction Continues:
With the updated user interface, the user can continue to interact with the application. This interaction can trigger additional requests to the backend, starting the cycle again.

The response cycle between Ruby-based backends and frontend frameworks like React host dynamic, interactive, and user-friendly web experiences. Ruby's syntax, combined with the structure provided by Sinatra and the data management offered by ActiveRecord, promotes the creation of seamless, well-organized, and maintainable web applications.

#In conclusion
Ruby, along with its powerful tools like ActiveRecord and Sinatra, provides a robust foundation for building web applications that seamlessly integrate with frontend frameworks. ActiveRecord serves as an efficient Object-Relational Mapping (ORM) library, simplifying database interactions and allowing developers to work with data using object-oriented principles. Sinatra, a lightweight Ruby web framework, is great for building RESTful APIs and small to medium-sized applications.