<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: sawincp</title>
    <description>The latest articles on DEV Community by sawincp (@sawincp).</description>
    <link>https://dev.to/sawincp</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F1010175%2F7c758c8d-875f-4fad-92dc-d9b63f7bf889.png</url>
      <title>DEV Community: sawincp</title>
      <link>https://dev.to/sawincp</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/sawincp"/>
    <language>en</language>
    <item>
      <title>Taming the State Beast: Redux vs. Recoil in React</title>
      <dc:creator>sawincp</dc:creator>
      <pubDate>Wed, 31 Jan 2024 23:55:20 +0000</pubDate>
      <link>https://dev.to/sawincp/taming-the-state-beast-redux-vs-recoil-in-react-2d35</link>
      <guid>https://dev.to/sawincp/taming-the-state-beast-redux-vs-recoil-in-react-2d35</guid>
      <description>&lt;p&gt;React's beauty lies in its component-driven nature, but managing state across those components can become messy. That's where state management libraries like Redux and Recoil come in, wrangling your app's data into a smooth, predictable flow. But which one deserves a place in your toolkit? Let's break down their key differences:&lt;/p&gt;

&lt;h2&gt;
  
  
  Architecture
&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;Redux: Think of Redux as a central bank for your app's state. Everything goes in and out through a single source of truth, the store. Actions, sent by components, trigger reducers that update the store, and changes automatically ripple through the app.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Recoil: Imagine a network of smaller vaults, called atoms, scattered across your components. They each hold independent pieces of state, connected by selectors that can combine or transform data between them. Updates happen locally, with only affected components notified.&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;h3&gt;
  
  
  Learning Curve
&lt;/h3&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;Redux: Buckle up for a steeper climb. Redux has a well-defined structure and requires more boilerplate code, but its predictability and extensive community support make it a reliable choice for complex projects.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Recoil: Take a gentler slope. Recoil boasts a simpler API and less code, making it easier to jump in. However, its decentralized nature can feel less structured and lacks the same level of community resources as its established counterpart.&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;h3&gt;
  
  
  How it works: Redux
&lt;/h3&gt;

&lt;p&gt;At its core, Redux allows us to create a single source of truth for our application. Think of this as a vault which holds the entire application's state which is called a store. &lt;/p&gt;

&lt;p&gt;To set up a store you have to set up a pure function called a Reducer that takes in the current state and an action that returns the updated state.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function counterReducer(state = 0, action) {
  switch (action.type) {
    case "INCREMENT":
      return state + 1;
    case "DECREMENT":
      return state - 1;
    default:
      return state;
  }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;To store your initial values and you need to import the &lt;em&gt;createStore&lt;/em&gt; function from the Redux library.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import { createStore } from 'redux';

const initialState = {
  counter: 0,
};
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;From here you can create your store (vault) with your reducer and initial state as arguments...&lt;/p&gt;

&lt;p&gt;&lt;code&gt;const store = createStore(counterReducer, initialState);&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;This will create the store object that will hold your app's state and manage its updates. &lt;/p&gt;

&lt;h4&gt;
  
  
  How to use Store from Redux
&lt;/h4&gt;

&lt;p&gt;In order to use your newly created store you first need to wrap your root component with the &lt;em&gt;Provider&lt;/em&gt; component and pass the store as a prop. This allows the store to be accessible to all components in your app.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import React from 'react';
import { Provider } from 'react-redux';
import store from './store'; // Import your created store

function App() {
  return (
    &amp;lt;Provider store={store}&amp;gt;
      {/* Your app components here */}
    &amp;lt;/Provider&amp;gt;
  );
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;From here we can connect two hooks to the store in our components. &lt;/p&gt;

&lt;p&gt;1) Use the &lt;em&gt;useSelector&lt;/em&gt; hook to read data from the store into your component.&lt;/p&gt;

&lt;p&gt;2) Use the useDispatch hook to dispatch actions that trigger state updates in your components.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import { useSelector, useDispatch } from 'react-redux';

function CounterComponent() {
  const count = useSelector((state) =&amp;gt; state.counter);
  const dispatch = useDispatch();

  const handleIncrement = () =&amp;gt; {
    dispatch({ type: 'INCREMENT' });
  };

  return (
    &amp;lt;div&amp;gt;
      Count: {count}
      &amp;lt;button onClick={handleIncrement}&amp;gt;Increment&amp;lt;/button&amp;gt;
    &amp;lt;/div&amp;gt;
  );
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In our example our &lt;em&gt;useSelector&lt;/em&gt; retrieves the data from our Redux store by accessing the counter value and storing it in the count variable. It allows the component to display the current count and react to changes in the store. &lt;/p&gt;

&lt;p&gt;&lt;em&gt;useDispatch&lt;/em&gt; will dispatch our action to trigger state updates in the store. The &lt;em&gt;useDispatch&lt;/em&gt; hook returns a function called &lt;em&gt;dispatch&lt;/em&gt; that's used to send actions to the Redux store. When our &lt;em&gt;handleIncrement&lt;/em&gt; function is called, it dispatches an action with the type &lt;em&gt;"INCREMENT"&lt;/em&gt; to our Redux store where our Reducer receives that action and performs the necessary logic and stores the new state value in our counter variable. &lt;/p&gt;

&lt;p&gt;PHEWWWWW.... that's a lot to take in. Let's take a look at our other example. &lt;/p&gt;

&lt;h3&gt;
  
  
  How it works: Recoil
&lt;/h3&gt;

&lt;p&gt;One of the biggest differences between Recoil and Redux is how they approach state. &lt;/p&gt;

&lt;p&gt;Redux uses a single store to hold the entire app's state where components read and update the store through actions and reducers (Centralized). &lt;/p&gt;

&lt;p&gt;Recoil uses atoms scattered across components, each holding a specific piece of state where components can directly access and update these atoms (Decentralized).&lt;/p&gt;

&lt;p&gt;To use Recoil we first have to import and define an atom from our Recoil library.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import { atom } from 'recoil';

const counterState = atom({
  key: 'counter',
  default: 0,
});
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Similar to Redux, we then have to make Recoil available throughout our app by wrapping our root component with &lt;em&gt;RecoilRoot&lt;/em&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import React from 'react';
import { RecoilRoot } from 'recoil';

function App() {
  return (
    &amp;lt;RecoilRoot&amp;gt;
      {/* Your app components here */}
    &amp;lt;/RecoilRoot&amp;gt;
  );
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;From here we can use Recoil hooks just like our useState hooks from our React library. We use &lt;em&gt;useRecoilState&lt;/em&gt; in our components to read and update our state in the component.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import { useRecoilState } from 'recoil';

function CounterComponent() {
  const [count, setCount] = useRecoilState(counterState);

  const handleIncrement = () =&amp;gt; {
    setCount(count + 1);
  };

  return (
    &amp;lt;div&amp;gt;
      Count: {count}
      &amp;lt;button onClick={handleIncrement}&amp;gt;Increment&amp;lt;/button&amp;gt;
    &amp;lt;/div&amp;gt;
  );
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Like our useState hook, when a user clicks on our button and triggers our &lt;em&gt;handleIncrement&lt;/em&gt; function, the function process the request by updating the &lt;em&gt;setState&lt;/em&gt; variable which directly updates the value of our &lt;em&gt;counterState&lt;/em&gt; atom. &lt;/p&gt;

&lt;p&gt;Seems a little bit easier eh?&lt;/p&gt;

&lt;h3&gt;
  
  
  Conclusion
&lt;/h3&gt;

&lt;p&gt;Both of these state management libraries are powerful tools to help tame state in your application. While Redux has a predictable data flow and a centralized store, it also comes with a steeper learning curve and less flexible architecture. Recoil on the other hand provides a simple API with flexible and decentralized state management but is considered less mature with a smaller community than Redux. &lt;/p&gt;

&lt;p&gt;Ultimately, the best choice for your project depends on your project needs and preferences: &lt;/p&gt;

&lt;p&gt;Choose Redux if you have a large and complex application requiring predictable state management, strong community, and established best practices &lt;/p&gt;

&lt;p&gt;OR&lt;/p&gt;

&lt;p&gt;Choose Recoil if you prefer a simpler API, have smaller projects, and prioritize potential performance benefits. &lt;/p&gt;

&lt;p&gt;Choose the tool that's right for you! &lt;/p&gt;

</description>
      <category>beginners</category>
      <category>programming</category>
      <category>react</category>
      <category>state</category>
    </item>
    <item>
      <title>THE MVC</title>
      <dc:creator>sawincp</dc:creator>
      <pubDate>Thu, 21 Dec 2023 17:57:30 +0000</pubDate>
      <link>https://dev.to/sawincp/the-mvc-3c2b</link>
      <guid>https://dev.to/sawincp/the-mvc-3c2b</guid>
      <description>&lt;p&gt;The Model-View-Controller (MVC) pattern is a commonly used design pattern in web development, especially in Ruby on Rails applications. This design pattern plays a crucial role in organizing and separating various aspects of an application, making it easier to maintain and scale. In today's discussion, we will delve into each of these components, exploring their roles and responsibilities within the MVC architecture.&lt;/p&gt;

&lt;h1&gt;
  
  
  Model (M)
&lt;/h1&gt;

&lt;p&gt;The Model component of the MVC represents the data and business logic of the application, with its main purpose being to interact with a database by retrieving and storing data. In Rails, models are created using Ruby classes that inherit from ActiveRecord::Base, with each model corresponding to a database table. By inheriting from ActiveRecord::Base, we can create Active Record Associations to accurately describe the relationships between our models (we'll discuss this in more detail shortly). Additionally, the model component is responsible for performing validations on our models to maintain data integrity for our application.&lt;/p&gt;

&lt;h2&gt;
  
  
  Foreign Keys
&lt;/h2&gt;

&lt;p&gt;Before we can delve into how to incorporate Active Record Associations into our models, it's important to briefly discuss Foreign Keys and how they relate within our database. Foreign Keys are columns in our database that reference the primary key of another table (or another model). When using Active Record, we name the column in the model we are referencing after the name of that model, followed by "_id." This establishes a relationship between our models.&lt;/p&gt;

&lt;h2&gt;
  
  
  Active Record Associations
&lt;/h2&gt;

&lt;p&gt;To define a relationship between one or more models we can use Active Record Associations:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;One-To-Many&lt;/li&gt;
&lt;li&gt;One-To-One&lt;/li&gt;
&lt;li&gt;Many-To-Many&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  One-To-Many Relationship
&lt;/h3&gt;

&lt;p&gt;This is the most common relationship when working with models. To establish this relationship, Active Record provides us with the &lt;em&gt;has_many&lt;/em&gt; and &lt;em&gt;belongs_to&lt;/em&gt; macros. These macros allow us to create instance methods that enable us to access data across models in a one-to-many relationship.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;class Recipe &amp;lt; ApplicationRecord
    belongs_to :user
end

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;From the code snippet above, each Recipe &lt;em&gt;belongs_to&lt;/em&gt; one User. This gives us access to a user method in our Recipe class so that we can now retrieve the User object attached to a Recipe. &lt;/p&gt;

&lt;p&gt;Going in the opposite direction, each User may have none, one, or many Recipes. To incorporate this we add the &lt;em&gt;has_many&lt;/em&gt; macro to our User model.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;class User &amp;lt; ApplicationRecord
    has_many :recipes
end
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  One-To-One Relationship
&lt;/h3&gt;

&lt;p&gt;A one-to-one relationship is typically the least common type of relationship you will encounter. In this type of relationship, two models are associated with each other in such a way that they have a unique and exclusive connection. For instance, a user may have only one associated profile.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;class User &amp;lt; ApplicationRecord
    has_many :recipes

    has_one :profile
end
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;class Profile &amp;lt; ApplicationRecord
    belongs_to :user
end
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If you're not sure which model should be declared with which macro, it's usually a safe bet to put &lt;em&gt;belongs_to&lt;/em&gt; on the model that has the foreign key column in its database table.&lt;/p&gt;

&lt;h3&gt;
  
  
  Many-To-Many Relationships
&lt;/h3&gt;

&lt;p&gt;A many-to-many relationship represents an association between two models where each record in one model can be associated with multiple records in another model, and vice versa. This connection is established through a join table that links the two models. You might wonder how to create this join table. It's straightforward: you create a new model (database table) specifically designed to facilitate the connection between the two associated models.&lt;/p&gt;

&lt;p&gt;For example, how are we able to explain that a taxi can have many passengers and a passenger can have many taxis? In order to tie these two models together, we need to create a third model (call it rides). &lt;/p&gt;

&lt;p&gt;If we think about it, a Ride &lt;em&gt;belongs_to&lt;/em&gt; a taxi but it also &lt;em&gt;belongs_to&lt;/em&gt; a passenger.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;class Ride &amp;lt; ApplicationRecord
  belongs_to :taxi
  belongs_to :passenger
end
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;And a taxi &lt;em&gt;has_many&lt;/em&gt; passengers &lt;em&gt;through&lt;/em&gt; rides&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;class Taxi &amp;lt; ApplicationRecord
  has_many :rides
  has_many :passengers, through: :rides
end
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;And vice versa for passengers. A passenger &lt;em&gt;has_many&lt;/em&gt; taxis &lt;em&gt;through&lt;/em&gt; rides&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;class Passenger &amp;lt; ApplicationRecord
  has_many :rides
  has_many :taxis, through: :rides
end
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The &lt;em&gt;has_many :through&lt;/em&gt; syntax tells Active Record to associate a Taxi to a Passengers through the Rides model. By using kind of association we are able to associate multiple taxis to multiple passengers and vice-versa. &lt;/p&gt;

&lt;h2&gt;
  
  
  Validations
&lt;/h2&gt;

&lt;p&gt;Validations are special method calls placed at the top of model class definitions, and they are used to prevent invalid data from being saved to the database. These validations ensure that the data stored in the database adheres to specific criteria or constraints before it is saved. They play a crucial role in maintaining data integrity and consistency within the application. &lt;/p&gt;

&lt;p&gt;To add validations to a model, you typically define them within the model class itself:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;class User &amp;lt; ApplicationRecord
    validates :username, presence: true, uniqueness: true
end
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In this example, we are validating that a &lt;strong&gt;User&lt;/strong&gt; record must have a username which must be unique. &lt;/p&gt;

&lt;p&gt;These validations are triggered automatically when a record is being saved to the database using methods like "save" or "create". If the data doesn't meet these specific criteria, the record won't be saved and errors will be added to the object that we can use to show to our users. &lt;/p&gt;

&lt;h1&gt;
  
  
  View (V)
&lt;/h1&gt;

&lt;p&gt;In a Rails application, the view layer should contain the least amount of logic of any layer in the MVC. The role of the view is to simply render whatever it is sent from the controller. &lt;/p&gt;

&lt;h1&gt;
  
  
  Controller (C)
&lt;/h1&gt;

&lt;p&gt;The Controller serves as an intermediary between the Model and View. Its primary function is to receive requests from the browser, process them, interact with the Model to retrieve or update data, and then choose the appropriate View to render the response. To facilitate this connectivity, every route defined in the routes file of our application should correspond to a method in our controller. When a request is received, Rails uses the routes file to determine which controller method to execute.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Rails.application.routes.draw do
  post "/signup", to: "users#create"
  get "/me", to: "users#show"
  post "/login", to: "sessions#create"
  delete "/logout", to: "sessions#destroy"
  get "/recipes", to: "recipes#index"
  post "/recipes", to: "recipes#create"
end
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In our Routes file listed above, you notice we have many different routes defined in which our user will directed to based off the corresponding HTTP request. With a matching request, the associated controller and controller action will fire. For instance when a user's request matches our &lt;/p&gt;

&lt;p&gt;&lt;code&gt;post "/signup"&lt;/code&gt; route, the User controller will fire along with the create action&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;class UsersController &amp;lt; ApplicationController

  def create
    user = User.create!(user_params)
    session[:user_id]= user.id
    render json: user, status: :created
  end

  private

  def user_params
    params.permit(:username, :password, :password_confirmation, :image_url, :bio)
  end

end
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In this create action in our User controller, we create a new user, create a session and add the user ID to the session hash and returns back to the new User object along with a HTTP status code of created. &lt;/p&gt;

&lt;p&gt;With all of this in mind here is the high-level flow of how the MVC pattern works in a Rails application:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;A user makes a request by navigating to a URL in their browser.&lt;/li&gt;
&lt;li&gt;The Rails router maps the URL to a specific controller action.&lt;/li&gt;
&lt;li&gt;The controller action processes the request, interacts with the model to fetch or modify data, and sets up any necessary variables for the view.&lt;/li&gt;
&lt;li&gt;The controller then renders the appropriate view, passing along the data.&lt;/li&gt;
&lt;li&gt;The view generates HTML or other content based on the data and sends it as a response to the user's browser.&lt;/li&gt;
&lt;/ol&gt;

</description>
      <category>rails</category>
      <category>beginners</category>
      <category>programming</category>
    </item>
    <item>
      <title>Object Oriented Programming- Self</title>
      <dc:creator>sawincp</dc:creator>
      <pubDate>Thu, 27 Jul 2023 20:21:15 +0000</pubDate>
      <link>https://dev.to/sawincp/object-oriented-programming-self-1jp1</link>
      <guid>https://dev.to/sawincp/object-oriented-programming-self-1jp1</guid>
      <description>&lt;p&gt;When it comes to object-oriented programming, the concept of "self" can be quite intimidating, especially for those who are new to the programming world. For today's discussion, I will try to explain the concept of "self" with respect to the Ruby programming language and how it works.&lt;/p&gt;

&lt;h2&gt;
  
  
  Introduction to Self
&lt;/h2&gt;

&lt;p&gt;One could think of &lt;strong&gt;self&lt;/strong&gt; as having "self-awareness" when it comes to what code should be executed at any given time. When we create a new instance of a class, that instance is considered to be an &lt;strong&gt;object&lt;/strong&gt; of that class that contains data and behaviors for that object.&lt;/p&gt;

&lt;p&gt;For example, if we have a Student class defined as:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;class Student

   attr_accessor :name

   def initialize(name)
     @name = name
   end

   def intro
    "Hi my name is #{name}"
   end
end
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;And then we create our new Student instance:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;tim = Student.new("Tim")
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We can access Tim's name by:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;tim.name
# =&amp;gt; "Tim"
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Or we can let Tim introduce himself:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;tim.intro
# =&amp;gt; "Hi my name is Tim"
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Using &lt;strong&gt;Self&lt;/strong&gt; in an Instance Method
&lt;/h2&gt;

&lt;p&gt;So... does Tim know that he is a student? This is where the concept of "self-awareness" comes into play. Since we created Tim as an instance of Student, is there anyway that Tim will know that he is a Student? One way we can find out is to ask him!&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;class Student
   def show_self
     puts self
   end
end
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;tim = Student.new
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;tim.show_self
# &amp;gt; Student: 0x00007f81b3924bb8&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;What is the keyword &lt;strong&gt;self&lt;/strong&gt; doing here? Self is referring to the &lt;strong&gt;instance&lt;/strong&gt; that the '&lt;strong&gt;show_self&lt;/strong&gt;' method is being called on, which is the Student class. Better yet, Tim is asking "What am I?" In which the answer is a Student instance.&lt;/p&gt;

&lt;h2&gt;
  
  
  Expanding our Code
&lt;/h2&gt;

&lt;p&gt;How do we add in Tim's teacher? Let's see how we can incorporate that into our code.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;class Student

   attr_accessor :name, :teacher

   def initialize(name)
     @name = name
   end

   def intro
    "Hi my name is #{name}"
   end
end

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Since we incorporated the teacher attribute in our attr_accessor (getter and setter method macro) we can set Tim's teacher by:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;tim.teacher = "Professor Black"
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;And then call Tim's teacher:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;tim.teacher
# =&amp;gt; "Professor Black"
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This is great! But what if Tim switches schools or gets a new teacher halfway through the year? How will our Student class be able to handle this?&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;def new_school(student, new_teacher)
   student.teacher = new_teacher
end
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here we have a method that takes in two arguments, an instance of Student(Tim) and the new teacher's name. From here we can have Tim update his new teacher:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;new_school(tim, "Professor McGonagall")

tim.teacher 
# =&amp;gt; "Professor McGonagall"
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This process will work; however, with the use of &lt;strong&gt;self&lt;/strong&gt; we can make this process a little cleaner.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;class Student

   attr_accessor :name, :teacher

   def initialize(name)
     @name = name
   end

   def intro
    "Hi my name is #{name}"
   end

  def new_school(new_teacher)
   self.teacher = new_teacher
  end
end
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here, the use of &lt;strong&gt;self&lt;/strong&gt; refers to whatever Student instance the '&lt;strong&gt;new_school&lt;/strong&gt;' method is being called on. In our example, self is referring to Tim and the new_teacher is going to be value we assign to this attribute. &lt;/p&gt;

&lt;p&gt;Here's how we can use the '&lt;strong&gt;new_school&lt;/strong&gt;' method:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;tim = Student.new("Tim")
tim.new_school("Professor McGonagall")
tim.teacher
 # =&amp;gt; "Professor McGonagall"
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In conclusion, understanding the concept of &lt;strong&gt;self&lt;/strong&gt; in object-oriented programming is fundamental to becoming proficient in languages like Ruby. Embracing the notion of "self-awareness" empowers us to create dynamic and flexible code, enabling our objects to interact effectively and adapt to changing scenarios.&lt;/p&gt;

&lt;p&gt;Happy Coding! &lt;/p&gt;

</description>
      <category>ruby</category>
      <category>beginners</category>
      <category>programming</category>
      <category>tutorial</category>
    </item>
    <item>
      <title>Ruby and Object Oriented Programing</title>
      <dc:creator>sawincp</dc:creator>
      <pubDate>Wed, 07 Jun 2023 17:20:44 +0000</pubDate>
      <link>https://dev.to/sawincp/ruby-and-object-oriented-programing-5382</link>
      <guid>https://dev.to/sawincp/ruby-and-object-oriented-programing-5382</guid>
      <description>&lt;p&gt;For today's topic we will discuss the Ruby programming language and how it's object-oriented approach makes it fun and happy for programmers to use. &lt;/p&gt;

&lt;h2&gt;
  
  
  Ruby
&lt;/h2&gt;

&lt;p&gt;Ruby is a dynamic object-oriented, general-purpose programming language designed and developed in the mid-1990s by Yukihiro "Matz" Matsumoto in Japan. Matz designed Ruby for programmers in mind so that programming should be happy and fun. &lt;/p&gt;

&lt;p&gt;Coming from "Matz"&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;The goal of Ruby is to make programmers happy. I started out to make a programming language that would make me happy, and as a side effect it’s made many, many programmers happy.&lt;/p&gt;

&lt;p&gt;I hope to see Ruby help every programmer in the world to be productive, and to enjoy programming, and to be happy. That is the primary purpose of Ruby language.&lt;/p&gt;

&lt;p&gt;— Yukihiro Matsumoto&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h2&gt;
  
  
  Objects in Ruby
&lt;/h2&gt;

&lt;p&gt;How does Ruby accomplish this task you may ask. Well one of the core concepts of the Ruby language is that fact it sees everything as an object. This means that every bit of information and code can be given their own properties and actions. Through Ruby's object-oriented approach we call properties by the name instance variables and actions are known as methods.&lt;/p&gt;

&lt;p&gt;To create an object in Ruby, you first define a class, which serves as a blueprint or template for creating objects. The class specifies the data (instance variables) and behavior (instance methods) that objects of that class will have. Once the class is defined, you can create objects or instances of that class.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;class Person
  def initialize(name, age)
    @name = name
    @age = age
  end

  def greet
    puts "Hello, my name is #{@name} and I'm #{@age} years old."
  end
end

person1 = Person.new("Alice", 25)
person1.greet #=&amp;gt; Hello, my name is Alice and I'm 25 years old.

person2 = Person.new("Bob", 30)
person2.greet #=&amp;gt; Hello, my name is Bob and I'm 30 years old.

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In this example, the Person class represents a person and has an initialize method that takes the name and age as parameters. The initialize method is automatically called when a new object is created using the new method.&lt;/p&gt;

&lt;p&gt;We create two objects (person1 and person2) using the Person.new syntax and pass the name and age as arguments. Each object has its own separate set of instance variables (&lt;a class="mentioned-user" href="https://dev.to/name"&gt;@name&lt;/a&gt; and @age) initialized with the provided values.&lt;/p&gt;

&lt;p&gt;We can then call the greet method on each object using the dot notation. The greet method accesses the instance variables &lt;a class="mentioned-user" href="https://dev.to/name"&gt;@name&lt;/a&gt; and @age to display a greeting specific to each person.&lt;/p&gt;

&lt;h2&gt;
  
  
  Ruby's  Flexibility
&lt;/h2&gt;

&lt;p&gt;In addition to Ruby's object-oriented approach, Ruby is extremely flexible to code in since it allows its users to freely alter its parts. Essential parts of Ruby can be removed or redefined, at will. Existing parts can be added upon. Ruby tries not to restrict the coder. Ruby's block help increase it's flexibility since a programmer can attach a closure to any method to describe how that method should act.&lt;/p&gt;

&lt;p&gt;In Ruby, block helper methods are a way to define methods that accept a block of code as an argument. These methods can execute the block of code within their implementation, allowing for more flexible and dynamic behavior.&lt;/p&gt;

&lt;p&gt;Block helper methods are commonly used in Ruby for tasks like iterating over collections, performing conditional operations, and executing custom logic. They provide a powerful way to encapsulate and reuse code.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;def greet
  puts "Hello!"
  yield if block_given?
  puts "Goodbye!"
end

greet do
  puts "Nice to meet you!"
end

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In this example, we define a method called greet that prints "Hello!" and "Goodbye!", with the yield keyword used to execute a block of code if it is given. The yield statement acts as a placeholder for the block.&lt;/p&gt;

&lt;p&gt;When we invoke greet and provide a block of code (do ... end), the code within the block is executed between the "Hello!" and "Goodbye!" messages. In this case, it prints "Nice to meet you!".&lt;/p&gt;

&lt;p&gt;Block helper methods can also accept parameters and pass them to the block.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;def repeat(n)
  n.times do |i|
    yield i
  end
end

repeat(3) do |i|
  puts "Iteration #{i+1}"
end

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In this example, the repeat method takes an integer n as a parameter. It then executes the block n times, passing the iteration index (i) to the block. The block prints the current iteration number.&lt;/p&gt;

&lt;p&gt;When we invoke repeat(3) and provide a block, the block is executed three times, printing "Iteration 1", "Iteration 2", and "Iteration 3".&lt;/p&gt;

&lt;p&gt;Block helper methods are widely used in Ruby, and they provide a flexible way to extend the behavior of methods and create reusable code patterns. They are especially useful for tasks that require iterating, filtering, transforming, or performing custom logic on collections or data structures.&lt;/p&gt;

&lt;h2&gt;
  
  
  Ruby Inheritance
&lt;/h2&gt;

&lt;p&gt;Finally, unlike many other object-oriented languages Ruby features single inheritance but understands the concept of modules which are a collection of methods. Classes can include different modules that gives them access to all the methods from that module for free.&lt;/p&gt;

&lt;p&gt;Inheritance is a fundamental concept in object-oriented programming that allows a class to inherit attributes and behaviors from another class. In Ruby, you can create a subclass that inherits from a superclass using the inheritance mechanism.&lt;/p&gt;

&lt;p&gt;To define a subclass that inherits from a superclass, you use the &amp;lt; symbol followed by the name of the superclass. The subclass inherits all the instance variables, instance methods, and class methods of the superclass.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;class Animal
  def breathe
    puts "I'm breathing!"
  end
end

class Mammal &amp;lt; Animal
  def feed_offspring
    puts "I'm feeding my offspring."
  end
end

dog = Mammal.new
dog.breathe #=&amp;gt; I'm breathing!
dog.feed_offspring #=&amp;gt; I'm feeding my offspring.

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In this example, we have a superclass called Animal that defines an instance method breathe. We then define a subclass called Mammal that inherits from Animal using the &amp;lt; symbol.&lt;/p&gt;

&lt;p&gt;The Mammal subclass inherits the breathe method from the Animal superclass. Additionally, we define a new instance method feed_offspring specific to the Mammal class.&lt;/p&gt;

&lt;p&gt;We create an instance of Mammal called dog and can call both the inherited method breathe and the specific method feed_offspring on that instance.&lt;/p&gt;

&lt;p&gt;Inheritance allows subclasses to reuse and extend the behavior defined in their superclass. Subclasses can add new methods, override existing methods, and access the inherited methods and instance variables.&lt;/p&gt;

&lt;p&gt;In Ruby, single inheritance is supported, meaning a class can inherit from only one superclass. However, you can create a hierarchy of classes with multiple levels of inheritance, where subclasses can themselves become superclasses for other subclasses.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;class Animal
  def breathe
    puts "I'm breathing!"
  end
end

class Mammal &amp;lt; Animal
  def feed_offspring
    puts "I'm feeding my offspring."
  end
end

class Dog &amp;lt; Mammal
  def bark
    puts "Woof woof!"
  end
end

dog = Dog.new
dog.breathe #=&amp;gt; I'm breathing!
dog.feed_offspring #=&amp;gt; I'm feeding my offspring.
dog.bark #=&amp;gt; Woof woof!

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In this updated example, we introduce a new subclass called Dog that inherits from Mammal. The Dog class adds its own instance method bark.&lt;/p&gt;

&lt;p&gt;Now, the Dog class inherits the methods breathe and feed_offspring from its superclass Mammal and can also call its own method bark. This demonstrates the hierarchical nature of inheritance, where subclasses inherit from their immediate superclass and all the way up the inheritance chain.&lt;/p&gt;

&lt;p&gt;Inheritance is a powerful mechanism that promotes code reuse and allows for creating class hierarchies, where classes can share common behaviors while adding their own specific characteristics.&lt;/p&gt;

&lt;p&gt;From what we have discussed, you can see why programmers enjoy working in Ruby. &lt;/p&gt;

</description>
    </item>
    <item>
      <title>Introduction to React!</title>
      <dc:creator>sawincp</dc:creator>
      <pubDate>Mon, 13 Mar 2023 18:45:11 +0000</pubDate>
      <link>https://dev.to/sawincp/introduction-to-react-3p8d</link>
      <guid>https://dev.to/sawincp/introduction-to-react-3p8d</guid>
      <description>&lt;p&gt;How do we, as web developers create dynamic and engaging web applications in a fast and efficient manner? In order to do this, one of the biggest tools in our arsenal is by using existing external libraries in our applications. When it comes to web development, there are many frontend libraries that are available for us, but one has surpassed others in popularity and demand. &lt;/p&gt;

&lt;h2&gt;
  
  
  What is React?
&lt;/h2&gt;

&lt;p&gt;React is an open-source JavaScript-based UI development library created and maintained by Meta (Facebook). This library first appeared in 2013 and has quickly developed into one of the most commonly used frontend libraries for web development. Some reasons for the increase in React's popularity are:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Ease of creating dynamic applications&lt;/li&gt;
&lt;li&gt;Improved performance&lt;/li&gt;
&lt;li&gt;Reusable components&lt;/li&gt;
&lt;li&gt;Unidirectional data flow&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Today we will discuss the last two points. What are React components and how is data passed between them?&lt;/p&gt;

&lt;h2&gt;
  
  
  React Components
&lt;/h2&gt;

&lt;p&gt;Components are the building blocks of any React application, and a single app usually consists of multiple components. These components have their own logic and controls, and they can be reused throughout the application, which in turn dramatically reduces the application’s development time.&lt;/p&gt;

&lt;h3&gt;
  
  
  Creating Components
&lt;/h3&gt;

&lt;p&gt;One thing we need to recognize when dealing with React Components is they share a parent-child relationship. This relationship tells React how and what to render to the DOM. When creating a React application the first component that is made is traditionally called App with all of the child components returned inside of the App component.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fpu7rw9fdgx74ll34p55z.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fpu7rw9fdgx74ll34p55z.png" alt="App Component" width="456" height="225"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;In our example above we can see that our App component has three child components:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Header&lt;/li&gt;
&lt;li&gt;About&lt;/li&gt;
&lt;li&gt;ArticleList&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F2d34zdaotmzp9cesuydd.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F2d34zdaotmzp9cesuydd.png" alt="Header Component" width="206" height="191"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fhcj9scrp1juh843nr41g.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fhcj9scrp1juh843nr41g.png" alt="About Component" width="525" height="222"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fxmi3sxuuq2xn737ctoek.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fxmi3sxuuq2xn737ctoek.png" alt="Article List" width="321" height="355"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;As we can see, each of these child components all have different different responsibilities within our application.  Our Header component is responsible for displaying the name, About is responsible for displaying the logo and the about text of our application and the ArticleList component is responsible for creating another component (Article) and displaying information for each article.&lt;/p&gt;

&lt;p&gt;When it comes to describing React components in JavaScript terms, a React component is just a JavaScript function. In JavaScript, functions are meant to handle a small portion of the overall applications functionality. In React, components work the same way. One difference between a JavaScript function and a React component is their syntax. If you notice in the code snippets above, each of our components start with a capital letter. This tells React that this function is a React component instead of a JavaScript function and are considered Functional Components in React. &lt;/p&gt;

&lt;p&gt;There is another kind of component in React called Class Components, however for the purpose of this article I will not be reviewing them and will leave a link at the bottom for further information. &lt;/p&gt;

&lt;h2&gt;
  
  
  Properties in React
&lt;/h2&gt;

&lt;p&gt;Props in React, short for properties, allows information to be passed to our components. These props help make the components more dynamic. Remember that one of the reasons React is popular is the fact that information is unidirectional and that React components share a parent-child relationship. This means that data can only flow down from a parent component to its child components. The way to do this is through Props. &lt;/p&gt;

&lt;h3&gt;
  
  
  Creating Props
&lt;/h3&gt;

&lt;p&gt;As stated, props are passed down from parent components their child components in React. The child components takes in those props as arguments that can be used within that component. &lt;/p&gt;

&lt;p&gt;To create a prop in react we first need to identify which component will be the parent and which one(s) will be the children. In our example we have App being the parent component of our Header, About and ArticleList components, then ArticleList being the parent of our Article component. &lt;/p&gt;

&lt;p&gt;When we look at our example, our Header component needs the name of our blog, the About component needs the image and about text of our blog and our ArticleList component needs the information about our articles. We are able to pass this information down by assign each of these a variable in our App component and setting it equal to the data we want: &lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F0eg85lasrsmsx71ywzkj.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F0eg85lasrsmsx71ywzkj.png" alt="Component Props" width="416" height="57"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;From here these variables are taken in by our components via arguments that contain the data we want to use. &lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fhhawoua3s8tcefgrgzu9.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fhhawoua3s8tcefgrgzu9.png" alt="Header Prop" width="216" height="64"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fbpwn8mm9uw5ajkj2gknr.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fbpwn8mm9uw5ajkj2gknr.png" alt="About Prop" width="521" height="67"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ffo27n81480rw6kzj0ly0.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ffo27n81480rw6kzj0ly0.png" alt="ArticleList Prop" width="260" height="57"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Our ArticleList component is also the parent of our Article component which passes down individual article elements to Article via props. &lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F5cvlyrcrrrzkzgbbv6y0.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F5cvlyrcrrrzkzgbbv6y0.png" alt="Article Props" width="207" height="116"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;ArticleList passes down the article key, title, date, and preview data into our Article component. Similarly, Article takes in these props as arguments and uses them within its own component. &lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F9gyoxs88xscylsf95mkl.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F9gyoxs88xscylsf95mkl.png" alt="Article Component" width="476" height="219"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;With all of our components created and information flowing in the correct manner, we have been able to create a fully functioning website!&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fs3treg8qp96z2qqzcmq7.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fs3treg8qp96z2qqzcmq7.png" alt="Blog App" width="800" height="568"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h4&gt;
  
  
  Links
&lt;/h4&gt;

&lt;p&gt;&lt;a href="https://gist.github.com/tkrotoff/b1caa4c3a185629299ec234d2314e190"&gt;Frontend Framework Popularity&lt;/a&gt;&lt;br&gt;
&lt;a href="https://beta.reactjs.org/reference/react-dom/components"&gt;React Components&lt;/a&gt;&lt;br&gt;
&lt;a href="https://beta.reactjs.org/learn/passing-props-to-a-component"&gt;React Props&lt;/a&gt;&lt;/p&gt;

</description>
      <category>react</category>
      <category>javascript</category>
      <category>beginners</category>
      <category>tutorial</category>
    </item>
    <item>
      <title>My Rick and Morty Search App: A Retrospective</title>
      <dc:creator>sawincp</dc:creator>
      <pubDate>Tue, 24 Jan 2023 02:15:06 +0000</pubDate>
      <link>https://dev.to/sawincp/my-rick-and-morty-search-app-a-retrospective-52j9</link>
      <guid>https://dev.to/sawincp/my-rick-and-morty-search-app-a-retrospective-52j9</guid>
      <description>&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F6n0x8f2ykcz1sj4os4b2.jpeg" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F6n0x8f2ykcz1sj4os4b2.jpeg" alt="Rick_and_Morty" width="640" height="358"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;When I look back at the first phase of my software engineering journey, I am proud of myself for learning a new skillset and honing some of my existing skills as a software developer. When reviewing all the topics that were covered during this phase, I felt confident when it was time to showcase what I have learned up to this point through my end of phase project. With that, I have created a search web application that utilizes the Rick and Morty API to search for specific character's of the Adult Swim series. &lt;/p&gt;

&lt;p&gt;My goal for this article is to walk through my thought process for this project and also highlight key concepts from the first phase of the program and show how the "three pillars of web programming" come together to create my final project. &lt;/p&gt;

&lt;h2&gt;
  
  
  The Decision
&lt;/h2&gt;

&lt;p&gt;For me, one of the hardest parts of this project was trying to pick where I should start. There are a lot of free, open-source application program interfaces (API's) out there for the public to use but in the beginning I was struggling to find something that was interesting and matched my skillset as a beginner programmer. In looking through the list of potential APIs to use, I came across the Rick and Morty API. This peaked my interest since I am a fan of the series and I felt that I could make an engaging web application that API data provided. &lt;/p&gt;

&lt;h2&gt;
  
  
  The Review
&lt;/h2&gt;

&lt;p&gt;According to the website, "this is a RESTful API in which the base url contains information about all available API's resources. All requests are GET requests and go over HTTPS and all responses will be return data in JSON."&lt;/p&gt;

&lt;p&gt;Because of this, when I created the fetch request in my application, I didn't need to create an object with a set of configuration parameters to get the type of data in the response from the API call.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const configureObject = {
      method: "GET",
      headers: {
        "Content-Type": "application/json",
        "Accept": "application/json",
   },
};

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Since the Rick and Morty API has this functionality already built into it, I just needed to choose which resource I wanted to use to complete my fetch request. There are three resources available when we use the Rick and Morty API:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Characters: &lt;a href="https://rickandmortyapi.com/api/character" rel="noopener noreferrer"&gt;https://rickandmortyapi.com/api/character&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;Locations: &lt;a href="https://rickandmortyapi.com/api/location" rel="noopener noreferrer"&gt;https://rickandmortyapi.com/api/location&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;Episodes: &lt;a href="https://rickandmortyapi.com/api/episode" rel="noopener noreferrer"&gt;https://rickandmortyapi.com/api/episode&lt;/a&gt;
&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;I knew I wanted to use the character resource but I also knew I didn't want to just list out all the characters at once; I wanted to be able to search through the characters based off of certain criteria. Luckily, the documentation mentioned there is a way to filter through characters by adding search parameters to the URL:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://rickandmortyapi.com/api/character/" rel="noopener noreferrer"&gt;https://rickandmortyapi.com/api/character/&lt;/a&gt;?&lt;/p&gt;

&lt;h2&gt;
  
  
  The Setup
&lt;/h2&gt;

&lt;p&gt;Now that I had the resource I wanted, it was time for me to start building out my fetch call with the parameters I wanted to use. I felt the best way to do this was by creating a search form where the user could type in the character's name  and add additional parameters of either the character's status (alive, dead, unknown) or the character's gender (male, female, genderless, unknown) or both. &lt;/p&gt;

&lt;p&gt;To do this, I started by creating a form in my HTML file along with adding the constant options for the different parameters. This rendered my search box and the drop downs for status and gender in my index.html file. &lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fryqvfvedno16tul5tvw8.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fryqvfvedno16tul5tvw8.png" alt="Form Elements" width="523" height="345"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fif4pvhccxbgti7nxmyev.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fif4pvhccxbgti7nxmyev.png" alt="Rendered HTML" width="800" height="272"&gt;&lt;/a&gt;&lt;/p&gt;




&lt;h2&gt;
  
  
  The Functionality
&lt;/h2&gt;

&lt;p&gt;Once I had my form created, I now had to add my JavaScript to  incorporate functionality into my application. &lt;/p&gt;

&lt;p&gt;By creating a form in my HTML, I was able to use an event listener to submit the information from my search. The first step was to capture the search value and the values from my drop down's and use them in my fetch call to return the appropriate data. After that I needed to display the results of my call to the DOM. &lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fl8uk6usx8ivlgm3x4fwl.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fl8uk6usx8ivlgm3x4fwl.png" alt="Submit Form" width="664" height="440"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fvh3dfirfblpmn2dpqqmz.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fvh3dfirfblpmn2dpqqmz.png" alt="Filled Form" width="800" height="231"&gt;&lt;/a&gt;&lt;/p&gt;




&lt;h2&gt;
  
  
  The DOM
&lt;/h2&gt;

&lt;p&gt;To display the results of my fetch call, I wanted to create a table on the DOM to show the character's name, and a button called "Character Information". Once the button was clicked, I wanted the table to populate with the character's avatar, last known location, origin and number of episodes that character has been in since the series started. &lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fjk3mjnsvay70sf513mz2.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fjk3mjnsvay70sf513mz2.png" alt="Display Character code" width="598" height="688"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fao2ldbaee240lzp58sx1.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fao2ldbaee240lzp58sx1.png" alt="Show Character Info" width="800" height="517"&gt;&lt;/a&gt;&lt;/p&gt;




&lt;h2&gt;
  
  
  The Final Step
&lt;/h2&gt;

&lt;p&gt;The final piece of my application was to add one more event listener to my table. For every piece of information displayed once the user clicks on the "Character Information Button" I wanted that information to highlight orange when the user hovered over it. The highlight is per piece and I set a timeout for this even to last about 5 seconds long. &lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Frgud3hvwzak3fudzvihq.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Frgud3hvwzak3fudzvihq.png" alt="Highlight Event" width="607" height="173"&gt;&lt;/a&gt;&lt;/p&gt;




&lt;h2&gt;
  
  
  Future Functionality
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Add a load more button to load more characters&lt;/li&gt;
&lt;li&gt;Change layout from a table to a grid&lt;/li&gt;
&lt;li&gt;Randomize return of characters from search&lt;/li&gt;
&lt;/ul&gt;

&lt;h4&gt;
  
  
  References
&lt;/h4&gt;

&lt;p&gt;&lt;a href="https://rickandmortyapi.com/documentation" rel="noopener noreferrer"&gt;Rick and Morty API Documentation&lt;/a&gt;&lt;/p&gt;

</description>
      <category>bitcoin</category>
      <category>cryptocurrency</category>
      <category>security</category>
      <category>discuss</category>
    </item>
    <item>
      <title>Working with Common Data Structures In JavaScript</title>
      <dc:creator>sawincp</dc:creator>
      <pubDate>Fri, 20 Jan 2023 18:14:29 +0000</pubDate>
      <link>https://dev.to/sawincp/working-with-common-data-structures-in-javascript-5dg6</link>
      <guid>https://dev.to/sawincp/working-with-common-data-structures-in-javascript-5dg6</guid>
      <description>&lt;p&gt;When starting your journey into software engineering it may be intimating when it comes to learning about data structures and data manipulation. Even so, it's a must know topic for anyone looking to work in the software development word.  &lt;/p&gt;

&lt;p&gt;In this article I will try to give a simple explanation of some of the fundamental data structures (Arrays and Objects), what they are, when are they useful, and how we can implement them using JavaScript.&lt;/p&gt;

&lt;h2&gt;
  
  
  What is a Data Structure?
&lt;/h2&gt;

&lt;p&gt;In programming, a data structure is a way to organize, manage and store data in a program so that we can efficiently access and modify that data in a specific way.&lt;/p&gt;

&lt;p&gt;Specifically, a data structure is a collection of data and their values, the relationship between them and any functions or operations that can be applied to that data. &lt;/p&gt;

&lt;p&gt;We will discuss the two most common types of data structures in JavaScript, Arrays and Objects. &lt;/p&gt;

&lt;h2&gt;
  
  
  Arrays
&lt;/h2&gt;

&lt;p&gt;An array is simply a list of data when it comes to JavaScript. It can be a list of any data type such as numbers, strings, or even other arrays. To access a value within an array we simply use the index (position) of the value we want. Remember that the index of an array always starts at zero (0) and goes up from there.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// array is a list of items
const array1 = ['a','b','c']

// array can contain multiple data types
const array2 = ['a', 1, 'hello!', 56]

// access string 'hello!' in array2
console.log (array2[2]) // hello!
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Unlike other programming languages, the JavaScript array can be any size with any type of data type contained within it. This allows the array to be dynamic, meaning it can grow and shrink as necessary to meet our needs. &lt;/p&gt;

&lt;p&gt;In JavaScript, arrays come with many methods we can use for different purposes, such as adding or deleting items from the array, sorting the array, filtering its values, finding its length, etc. These tools allow us to work with the dynamic nature of the JavaScript array and create dynamic engaging programs. &lt;/p&gt;

&lt;p&gt;Arrays are useful when we have to store individual values and add/delete values from the beginning or end of the data structure. The array is one of fundamental data structures used in JavaScript and understanding when and how to incorporate them into your programs is essential to building your programs.&lt;/p&gt;

&lt;h2&gt;
  
  
  Objects
&lt;/h2&gt;

&lt;p&gt;In JavaScript, an object is a collection of key-value pairs. We use curly braces to declare the object then declare the key followed by a colon and then the corresponding value and each key must be unique within the object. This means you cannot have two keys with the same name.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const object = {
      key1: value1
      key2: value2
      key3: value3
}

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;To access values in an objects we are able to use two different methods. We can use the dot (.) notation method or the bracket ([]) notation method.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;console.log(object.key1) // value1

console.log(object[key1]) // value1
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Objects can store different data types (like arrays!), other objects and even functions! &lt;/p&gt;

&lt;p&gt;Just like arrays, the JavaScript object has many methods we can use to manipulate the object data. &lt;/p&gt;

&lt;p&gt;We use objects when we need a way to group together data that have something in common or are somehow related (think of an address book). A persons address book may contain contain the person's name, their address, phone number and their email.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const addressBook = {
      name: "John Smith"
      address: "123 Main Street"
      phoneNumber: "123-4567-8910"
      email:"john@example.com"
}

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This allows us to group certain data together for easy access when creating our programs. &lt;/p&gt;

&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;I hope I've helped clarify some of the mystery surrounding JavaScript Array's and Objects. I've tried to put each of these subjects in general terms as to better understand what they are, when they are useful and when you may implement them in your programs. If you're interested in learning more I have put links at the bottom to MDN and w3School reference documents on these topics. &lt;/p&gt;

&lt;p&gt;Happy Coding! &lt;/p&gt;

&lt;p&gt;&lt;a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array" rel="noopener noreferrer"&gt;MDN Array&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" rel="noopener noreferrer"&gt;MDN Object&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://www.w3schools.com/js/js_arrays.asp" rel="noopener noreferrer"&gt;w3School Array&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://www.w3schools.com/js/js_objects.asp" rel="noopener noreferrer"&gt;w3School Object&lt;/a&gt;&lt;/p&gt;

</description>
      <category>developer</category>
      <category>productivity</category>
    </item>
  </channel>
</rss>
