<?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: Amirul Asyraf</title>
    <description>The latest articles on DEV Community by Amirul Asyraf (@asyraf).</description>
    <link>https://dev.to/asyraf</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%2F347549%2F7357d8c0-b9d0-46d0-beaa-5daaf53ec029.jpg</url>
      <title>DEV Community: Amirul Asyraf</title>
      <link>https://dev.to/asyraf</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/asyraf"/>
    <language>en</language>
    <item>
      <title>Deleting all data from a table in Ruby on Rails</title>
      <dc:creator>Amirul Asyraf</dc:creator>
      <pubDate>Thu, 05 Aug 2021 00:08:37 +0000</pubDate>
      <link>https://dev.to/asyraf/deleting-all-data-from-a-table-in-ruby-on-rails-39he</link>
      <guid>https://dev.to/asyraf/deleting-all-data-from-a-table-in-ruby-on-rails-39he</guid>
      <description>&lt;p&gt;If you need to refresh your database, you might find that you need to delete every row from a specific table during development. &lt;/p&gt;

&lt;p&gt;That is &lt;strong&gt;slow&lt;/strong&gt; 🐢. &lt;strong&gt;Freaking slow&lt;/strong&gt; 🐄.&lt;/p&gt;

&lt;p&gt;Glad Rails have many small tricks to solve everything 🌈 (but still not documented properly 😂).&lt;/p&gt;

&lt;p&gt;There are several ways to achieve this. I will rank it from the slowest/scary/bad to the simplest and yet powerful solution.&lt;/p&gt;

&lt;p&gt;Let's go !!!&lt;/p&gt;




&lt;h2&gt;
  
  
  3) Drop the whole database and bring it back up again
&lt;/h2&gt;

&lt;p&gt;We can achieve this with &lt;code&gt;rake:db:drop&lt;/code&gt;, then &lt;code&gt;rake:db:setup&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Wuuu, drop the database then create a new one, load the schema, and initialize it with the seed data.&lt;/p&gt;

&lt;p&gt;Slow &amp;amp; Expensive !!! 🐢&lt;/p&gt;

&lt;h2&gt;
  
  
  2) destroy_all
&lt;/h2&gt;

&lt;p&gt;Destroys the records by &lt;strong&gt;instantiating each record&lt;/strong&gt; and &lt;strong&gt;calling its &lt;code&gt;#destroy&lt;/code&gt; method&lt;/strong&gt;. Each object's &lt;strong&gt;callbacks are executed&lt;/strong&gt; (including &lt;code&gt;:dependent&lt;/code&gt; association options). &lt;/p&gt;

&lt;p&gt;Records are instantiated and it invokes &lt;code&gt;before_remove&lt;/code&gt;, &lt;code&gt;after_remove&lt;/code&gt; , &lt;code&gt;before_destroy&lt;/code&gt; and &lt;code&gt;after_destroy&lt;/code&gt; callbacks.&lt;/p&gt;

&lt;p&gt;Examples&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight ruby"&gt;&lt;code&gt;&lt;span class="c1"&gt;# 1&lt;/span&gt;
&lt;span class="no"&gt;Person&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;where&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="ss"&gt;sex: &lt;/span&gt;&lt;span class="s2"&gt;"male"&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nf"&gt;destroy_all&lt;/span&gt; &lt;span class="c1"&gt;# 😂&lt;/span&gt;

&lt;span class="c1"&gt;# 2&lt;/span&gt;
&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Author&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="no"&gt;ActiveRecord&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="no"&gt;Base&lt;/span&gt;
  &lt;span class="n"&gt;has_many&lt;/span&gt; &lt;span class="ss"&gt;:books&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;

&lt;span class="n"&gt;author&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;books&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;size&lt;/span&gt; &lt;span class="c1"&gt;# =&amp;gt; 3&lt;/span&gt;
&lt;span class="n"&gt;author&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;books&lt;/span&gt;
&lt;span class="c1"&gt;# =&amp;gt; [&lt;/span&gt;
&lt;span class="c1"&gt;#       #&amp;lt;Book id: 1, name: "Sapiens", author_id: 1&amp;gt;,&lt;/span&gt;
&lt;span class="c1"&gt;#       #&amp;lt;Book id: 2, name: "The Artist's Way", author_id: 2&amp;gt;,&lt;/span&gt;
&lt;span class="c1"&gt;#       #&amp;lt;Book id: 3, name: "Remote", author_id: 3&amp;gt;&lt;/span&gt;
&lt;span class="c1"&gt;#    ]&lt;/span&gt;

&lt;span class="n"&gt;author&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;books&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;destroy_all&lt;/span&gt;

&lt;span class="n"&gt;author&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;books&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;size&lt;/span&gt; &lt;span class="c1"&gt;# =&amp;gt; 0&lt;/span&gt;
&lt;span class="n"&gt;author&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;books&lt;/span&gt;      &lt;span class="c1"&gt;# =&amp;gt; []&lt;/span&gt;

&lt;span class="no"&gt;Book&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;find&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="c1"&gt;# =&amp;gt; Couldn't find Book with id=1&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;blockquote&gt;
&lt;p&gt;Note: &lt;strong&gt;Instantiation&lt;/strong&gt;, &lt;strong&gt;callback execution&lt;/strong&gt;, and &lt;strong&gt;deletion of each record&lt;/strong&gt; can be &lt;strong&gt;time consuming&lt;/strong&gt; when you're removing many records at once. It generates at least one SQL &lt;code&gt;DELETE&lt;/code&gt; query per record (or possibly more, to enforce your callbacks).&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h2&gt;
  
  
  1) delete_all 👑
&lt;/h2&gt;

&lt;p&gt;If you want to &lt;strong&gt;delete many rows quickly&lt;/strong&gt;, without concern for their &lt;strong&gt;associations&lt;/strong&gt; or &lt;strong&gt;callbacks&lt;/strong&gt;, use &lt;code&gt;delete_all&lt;/code&gt; instead.&lt;/p&gt;

&lt;p&gt;Boot up a console and call &lt;code&gt;delete_all&lt;/code&gt; on your model:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight ruby"&gt;&lt;code&gt;&lt;span class="sx"&gt;% rails &lt;/span&gt;&lt;span class="n"&gt;c&lt;/span&gt;
&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="no"&gt;Book&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;count&lt;/span&gt;
&lt;span class="c1"&gt;# =&amp;gt; 1200 &lt;/span&gt;
&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="no"&gt;Book&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;delete_all&lt;/span&gt;
&lt;span class="c1"&gt;# =&amp;gt; 1200&lt;/span&gt;
&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="no"&gt;Book&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;count&lt;/span&gt;
&lt;span class="c1"&gt;# =&amp;gt; 0&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Deletes the records &lt;strong&gt;without instantiating the records first&lt;/strong&gt;, and hence &lt;strong&gt;not calling the &lt;code&gt;#destroy&lt;/code&gt; method&lt;/strong&gt; nor &lt;strong&gt;invoking callbacks&lt;/strong&gt;. &lt;/p&gt;

&lt;p&gt;This is a single SQL &lt;code&gt;DELETE&lt;/code&gt; statement that goes straight to the database, much more efficient than &lt;code&gt;destroy_all&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;You need to be careful on two things:&lt;/p&gt;

&lt;p&gt;1- As &lt;code&gt;.delete_all&lt;/code&gt; method does not instantiate any object hence does not provide any &lt;strong&gt;callback&lt;/strong&gt; (&lt;code&gt;before_*&lt;/code&gt; and &lt;code&gt;after_destroy&lt;/code&gt; don't get triggered).&lt;/p&gt;

&lt;p&gt;2- Be careful with relations, in particular &lt;code&gt;:dependent&lt;/code&gt; rules defined on associations are not honored. Returns the number of rows affected.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight ruby"&gt;&lt;code&gt;&lt;span class="no"&gt;Post&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;where&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="ss"&gt;person_id: &lt;/span&gt;&lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nf"&gt;where&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="ss"&gt;category: &lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s1"&gt;'Something'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;'Else'&lt;/span&gt;&lt;span class="p"&gt;]).&lt;/span&gt;&lt;span class="nf"&gt;delete_all&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Both calls delete the affected posts all at once with a single &lt;code&gt;DELETE&lt;/code&gt; statement. &lt;/p&gt;

&lt;p&gt;If you need to &lt;strong&gt;destroy dependent associations&lt;/strong&gt; or &lt;strong&gt;call your &lt;code&gt;before_*&lt;/code&gt; or &lt;code&gt;after_destroy&lt;/code&gt; callbacks&lt;/strong&gt;, use the &lt;code&gt;destroy_all&lt;/code&gt; method instead.&lt;/p&gt;

&lt;p&gt;More on &lt;a href="https://api.rubyonrails.org/classes/ActiveRecord/Associations/CollectionProxy.html#method-i-delete_all"&gt;here&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The end&lt;/p&gt;




&lt;p&gt;resources:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://api.rubyonrails.org/classes/ActiveRecord/Relation.html#method-i-destroy_all"&gt;1&lt;/a&gt; &lt;a href="https://api.rubyonrails.org/classes/ActiveRecord/Associations/CollectionProxy.html#method-i-destroy_all"&gt;2&lt;/a&gt; &lt;a href="https://www.dxw.com/2014/04/ruby-on-rails-deleting-all-data-from-a-table/"&gt;3&lt;/a&gt; &lt;a href="https://riptutorial.com/ruby-on-rails/example/9287/-delete-all"&gt;4&lt;/a&gt; &lt;a href="https://stackoverflow.com/questions/5322298/deleting-all-records-in-a-database-table"&gt;5&lt;/a&gt; &lt;a href="https://api.rubyonrails.org/classes/ActiveRecord/Relation.html#method-i-delete_all"&gt;6&lt;/a&gt; &lt;a href="https://guides.rubyonrails.org/active_record_migrations.html"&gt;7&lt;/a&gt;&lt;/p&gt;

</description>
      <category>ruby</category>
      <category>rails</category>
      <category>database</category>
      <category>beginners</category>
    </item>
    <item>
      <title>See if User is online using Devise in Ruby on Rails</title>
      <dc:creator>Amirul Asyraf</dc:creator>
      <pubDate>Mon, 02 Aug 2021 23:55:53 +0000</pubDate>
      <link>https://dev.to/asyraf/see-if-user-is-online-using-devise-in-ruby-on-rails-6jh</link>
      <guid>https://dev.to/asyraf/see-if-user-is-online-using-devise-in-ruby-on-rails-6jh</guid>
      <description>&lt;p&gt;Recently, I needed to figure out if a user was active(online) on my site. Because I hadn't done this before, I Googled for inspiration 🌈 but found little success 🙃. The majority of posts on Stack Overflow and blogs were variations of the same solution:&lt;/p&gt;

&lt;h3&gt;
  
  
  1) Add a &lt;code&gt;last_online&lt;/code&gt; timestamp column to your User model, then define a scope for user called &lt;code&gt;online_now&lt;/code&gt;.
&lt;/h3&gt;

&lt;p&gt;Meaning, add new column to store on every timestamp the user is active/online. Then make a query(scope) from database which user that last active 2-10 minutes ago and display to the view.&lt;/p&gt;

&lt;p&gt;This may take up some of your app's resources if you have many users who are logged in, because this will execute(update column &amp;amp; database query) before every controller action requested by someone who is logged in.&lt;/p&gt;

&lt;p&gt;I think this process is slow.&lt;/p&gt;

&lt;h3&gt;
  
  
  2) &lt;a href="https://stackoverflow.com/a/5504188/12595487"&gt;devise_lastseenable&lt;/a&gt; Rubygems
&lt;/h3&gt;

&lt;p&gt;Pretty old gems.&lt;/p&gt;

&lt;h3&gt;
  
  
  3) When someone has &lt;code&gt;logged in&lt;/code&gt;, you can push him into an array, if someone has &lt;code&gt;logged out&lt;/code&gt;, just remove him from the array.
&lt;/h3&gt;

&lt;p&gt;I think last time I use this trick is on my Computer Science studies 😂 at universities long time ago.&lt;/p&gt;

&lt;h3&gt;
  
  
  4) WebSocket &amp;amp; Redis
&lt;/h3&gt;

&lt;p&gt;If you're bothered by making a trip to database on every. single. http. request. only to have some small window where you can sort of assume that a user is online; There have an alternate solution.&lt;/p&gt;

&lt;p&gt;Using &lt;strong&gt;websockets&lt;/strong&gt; and &lt;strong&gt;redis&lt;/strong&gt; it's possible to reliably get a user's online status up to the millisecond without making a billion costly writes to disk. &lt;/p&gt;

&lt;p&gt;Unfortunately, it requires a good bit &lt;strong&gt;more work&lt;/strong&gt; and &lt;strong&gt;has two additional dependencies&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;There is &lt;a href="https://www.ryanepp.com/blog/how-do-i-tell-if-a-user-is-online"&gt;one article&lt;/a&gt; that explains well how to use this solution if you are interested.&lt;/p&gt;

&lt;p&gt;But probably not my choice for a simple project.&lt;/p&gt;




&lt;h2&gt;
  
  
  So, What else ?
&lt;/h2&gt;

&lt;p&gt;Well, this solution is related to &lt;strong&gt;Devise&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;When we create a User Model based on Devise;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight ruby"&gt;&lt;code&gt;&lt;span class="n"&gt;rails&lt;/span&gt; &lt;span class="n"&gt;generate&lt;/span&gt; &lt;span class="n"&gt;devise&lt;/span&gt; &lt;span class="no"&gt;User&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Devise will create &lt;code&gt;updated_at&lt;/code&gt; column with a &lt;code&gt;datetime&lt;/code&gt; data type.&lt;/p&gt;

&lt;p&gt;This is good tho. We can utilize already column from Devise instead of creating a new column like previous solution.&lt;/p&gt;

&lt;p&gt;Whenever the &lt;code&gt;current_user&lt;/code&gt; does any action, his &lt;code&gt;updated_at&lt;/code&gt; will be set as &lt;code&gt;Time.now&lt;/code&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight ruby"&gt;&lt;code&gt;&lt;span class="c1"&gt;#application_controller.rb&lt;/span&gt;

&lt;span class="n"&gt;after_action&lt;/span&gt; &lt;span class="ss"&gt;:update_user_online&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="ss"&gt;if: :user_signed_in?&lt;/span&gt;

&lt;span class="kp"&gt;private&lt;/span&gt;

&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;update_user_online&lt;/span&gt;
  &lt;span class="n"&gt;current_user&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;try&lt;/span&gt; &lt;span class="ss"&gt;:touch&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;And we will just say that the user is online if he was &lt;code&gt;updated_at&lt;/code&gt; during the last &lt;code&gt;2.minutes&lt;/code&gt; or &lt;code&gt;1.minutes&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight ruby"&gt;&lt;code&gt;&lt;span class="c1"&gt;#user.rb&lt;/span&gt;

&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;online?&lt;/span&gt;
  &lt;span class="n"&gt;updated_at&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;minutes&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;ago&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now we can get &lt;code&gt;true&lt;/code&gt; or &lt;code&gt;false&lt;/code&gt; if we make a call like &lt;code&gt;@user.online?&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight ruby"&gt;&lt;code&gt;&lt;span class="c1"&gt;#users/show.html.erb&lt;/span&gt;

&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sx"&gt;%= @user.online? %&amp;gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This is one of the best and simple solution I get when doing research and a little bit of experimentation. &lt;/p&gt;

&lt;p&gt;I am very happy 😇 if you can share to me other tricks that is better than this.&lt;/p&gt;

&lt;p&gt;The end&lt;/p&gt;




&lt;p&gt;resources:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://stackoverflow.com/questions/10805479/rails-how-to-check-for-online-users"&gt;1&lt;/a&gt; &lt;a href="https://stackoverflow.com/questions/3184973/rails-using-devise-to-show-online-users"&gt;2&lt;/a&gt; &lt;a href="https://stackoverflow.com/questions/20821341/rails-how-to-show-users-last-seen-at-time"&gt;3&lt;/a&gt; &lt;a href="https://www.ryanepp.com/blog/how-do-i-tell-if-a-user-is-online"&gt;4&lt;/a&gt; &lt;a href="https://blog.corsego.com/set-user-status-online"&gt;5&lt;/a&gt;&lt;/p&gt;

</description>
      <category>ruby</category>
      <category>rails</category>
      <category>authentication</category>
      <category>ux</category>
    </item>
    <item>
      <title>Rails ActiveRecord Data Types</title>
      <dc:creator>Amirul Asyraf</dc:creator>
      <pubDate>Tue, 27 Jul 2021 00:23:38 +0000</pubDate>
      <link>https://dev.to/asyraf/rails-activerecord-data-types-32ip</link>
      <guid>https://dev.to/asyraf/rails-activerecord-data-types-32ip</guid>
      <description>&lt;p&gt;I usually curios to know about &lt;strong&gt;Data Types for ActiveRecord&lt;/strong&gt;, but the &lt;a href="https://guides.rubyonrails.org/active_record_migrations.html" rel="noopener noreferrer"&gt;Active Record Migration&lt;/a&gt; documentation itself does not have that information.&lt;/p&gt;

&lt;p&gt;So, I do some Google search and here is the TL;DR.&lt;/p&gt;

&lt;h3&gt;
  
  
  The ActiveRecord data types available in Rails 6;
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;&lt;code&gt;:primary_key&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;:string&lt;/code&gt;: &lt;strong&gt;short text&lt;/strong&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;:text&lt;/code&gt;: &lt;strong&gt;long text&lt;/strong&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;:integer&lt;/code&gt;: &lt;strong&gt;whole numbers&lt;/strong&gt; [-4, 0, 9, 772]&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;:float&lt;/code&gt;: &lt;strong&gt;double-precision floating-point numbers&lt;/strong&gt; [3244.90]&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;:decimal&lt;/code&gt;: &lt;strong&gt;high-precision floating-point numbers&lt;/strong&gt; [3244.2342343789212]&lt;/li&gt;
&lt;li&gt;&lt;code&gt;:datetime&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;:time&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;:date&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;:binary&lt;/code&gt;: &lt;strong&gt;1 / 0&lt;/strong&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;:boolean&lt;/code&gt;: &lt;strong&gt;true or false&lt;/strong&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;These data types are used in instances such as &lt;strong&gt;migrations&lt;/strong&gt;.&lt;/p&gt;

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

&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;change&lt;/span&gt;
  &lt;span class="n"&gt;create_table&lt;/span&gt; &lt;span class="ss"&gt;:categories&lt;/span&gt; &lt;span class="k"&gt;do&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt;&lt;span class="n"&gt;t&lt;/span&gt;&lt;span class="o"&gt;|&lt;/span&gt;
    &lt;span class="n"&gt;t&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;string&lt;/span&gt; &lt;span class="ss"&gt;:title&lt;/span&gt;
    &lt;span class="n"&gt;t&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;boolean&lt;/span&gt; &lt;span class="ss"&gt;:is_subcategory&lt;/span&gt;
    &lt;span class="n"&gt;t&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;string&lt;/span&gt; &lt;span class="ss"&gt;:permalink&lt;/span&gt;
    &lt;span class="n"&gt;t&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;timestamps&lt;/span&gt;
  &lt;span class="k"&gt;end&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;


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

&lt;/div&gt;




&lt;h2&gt;
  
  
  Specific DBMS Data Type
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://github.com/rails/rails/blob/25f6a8e84fb6c27f489fc3e593656ddb72f25613/activerecord/lib/active_record/connection_adapters/abstract_mysql_adapter.rb#L16" rel="noopener noreferrer"&gt;&lt;strong&gt;MySQL&lt;/strong&gt; Data Types&lt;/a&gt;&lt;/p&gt;

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

&lt;span class="no"&gt;NATIVE_DATABASE_TYPES&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="ss"&gt;primary_key: &lt;/span&gt;&lt;span class="s2"&gt;"bigint auto_increment PRIMARY KEY"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="ss"&gt;string:      &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="ss"&gt;name: &lt;/span&gt;&lt;span class="s2"&gt;"varchar"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="ss"&gt;limit: &lt;/span&gt;&lt;span class="mi"&gt;255&lt;/span&gt; &lt;span class="p"&gt;},&lt;/span&gt;
        &lt;span class="ss"&gt;text:        &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="ss"&gt;name: &lt;/span&gt;&lt;span class="s2"&gt;"text"&lt;/span&gt; &lt;span class="p"&gt;},&lt;/span&gt;
        &lt;span class="ss"&gt;integer:     &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="ss"&gt;name: &lt;/span&gt;&lt;span class="s2"&gt;"int"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="ss"&gt;limit: &lt;/span&gt;&lt;span class="mi"&gt;4&lt;/span&gt; &lt;span class="p"&gt;},&lt;/span&gt;
        &lt;span class="ss"&gt;float:       &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="ss"&gt;name: &lt;/span&gt;&lt;span class="s2"&gt;"float"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="ss"&gt;limit: &lt;/span&gt;&lt;span class="mi"&gt;24&lt;/span&gt; &lt;span class="p"&gt;},&lt;/span&gt;
        &lt;span class="ss"&gt;decimal:     &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="ss"&gt;name: &lt;/span&gt;&lt;span class="s2"&gt;"decimal"&lt;/span&gt; &lt;span class="p"&gt;},&lt;/span&gt;
        &lt;span class="ss"&gt;datetime:    &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="ss"&gt;name: &lt;/span&gt;&lt;span class="s2"&gt;"datetime"&lt;/span&gt; &lt;span class="p"&gt;},&lt;/span&gt;
        &lt;span class="ss"&gt;timestamp:   &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="ss"&gt;name: &lt;/span&gt;&lt;span class="s2"&gt;"timestamp"&lt;/span&gt; &lt;span class="p"&gt;},&lt;/span&gt;
        &lt;span class="ss"&gt;time:        &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="ss"&gt;name: &lt;/span&gt;&lt;span class="s2"&gt;"time"&lt;/span&gt; &lt;span class="p"&gt;},&lt;/span&gt;
        &lt;span class="ss"&gt;date:        &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="ss"&gt;name: &lt;/span&gt;&lt;span class="s2"&gt;"date"&lt;/span&gt; &lt;span class="p"&gt;},&lt;/span&gt;
        &lt;span class="ss"&gt;binary:      &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="ss"&gt;name: &lt;/span&gt;&lt;span class="s2"&gt;"blob"&lt;/span&gt; &lt;span class="p"&gt;},&lt;/span&gt;
        &lt;span class="ss"&gt;blob:        &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="ss"&gt;name: &lt;/span&gt;&lt;span class="s2"&gt;"blob"&lt;/span&gt; &lt;span class="p"&gt;},&lt;/span&gt;
        &lt;span class="ss"&gt;boolean:     &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="ss"&gt;name: &lt;/span&gt;&lt;span class="s2"&gt;"tinyint"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="ss"&gt;limit: &lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt; &lt;span class="p"&gt;},&lt;/span&gt;
        &lt;span class="ss"&gt;json:        &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="ss"&gt;name: &lt;/span&gt;&lt;span class="s2"&gt;"json"&lt;/span&gt; &lt;span class="p"&gt;},&lt;/span&gt;
      &lt;span class="p"&gt;}&lt;/span&gt;


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

&lt;/div&gt;

&lt;p&gt;&lt;a href="https://github.com/rails/rails/blob/25f6a8e84fb6c27f489fc3e593656ddb72f25613/activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb#L119" rel="noopener noreferrer"&gt;&lt;strong&gt;PostgreSQL&lt;/strong&gt; Data Types&lt;/a&gt;&lt;/p&gt;

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

&lt;span class="no"&gt;NATIVE_DATABASE_TYPES&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="ss"&gt;primary_key: &lt;/span&gt;&lt;span class="s2"&gt;"bigserial primary key"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="ss"&gt;string:      &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="ss"&gt;name: &lt;/span&gt;&lt;span class="s2"&gt;"character varying"&lt;/span&gt; &lt;span class="p"&gt;},&lt;/span&gt;
        &lt;span class="ss"&gt;text:        &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="ss"&gt;name: &lt;/span&gt;&lt;span class="s2"&gt;"text"&lt;/span&gt; &lt;span class="p"&gt;},&lt;/span&gt;
        &lt;span class="ss"&gt;integer:     &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="ss"&gt;name: &lt;/span&gt;&lt;span class="s2"&gt;"integer"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="ss"&gt;limit: &lt;/span&gt;&lt;span class="mi"&gt;4&lt;/span&gt; &lt;span class="p"&gt;},&lt;/span&gt;
        &lt;span class="ss"&gt;float:       &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="ss"&gt;name: &lt;/span&gt;&lt;span class="s2"&gt;"float"&lt;/span&gt; &lt;span class="p"&gt;},&lt;/span&gt;
        &lt;span class="ss"&gt;decimal:     &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="ss"&gt;name: &lt;/span&gt;&lt;span class="s2"&gt;"decimal"&lt;/span&gt; &lt;span class="p"&gt;},&lt;/span&gt;
        &lt;span class="ss"&gt;datetime:    &lt;/span&gt;&lt;span class="p"&gt;{},&lt;/span&gt; &lt;span class="c1"&gt;# set dynamically based on datetime_type&lt;/span&gt;
        &lt;span class="ss"&gt;timestamp:   &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="ss"&gt;name: &lt;/span&gt;&lt;span class="s2"&gt;"timestamp"&lt;/span&gt; &lt;span class="p"&gt;},&lt;/span&gt;
        &lt;span class="ss"&gt;timestamptz: &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="ss"&gt;name: &lt;/span&gt;&lt;span class="s2"&gt;"timestamptz"&lt;/span&gt; &lt;span class="p"&gt;},&lt;/span&gt;
        &lt;span class="ss"&gt;time:        &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="ss"&gt;name: &lt;/span&gt;&lt;span class="s2"&gt;"time"&lt;/span&gt; &lt;span class="p"&gt;},&lt;/span&gt;
        &lt;span class="ss"&gt;date:        &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="ss"&gt;name: &lt;/span&gt;&lt;span class="s2"&gt;"date"&lt;/span&gt; &lt;span class="p"&gt;},&lt;/span&gt;
        &lt;span class="ss"&gt;daterange:   &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="ss"&gt;name: &lt;/span&gt;&lt;span class="s2"&gt;"daterange"&lt;/span&gt; &lt;span class="p"&gt;},&lt;/span&gt;
        &lt;span class="ss"&gt;numrange:    &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="ss"&gt;name: &lt;/span&gt;&lt;span class="s2"&gt;"numrange"&lt;/span&gt; &lt;span class="p"&gt;},&lt;/span&gt;
        &lt;span class="ss"&gt;tsrange:     &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="ss"&gt;name: &lt;/span&gt;&lt;span class="s2"&gt;"tsrange"&lt;/span&gt; &lt;span class="p"&gt;},&lt;/span&gt;
        &lt;span class="ss"&gt;tstzrange:   &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="ss"&gt;name: &lt;/span&gt;&lt;span class="s2"&gt;"tstzrange"&lt;/span&gt; &lt;span class="p"&gt;},&lt;/span&gt;
        &lt;span class="ss"&gt;int4range:   &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="ss"&gt;name: &lt;/span&gt;&lt;span class="s2"&gt;"int4range"&lt;/span&gt; &lt;span class="p"&gt;},&lt;/span&gt;
        &lt;span class="ss"&gt;int8range:   &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="ss"&gt;name: &lt;/span&gt;&lt;span class="s2"&gt;"int8range"&lt;/span&gt; &lt;span class="p"&gt;},&lt;/span&gt;
        &lt;span class="ss"&gt;binary:      &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="ss"&gt;name: &lt;/span&gt;&lt;span class="s2"&gt;"bytea"&lt;/span&gt; &lt;span class="p"&gt;},&lt;/span&gt;
        &lt;span class="ss"&gt;boolean:     &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="ss"&gt;name: &lt;/span&gt;&lt;span class="s2"&gt;"boolean"&lt;/span&gt; &lt;span class="p"&gt;},&lt;/span&gt;
        &lt;span class="ss"&gt;xml:         &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="ss"&gt;name: &lt;/span&gt;&lt;span class="s2"&gt;"xml"&lt;/span&gt; &lt;span class="p"&gt;},&lt;/span&gt;
        &lt;span class="ss"&gt;tsvector:    &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="ss"&gt;name: &lt;/span&gt;&lt;span class="s2"&gt;"tsvector"&lt;/span&gt; &lt;span class="p"&gt;},&lt;/span&gt;
        &lt;span class="ss"&gt;hstore:      &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="ss"&gt;name: &lt;/span&gt;&lt;span class="s2"&gt;"hstore"&lt;/span&gt; &lt;span class="p"&gt;},&lt;/span&gt;
        &lt;span class="ss"&gt;inet:        &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="ss"&gt;name: &lt;/span&gt;&lt;span class="s2"&gt;"inet"&lt;/span&gt; &lt;span class="p"&gt;},&lt;/span&gt;
        &lt;span class="ss"&gt;cidr:        &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="ss"&gt;name: &lt;/span&gt;&lt;span class="s2"&gt;"cidr"&lt;/span&gt; &lt;span class="p"&gt;},&lt;/span&gt;
        &lt;span class="ss"&gt;macaddr:     &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="ss"&gt;name: &lt;/span&gt;&lt;span class="s2"&gt;"macaddr"&lt;/span&gt; &lt;span class="p"&gt;},&lt;/span&gt;
        &lt;span class="ss"&gt;uuid:        &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="ss"&gt;name: &lt;/span&gt;&lt;span class="s2"&gt;"uuid"&lt;/span&gt; &lt;span class="p"&gt;},&lt;/span&gt;
        &lt;span class="ss"&gt;json:        &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="ss"&gt;name: &lt;/span&gt;&lt;span class="s2"&gt;"json"&lt;/span&gt; &lt;span class="p"&gt;},&lt;/span&gt;
        &lt;span class="ss"&gt;jsonb:       &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="ss"&gt;name: &lt;/span&gt;&lt;span class="s2"&gt;"jsonb"&lt;/span&gt; &lt;span class="p"&gt;},&lt;/span&gt;
        &lt;span class="ss"&gt;ltree:       &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="ss"&gt;name: &lt;/span&gt;&lt;span class="s2"&gt;"ltree"&lt;/span&gt; &lt;span class="p"&gt;},&lt;/span&gt;
        &lt;span class="ss"&gt;citext:      &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="ss"&gt;name: &lt;/span&gt;&lt;span class="s2"&gt;"citext"&lt;/span&gt; &lt;span class="p"&gt;},&lt;/span&gt;
        &lt;span class="ss"&gt;point:       &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="ss"&gt;name: &lt;/span&gt;&lt;span class="s2"&gt;"point"&lt;/span&gt; &lt;span class="p"&gt;},&lt;/span&gt;
        &lt;span class="ss"&gt;line:        &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="ss"&gt;name: &lt;/span&gt;&lt;span class="s2"&gt;"line"&lt;/span&gt; &lt;span class="p"&gt;},&lt;/span&gt;
        &lt;span class="ss"&gt;lseg:        &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="ss"&gt;name: &lt;/span&gt;&lt;span class="s2"&gt;"lseg"&lt;/span&gt; &lt;span class="p"&gt;},&lt;/span&gt;
        &lt;span class="ss"&gt;box:         &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="ss"&gt;name: &lt;/span&gt;&lt;span class="s2"&gt;"box"&lt;/span&gt; &lt;span class="p"&gt;},&lt;/span&gt;
        &lt;span class="ss"&gt;path:        &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="ss"&gt;name: &lt;/span&gt;&lt;span class="s2"&gt;"path"&lt;/span&gt; &lt;span class="p"&gt;},&lt;/span&gt;
        &lt;span class="ss"&gt;polygon:     &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="ss"&gt;name: &lt;/span&gt;&lt;span class="s2"&gt;"polygon"&lt;/span&gt; &lt;span class="p"&gt;},&lt;/span&gt;
        &lt;span class="ss"&gt;circle:      &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="ss"&gt;name: &lt;/span&gt;&lt;span class="s2"&gt;"circle"&lt;/span&gt; &lt;span class="p"&gt;},&lt;/span&gt;
        &lt;span class="ss"&gt;bit:         &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="ss"&gt;name: &lt;/span&gt;&lt;span class="s2"&gt;"bit"&lt;/span&gt; &lt;span class="p"&gt;},&lt;/span&gt;
        &lt;span class="ss"&gt;bit_varying: &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="ss"&gt;name: &lt;/span&gt;&lt;span class="s2"&gt;"bit varying"&lt;/span&gt; &lt;span class="p"&gt;},&lt;/span&gt;
        &lt;span class="ss"&gt;money:       &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="ss"&gt;name: &lt;/span&gt;&lt;span class="s2"&gt;"money"&lt;/span&gt; &lt;span class="p"&gt;},&lt;/span&gt;
        &lt;span class="ss"&gt;interval:    &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="ss"&gt;name: &lt;/span&gt;&lt;span class="s2"&gt;"interval"&lt;/span&gt; &lt;span class="p"&gt;},&lt;/span&gt;
        &lt;span class="ss"&gt;oid:         &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="ss"&gt;name: &lt;/span&gt;&lt;span class="s2"&gt;"oid"&lt;/span&gt; &lt;span class="p"&gt;},&lt;/span&gt;
      &lt;span class="p"&gt;}&lt;/span&gt;


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

&lt;/div&gt;




&lt;h3&gt;
  
  
  &lt;a href="https://stackoverflow.com/questions/17918117/rails-4-list-of-available-datatypes/25702629#25702629" rel="noopener noreferrer"&gt;Rails data types mapping to different DB data types:&lt;/a&gt;
&lt;/h3&gt;

&lt;p&gt;It is important to know not only the types but the mapping of these types to the database types, too:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fgithub.com%2Fifyouseewendy%2Fifyouseewendy.github.io%2Fraw%2Fsource%2Fimage-repo%2Fdata_types_1.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fgithub.com%2Fifyouseewendy%2Fifyouseewendy.github.io%2Fraw%2Fsource%2Fimage-repo%2Fdata_types_1.png" alt="Data type mapping 1"&gt;&lt;/a&gt;&lt;br&gt;
&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fgithub.com%2Fifyouseewendy%2Fifyouseewendy.github.io%2Fraw%2Fsource%2Fimage-repo%2Fdata_types_2.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fgithub.com%2Fifyouseewendy%2Fifyouseewendy.github.io%2Fraw%2Fsource%2Fimage-repo%2Fdata_types_2.png" alt="Data type mapping 2"&gt;&lt;/a&gt;&lt;/p&gt;




&lt;h3&gt;
  
  
  The easy way ✌️
&lt;/h3&gt;

&lt;p&gt;You can access this list everytime you want (even if you don't have Internet access) through:&lt;/p&gt;

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

&lt;span class="n"&gt;rails&lt;/span&gt; &lt;span class="n"&gt;generate&lt;/span&gt; &lt;span class="n"&gt;model&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="n"&gt;h&lt;/span&gt;


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

&lt;/div&gt;

&lt;p&gt;The end&lt;/p&gt;




&lt;p&gt;resources;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://michaelsoolee.com/rails-activerecord-data-types/" rel="noopener noreferrer"&gt;1&lt;/a&gt; &lt;a href="https://blog.corsego.com/rails-activerecord-data-types" rel="noopener noreferrer"&gt;2&lt;/a&gt; &lt;a href="https://blog.ifyouseewendy.com/blog/2015/08/10/data-types-in-rails/" rel="noopener noreferrer"&gt;3&lt;/a&gt; &lt;a href="https://stackoverflow.com/questions/17918117/rails-4-list-of-available-datatypes" rel="noopener noreferrer"&gt;4&lt;/a&gt; &lt;a href="https://stackoverflow.com/questions/3956186/where-is-the-documentation-page-for-activerecord-data-types" rel="noopener noreferrer"&gt;5&lt;/a&gt; &lt;a href="https://edgeguides.rubyonrails.org/active_record_postgresql.html" rel="noopener noreferrer"&gt;6&lt;/a&gt; &lt;a href="https://stackoverflow.com/a/30132729/12595487" rel="noopener noreferrer"&gt;7&lt;/a&gt;&lt;/p&gt;

</description>
      <category>ruby</category>
      <category>rails</category>
      <category>database</category>
    </item>
    <item>
      <title>How to create a transparent navbar with CSS</title>
      <dc:creator>Amirul Asyraf</dc:creator>
      <pubDate>Fri, 23 Jul 2021 06:44:49 +0000</pubDate>
      <link>https://dev.to/asyraf/how-to-create-a-transparent-navbar-with-css-56l8</link>
      <guid>https://dev.to/asyraf/how-to-create-a-transparent-navbar-with-css-56l8</guid>
      <description>&lt;p&gt;Last week, I look at the Gumroad website to know how to &lt;a href="https://dev.to/asyraf/how-to-add-made-by-at-the-bottom-corner-of-the-website-11jb"&gt;Add &lt;strong&gt;Made By&lt;/strong&gt; at the bottom corner of the website&lt;/a&gt; (Pssst, read that article, Pretty cool stuff 🔥).&lt;/p&gt;

&lt;p&gt;This week, when I look at the &lt;a href="https://super.so/" rel="noopener noreferrer"&gt;Super.so&lt;/a&gt; website, I notice new &lt;strong&gt;interesting CSS stuff&lt;/strong&gt; :P.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn.hashnode.com%2Fres%2Fhashnode%2Fimage%2Fupload%2Fv1626918550117%2FMHkvdd6I8.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn.hashnode.com%2Fres%2Fhashnode%2Fimage%2Fupload%2Fv1626918550117%2FMHkvdd6I8.gif" alt="Screen-Recording-2021-07-22-at-0 (3).gif"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Check the &lt;a href="https://super.so/" rel="noopener noreferrer"&gt;Super.so&lt;/a&gt; website.&lt;/p&gt;

&lt;p&gt;Notice the slightly transparent navbar. How cool is that?&lt;/p&gt;

&lt;p&gt;I already see this kind of design on other websites too !!!&lt;/p&gt;

&lt;p&gt;Other examples;&lt;/p&gt;

&lt;h3&gt;
  
  
  ✅ &lt;a href="https://obsidian.md/" rel="noopener noreferrer"&gt;Obsidian&lt;/a&gt;
&lt;/h3&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn.hashnode.com%2Fres%2Fhashnode%2Fimage%2Fupload%2Fv1626917479129%2F-lVBMzFzb.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn.hashnode.com%2Fres%2Fhashnode%2Fimage%2Fupload%2Fv1626917479129%2F-lVBMzFzb.gif" alt="Screen-Recording-2021-07-22-at-0.gif"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  ✅ &lt;a href="https://mikkelmalmberg.com/ama" rel="noopener noreferrer"&gt;Mikkel Malmberg&lt;/a&gt;
&lt;/h3&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn.hashnode.com%2Fres%2Fhashnode%2Fimage%2Fupload%2Fv1626918601839%2F7IagPaxbJ.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn.hashnode.com%2Fres%2Fhashnode%2Fimage%2Fupload%2Fv1626918601839%2F7IagPaxbJ.gif" alt="Screen-Recording-2021-07-22-at-0 (4).gif"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Pretty cool right 🌈. So, &lt;strong&gt;how are we gonna make it ??&lt;/strong&gt;&lt;/p&gt;




&lt;h2&gt;
  
  
  Code Snippet
&lt;/h2&gt;

&lt;p&gt;&lt;iframe height="600" src="https://codepen.io/asyrafff/embed/ExmwWVM?height=600&amp;amp;default-tab=result&amp;amp;embed-version=2"&gt;
&lt;/iframe&gt;
&lt;/p&gt;

&lt;p&gt;Try to scroll.&lt;/p&gt;

&lt;p&gt;When I inspect the navbar on Super.so website, they already setup for &lt;strong&gt;Webkit&lt;/strong&gt; (Safari) &amp;amp; &lt;strong&gt;Moz&lt;/strong&gt; (Mozilla Firefox).&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;The main thing to implement this trick is;&lt;/strong&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  ✅ &lt;a href="https://developer.mozilla.org/en-US/docs/Web/CSS/backdrop-filter" rel="noopener noreferrer"&gt;&lt;strong&gt;backdrop-filter&lt;/strong&gt;&lt;/a&gt;
&lt;/h3&gt;

&lt;p&gt;Without any explanation, try to change the &lt;code&gt;blur(20px)&lt;/code&gt; to &lt;code&gt;blur(10px)&lt;/code&gt; or &lt;code&gt;blur(50px)&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Do you notice the transparent blur is changed?&lt;/p&gt;

&lt;p&gt;That is the main ingredient to implement this trick ✌️.&lt;/p&gt;

&lt;h3&gt;
  
  
  ✅ &lt;strong&gt;background&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;If you remove the background, the text that appears behind the navbar seems ugly.&lt;/p&gt;

&lt;p&gt;It is kinda a glitch when you remove the background CSS. So, add it.&lt;/p&gt;

&lt;p&gt;Make the color or text visible behind the navbar a little less obvious by adding a background.&lt;/p&gt;

&lt;h3&gt;
  
  
  ✅ &lt;strong&gt;position&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;As always, for any navbar anywhere in the world, we must place &lt;code&gt;sticky&lt;/code&gt; to keep the navbar at the top when we scroll the website.&lt;/p&gt;

&lt;h3&gt;
  
  
  ✅ &lt;strong&gt;top&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;Usually, people place the navbar at the top. I don't know if you want to put it at the bottom?? 😂&lt;/p&gt;

&lt;h3&gt;
  
  
  ✅ &lt;strong&gt;z-index&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;You can put any value more than zero to the &lt;code&gt;z-index&lt;/code&gt; . You put it &lt;code&gt;n&lt;/code&gt; index from the root floor of the page. &lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Imagine a house with five floors, 3 on top and 2 on ground level. The root floor is located on the Ground floor, which is 3 floors from the bottom. You want the navbar to be more obvious, so you place it on the third floor, the highest floor, and not on the lower ground floor.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;That's it. 🌈&lt;/p&gt;

&lt;p&gt;The End&lt;/p&gt;




&lt;p&gt;Resources:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;no resources as I just use DevTool 😆&lt;/p&gt;
&lt;/blockquote&gt;

</description>
      <category>html</category>
      <category>css</category>
      <category>uiweekly</category>
      <category>ux</category>
    </item>
    <item>
      <title>How to add "Made By" at the bottom corner of the website?</title>
      <dc:creator>Amirul Asyraf</dc:creator>
      <pubDate>Fri, 23 Jul 2021 06:41:07 +0000</pubDate>
      <link>https://dev.to/asyraf/how-to-add-made-by-at-the-bottom-corner-of-the-website-11jb</link>
      <guid>https://dev.to/asyraf/how-to-add-made-by-at-the-bottom-corner-of-the-website-11jb</guid>
      <description>&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;hey&lt;/strong&gt; 👋 , I still haven't figured out what the title should be. Comment if you find a well-suited title after reading this article. You're awesome :P&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Last week, I look at the Basecamp website to know how to  &lt;a href="https://dev.to/asyraf/how-to-change-link-underline-color-with-css-m47"&gt;Change the underline color Tricks in CSS&lt;/a&gt; (Pssst, read that article, Pretty cool stuff 🔥).&lt;/p&gt;

&lt;p&gt;This week, when I look at the &lt;a href="https://gumroad.com/"&gt;Gumroad&lt;/a&gt; website, specifically all of the product pages, I notice new &lt;strong&gt;interesting HTML stuff&lt;/strong&gt; :P.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--1pqPpwDU--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://cdn.hashnode.com/res/hashnode/image/upload/v1625402479146/tJ5N_S-O1.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--1pqPpwDU--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://cdn.hashnode.com/res/hashnode/image/upload/v1625402479146/tJ5N_S-O1.png" alt="Screenshot 2021-07-04 at 20.38.54.png" width="800" height="468"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Check this example &lt;a href="https://maketheweb.gumroad.com/l/cleanshot"&gt;product page&lt;/a&gt;. &lt;/p&gt;

&lt;p&gt;Notice the &lt;strong&gt;Powered by Gumroad&lt;/strong&gt; floating stuff(I don't know what to call it 😂). That is cool actually. I have already seen this trend on many websites nowadays.&lt;/p&gt;

&lt;p&gt;Free marketing. It &lt;strong&gt;makes your brand stands out.&lt;/strong&gt; 🦄&lt;/p&gt;

&lt;p&gt;Other examples;&lt;/p&gt;

&lt;h3&gt;
  
  
  ✅ &lt;a href="https://webflow.com/"&gt;Webflow&lt;/a&gt;
&lt;/h3&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--7I7I6LcK--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://cdn.hashnode.com/res/hashnode/image/upload/v1625403848848/UVXPSnI52.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--7I7I6LcK--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://cdn.hashnode.com/res/hashnode/image/upload/v1625403848848/UVXPSnI52.png" alt="Screenshot 2021-07-04 at 21.03.54.png" width="302" height="116"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  ✅ &lt;a href="https://stripe.com/"&gt;Stripe&lt;/a&gt;
&lt;/h3&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--FhvAXvZE--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://cdn.hashnode.com/res/hashnode/image/upload/v1625405986451/4es9B8a5dQ.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--FhvAXvZE--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://cdn.hashnode.com/res/hashnode/image/upload/v1625405986451/4es9B8a5dQ.png" alt="Screenshot 2021-07-04 at 21.39.33.png" width="240" height="84"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  ✅ &lt;a href="https://mmm.page/"&gt;mmm.page&lt;/a&gt;
&lt;/h3&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--FabFD7OZ--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_800/https://cdn.hashnode.com/res/hashnode/image/upload/v1625524700710/T-m2zSc4O.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--FabFD7OZ--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_800/https://cdn.hashnode.com/res/hashnode/image/upload/v1625524700710/T-m2zSc4O.gif" alt="mmmgif.gif" width="576" height="200"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;If you have a &lt;strong&gt;side project&lt;/strong&gt;, you can show: &lt;strong&gt;"Made by asyraf"&lt;/strong&gt; too!!&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Examples:&lt;/p&gt;

&lt;h3&gt;
  
  
  ✅ &lt;a href="https://makerreads.com/"&gt;MakerReads&lt;/a&gt;
&lt;/h3&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--wgVKTneD--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://cdn.hashnode.com/res/hashnode/image/upload/v1625404204004/JblUw5dAx.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--wgVKTneD--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://cdn.hashnode.com/res/hashnode/image/upload/v1625404204004/JblUw5dAx.png" alt="Screenshot 2021-07-04 at 21.09.53.png" width="378" height="134"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  ✅ &lt;a href="https://nomoregoogle.com/"&gt;No More Google&lt;/a&gt;
&lt;/h3&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--hlhY-qB8--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://cdn.hashnode.com/res/hashnode/image/upload/v1625406724221/dLREmmHRB.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--hlhY-qB8--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://cdn.hashnode.com/res/hashnode/image/upload/v1625406724221/dLREmmHRB.png" alt="Screenshot 2021-07-04 at 21.51.52.png" width="338" height="134"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  ✅ &lt;a href="https://begreat.me/"&gt;Begreate.me&lt;/a&gt;
&lt;/h3&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--XR2lxPwP--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://cdn.hashnode.com/res/hashnode/image/upload/v1625544977350/GACnz6uuw.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--XR2lxPwP--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://cdn.hashnode.com/res/hashnode/image/upload/v1625544977350/GACnz6uuw.png" alt="Screenshot 2021-07-06 at 12.15.06.png" width="272" height="122"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  ✅ &lt;a href="https://chart2site.com/"&gt;chart2site.com&lt;/a&gt;
&lt;/h3&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--gJ_lSK6_--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://cdn.hashnode.com/res/hashnode/image/upload/v1625545054516/-0DOGxSKH.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--gJ_lSK6_--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://cdn.hashnode.com/res/hashnode/image/upload/v1625545054516/-0DOGxSKH.png" alt="Screenshot 2021-07-06 at 12.17.05.png" width="416" height="138"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Pretty cool right 🌈. So, how we gonna make it ??&lt;/p&gt;




&lt;p&gt;I have two  ways for you;&lt;/p&gt;

&lt;h2&gt;
  
  
  1) Inspect DevTool
&lt;/h2&gt;

&lt;p&gt;Honestly, this is easy. You can just open your &lt;strong&gt;DevTool&lt;/strong&gt; and &lt;strong&gt;inspect&lt;/strong&gt; the element. Most of the view is written in HTML &amp;amp; CSS. Pretty easy right. Don't bother to think of React or Vue or Svelte or other stuff.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--X8ULDb8C--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://cdn.hashnode.com/res/hashnode/image/upload/v1625525183686/I9QTyVDfW.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--X8ULDb8C--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://cdn.hashnode.com/res/hashnode/image/upload/v1625525183686/I9QTyVDfW.png" alt="Screenshot 2021-07-06 at 06.46.04.png" width="800" height="384"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Go and try to inspect the element&lt;/strong&gt;. Use the utilities that Google has been given to you(that's why I love using vanilla things). &lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;If you already forgot how HTML &amp;amp; CSS is, I'm here to help you. Keep reading :P&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h2&gt;
  
  
  2) Code Snippet
&lt;/h2&gt;

&lt;p&gt;There are two variations I came across here. One is like a Gumroad, and the second is like Pieter Levels.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Gumroad&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;iframe height="600" src="https://codepen.io/asyrafff/embed/gOWPodv?height=600&amp;amp;default-tab=result&amp;amp;embed-version=2"&gt;
&lt;/iframe&gt;
&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Pieter Levels&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;iframe height="600" src="https://codepen.io/asyrafff/embed/YzVwYRQ?height=600&amp;amp;default-tab=result&amp;amp;embed-version=2"&gt;
&lt;/iframe&gt;
&lt;/p&gt;

&lt;p&gt;The main thing here is:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;a&lt;/code&gt; tag: to wrap the floating stuff, the text &amp;amp; the logo&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;left&lt;/code&gt; or &lt;code&gt;right&lt;/code&gt;: either you want to put it on the left/right/bottom&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;position&lt;/code&gt;: set it to &lt;code&gt;fixed&lt;/code&gt; as you want it to be sticky when you scroll the page and stick it at its position.&lt;/li&gt;
&lt;li&gt;logo stuff&lt;/li&gt;
&lt;li&gt;decoration stuff; &lt;code&gt;border-radius&lt;/code&gt;, &lt;code&gt;text-decoration&lt;/code&gt;, &lt;code&gt;box-shadow&lt;/code&gt; ......&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;That is all my friends. &lt;strong&gt;Don't forget to mention me&lt;/strong&gt; on &lt;a href="https://twitter.com/asyr0f"&gt;Twitter&lt;/a&gt; if you put it on your website. Happy to see the variation of your guys' creativity 🌈.&lt;/p&gt;

&lt;p&gt;The End&lt;/p&gt;




&lt;p&gt;Resources:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;no resources as I just use DevTool 😆&lt;/p&gt;
&lt;/blockquote&gt;

</description>
      <category>html</category>
      <category>css</category>
      <category>design</category>
      <category>uiweekly</category>
    </item>
    <item>
      <title>How to Get Values From Array With Specific Range in Javascript</title>
      <dc:creator>Amirul Asyraf</dc:creator>
      <pubDate>Thu, 01 Jul 2021 05:08:29 +0000</pubDate>
      <link>https://dev.to/asyraf/how-to-get-values-from-array-with-specific-range-in-javascript-56a7</link>
      <guid>https://dev.to/asyraf/how-to-get-values-from-array-with-specific-range-in-javascript-56a7</guid>
      <description>&lt;h2&gt;
  
  
  Ruby World 💎
&lt;/h2&gt;

&lt;p&gt;In Ruby, I can get the specific range from an array like this;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight ruby"&gt;&lt;code&gt;&lt;span class="o"&gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;myArray&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s1"&gt;'a'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="s1"&gt;'b'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="s1"&gt;'c'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="s1"&gt;'d'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="s1"&gt;'e'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="s1"&gt;'f'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="s1"&gt;'g'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;'h'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;'i'&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
&lt;span class="o"&gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;myArray&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="o"&gt;..&lt;/span&gt;&lt;span class="mi"&gt;4&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
&lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s1"&gt;'a'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="s1"&gt;'b'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="s1"&gt;'c'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;'d'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;'e'&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;However, since I am not using &lt;code&gt;Ruby&lt;/code&gt; on this project and am using &lt;strong&gt;vanilla Javascript&lt;/strong&gt;, I wonder how to achieve the same result.&lt;/p&gt;

&lt;p&gt;Turns out that it's extremely easy to do !!!&lt;/p&gt;




&lt;h2&gt;
  
  
  Javascript World 🌈
&lt;/h2&gt;

&lt;h4&gt;
  
  
  You can use &lt;a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/slice"&gt;&lt;code&gt;array.slice(begin [, end])&lt;/code&gt;&lt;/a&gt; !!!
&lt;/h4&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="nx"&gt;myArray&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;a&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;b&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;c&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;d&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;e&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;f&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;g&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;h&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;i&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt;
&lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="nx"&gt;sliced&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;myArray&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;slice&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;//will contain ['a', 'b', 'c','d', 'e']&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Easy right.&lt;/p&gt;

&lt;p&gt;If you notice, the last index is different from the &lt;code&gt;Ruby&lt;/code&gt; implementation.&lt;/p&gt;

&lt;p&gt;This is because &lt;strong&gt;the last index is non-inclusive&lt;/strong&gt;; to mimic ruby's behavior you have to increment the &lt;code&gt;end&lt;/code&gt; value.&lt;/p&gt;

&lt;p&gt;So, in &lt;code&gt;Ruby&lt;/code&gt;;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight ruby"&gt;&lt;code&gt;&lt;span class="n"&gt;myArray&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;m&lt;/span&gt;&lt;span class="o"&gt;..&lt;/span&gt;&lt;span class="n"&gt;n&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;in &lt;code&gt;Javascript&lt;/code&gt;;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nx"&gt;myArray&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;slice&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;m&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;n&lt;/span&gt;&lt;span class="o"&gt;+&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;blockquote&gt;
&lt;p&gt;the second argument to slice in &lt;code&gt;Ruby&lt;/code&gt; is the &lt;strong&gt;length&lt;/strong&gt;, but in &lt;code&gt;JavaScript&lt;/code&gt; it is the &lt;strong&gt;index of the last element&lt;/strong&gt;.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h2&gt;
  
  
  Other stuff
&lt;/h2&gt;

&lt;p&gt;1) You can also pass a &lt;strong&gt;negative number&lt;/strong&gt;, which selects from the &lt;strong&gt;end of the array&lt;/strong&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="nx"&gt;myArray&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;a&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;b&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;c&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;d&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;e&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;f&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;g&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;h&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;i&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt;
&lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="nx"&gt;lastThree&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;myArray&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;slice&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;  &lt;span class="c1"&gt;//g, h, i&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;2) If &lt;code&gt;end&lt;/code&gt; is &lt;strong&gt;omitted&lt;/strong&gt;, &lt;code&gt;slice&lt;/code&gt; extracts through the end of the sequence (&lt;code&gt;arr.length&lt;/code&gt;).&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="nx"&gt;myArray&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;a&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;b&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;c&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;d&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;e&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;f&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;g&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;h&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;i&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt;
&lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="nx"&gt;noEndInSlice&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;myArray&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;slice&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;  &lt;span class="c1"&gt;//f, g, h, i&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;blockquote&gt;
&lt;p&gt;which indicate that the &lt;code&gt;end&lt;/code&gt; value is &lt;strong&gt;optional&lt;/strong&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;The End&lt;/p&gt;




&lt;p&gt;Resources:&lt;br&gt;
&lt;a href="https://stackoverflow.com/questions/3580239/javascript-array-get-range-of-items"&gt;1&lt;/a&gt; &lt;a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/slice"&gt;2&lt;/a&gt; &lt;a href="https://cmsdk.com/javascript/get-values-from-array-with-specific-range--javascript.html"&gt;3&lt;/a&gt; &lt;/p&gt;

</description>
      <category>javascript</category>
      <category>api</category>
      <category>array</category>
      <category>ruby</category>
    </item>
    <item>
      <title>Ruby Hash Method 101</title>
      <dc:creator>Amirul Asyraf</dc:creator>
      <pubDate>Fri, 25 Jun 2021 01:56:16 +0000</pubDate>
      <link>https://dev.to/asyraf/ruby-hash-method-101-2mn8</link>
      <guid>https://dev.to/asyraf/ruby-hash-method-101-2mn8</guid>
      <description>&lt;h2&gt;
  
  
  What the heck is &lt;strong&gt;Ruby Hash&lt;/strong&gt; ??
&lt;/h2&gt;

&lt;p&gt;You can think of Ruby Hash is kind of an &lt;strong&gt;Array without the numerical indexes&lt;/strong&gt;. You &lt;strong&gt;access the Hash values with &lt;code&gt;Keys&lt;/code&gt;&lt;/strong&gt;. A Hash is a data structure used to store data in the form of &lt;strong&gt;UNIQUE key-value pairs&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;A Hash has certain similarities to an Array, but:&lt;/p&gt;

&lt;p&gt;✅ An Array index is &lt;strong&gt;always an Integer&lt;/strong&gt;&lt;br&gt;
✅ A Hash key &lt;strong&gt;can be (almost) any object&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;We are gonna dive 🤿 into the Hash world in Ruby. But not too deep, just at the level where most people dive in :p. &lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;note:&lt;/strong&gt; I encourage you to open &lt;code&gt;irb&lt;/code&gt; in your favourite terminal app. Make sure you get your hands 🙌 dirty.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;&lt;a id="top"&gt;&lt;/a&gt;  &lt;/p&gt;
&lt;h1&gt;
  
  
  TL;DR
&lt;/h1&gt;

&lt;ul&gt;
&lt;li&gt;1️⃣ Hash Data Syntax&lt;/li&gt;
&lt;li&gt;2️⃣ Common use case of using Hash&lt;/li&gt;
&lt;li&gt;3️⃣ How to Create a Hash&lt;/li&gt;
&lt;li&gt;4️⃣ How to create or update a Hash value&lt;/li&gt;
&lt;li&gt;5️⃣ Values in a Ruby Hash&lt;/li&gt;
&lt;li&gt;6️⃣ Hash Entry Order&lt;/li&gt;
&lt;li&gt;7️⃣ How to delete a Hash entry&lt;/li&gt;
&lt;li&gt;8️⃣ How to Access Values From a Hash&lt;/li&gt;
&lt;li&gt;9️⃣ Extract a nested Hash value&lt;/li&gt;
&lt;li&gt;🔟 Hash Keys 101&lt;/li&gt;
&lt;li&gt;1️⃣1️⃣ Default Values&lt;/li&gt;
&lt;li&gt;1️⃣2️⃣ How to Merge Two Ruby Hashes&lt;/li&gt;
&lt;li&gt;1️⃣3️⃣ Multiple Hash values for one key&lt;/li&gt;
&lt;li&gt;1️⃣4️⃣ Get All Keys and Values From a Hash&lt;/li&gt;
&lt;li&gt;1️⃣5️⃣ Check if key exists in hash&lt;/li&gt;
&lt;li&gt;1️⃣6️⃣ How to change a Hash to Array&lt;/li&gt;
&lt;/ul&gt;


&lt;h2&gt;
  
  
  ✅ Hash Data Syntax
&lt;/h2&gt;

&lt;p&gt;Hash has three Syntax style as &lt;em&gt;Ruby 3.0&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;1) &lt;code&gt;hash rocket =&amp;gt;&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight ruby"&gt;&lt;code&gt;&lt;span class="n"&gt;hashy&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="ss"&gt;:foo&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="ss"&gt;:bar&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="ss"&gt;:baz&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="n"&gt;hashy&lt;/span&gt; &lt;span class="c1"&gt;# =&amp;gt; {:foo=&amp;gt;0, :bar=&amp;gt;1, :baz=&amp;gt;2}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;2) &lt;code&gt;JSON-style syntax&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight ruby"&gt;&lt;code&gt;&lt;span class="n"&gt;hashy&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="ss"&gt;foo: &lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="ss"&gt;bar: &lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="ss"&gt;baz: &lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="n"&gt;hashy&lt;/span&gt; &lt;span class="c1"&gt;# =&amp;gt; {:foo=&amp;gt;0, :bar=&amp;gt;1, :baz=&amp;gt;2} &lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;note:&lt;/strong&gt; The Hash key become a &lt;code&gt;Symbol&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;note:&lt;/strong&gt; You will get an &lt;strong&gt;error&lt;/strong&gt; if you use the key that's not a bareword or a String&lt;br&gt;
&lt;/p&gt;
&lt;/blockquote&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;# Raises SyntaxError (syntax error, unexpected ':', expecting =&amp;gt;):
hashy = {0: 'zero'}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;3) &lt;code&gt;String&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight ruby"&gt;&lt;code&gt;&lt;span class="n"&gt;hashy&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="s1"&gt;'foo'&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;'bar'&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;'baz'&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="n"&gt;hashy&lt;/span&gt; &lt;span class="c1"&gt;# =&amp;gt; {:foo=&amp;gt;0, :bar=&amp;gt;1, :baz=&amp;gt;2} &lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;note:&lt;/strong&gt; same like number 2. But the key is change to the &lt;code&gt;String&lt;/code&gt;, instead of symbol.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;And you can &lt;strong&gt;mix the styles&lt;/strong&gt;;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight ruby"&gt;&lt;code&gt;&lt;span class="n"&gt;hashy&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="ss"&gt;foo: &lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="ss"&gt;:bar&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;'baz'&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="n"&gt;hashy&lt;/span&gt; &lt;span class="c1"&gt;# =&amp;gt; {:foo=&amp;gt;0, :bar=&amp;gt;1, :baz=&amp;gt;2}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Back to the top&lt;/p&gt;

&lt;h2&gt;
  
  
  ✅ Common use case of using Hash
&lt;/h2&gt;

&lt;p&gt;1) Give names to objects&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight ruby"&gt;&lt;code&gt;&lt;span class="n"&gt;book&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="ss"&gt;author: &lt;/span&gt;&lt;span class="s2"&gt;"Daniel J. Levitin"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="ss"&gt;title: &lt;/span&gt;&lt;span class="s2"&gt;"This is Your Brain on Music}"&lt;/span&gt;
&lt;span class="n"&gt;book&lt;/span&gt; &lt;span class="c1"&gt;# =&amp;gt; {:author=&amp;gt;"Daniel J. Levitin", :title=&amp;gt;"This is Your Brain on Music"}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;2) Give names to method arguments&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight ruby"&gt;&lt;code&gt;&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;some_method&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;hash&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
  &lt;span class="nb"&gt;p&lt;/span&gt; &lt;span class="nb"&gt;hash&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;
&lt;span class="n"&gt;some_method&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;&lt;span class="ss"&gt;foo: &lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="ss"&gt;bar: &lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="ss"&gt;baz: &lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;})&lt;/span&gt; &lt;span class="c1"&gt;# =&amp;gt; {:foo=&amp;gt;0, :bar=&amp;gt;1, :baz=&amp;gt;2}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;3) initialize an object&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight ruby"&gt;&lt;code&gt;&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Book&lt;/span&gt;
  &lt;span class="nb"&gt;attr_accessor&lt;/span&gt; &lt;span class="ss"&gt;:author&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="ss"&gt;:title&lt;/span&gt;
  &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;initialize&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;hash&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="nb"&gt;self&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;author&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;hash&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="ss"&gt;:author&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
    &lt;span class="nb"&gt;self&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;title&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;hash&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="ss"&gt;:title&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
  &lt;span class="k"&gt;end&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;
&lt;span class="n"&gt;book1&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="no"&gt;Book&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;new&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="ss"&gt;author: &lt;/span&gt;&lt;span class="s1"&gt;'Daniel J. Levitin'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="ss"&gt;title: &lt;/span&gt;&lt;span class="s2"&gt;"'This is Your Brain on Music')"&lt;/span&gt;
&lt;span class="n"&gt;book1&lt;/span&gt; &lt;span class="c1"&gt;# =&amp;gt; #&amp;lt;Book: @author="Daniel J. Levitin", @title="This is Your Brain on Music"&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;4) Get the frequency from a list of numbers&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight ruby"&gt;&lt;code&gt;&lt;span class="n"&gt;numbers&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;4&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;65&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;55&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;54&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;55&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
&lt;span class="n"&gt;freq_hash&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;numbers&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;each_with_object&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="no"&gt;Hash&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;new&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt;&lt;span class="n"&gt;number&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nb"&gt;hash&lt;/span&gt;&lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="nb"&gt;hash&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;number&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;+=&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="nb"&gt;puts&lt;/span&gt; &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="si"&gt;#{&lt;/span&gt;&lt;span class="n"&gt;freq_hash&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt;
&lt;span class="c1"&gt;# {1=&amp;gt;3, 2=&amp;gt;1, 4=&amp;gt;1, 65=&amp;gt;1, 55=&amp;gt;2, 54=&amp;gt;1}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Another tricks is using &lt;code&gt;tally&lt;/code&gt; method.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight ruby"&gt;&lt;code&gt;&lt;span class="n"&gt;numbers&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;4&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;65&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;55&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;54&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;55&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
&lt;span class="n"&gt;numbers&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;tally&lt;/span&gt;
&lt;span class="c1"&gt;# output&lt;/span&gt;
&lt;span class="c1"&gt;# {1=&amp;gt;3, 2=&amp;gt;1, 4=&amp;gt;1, 65=&amp;gt;1, 55=&amp;gt;2, 54=&amp;gt;1}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;note:&lt;/strong&gt; This is for &lt;strong&gt;Ruby 2.7+&lt;/strong&gt; only. :P&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;5) Specify &lt;code&gt;routes&lt;/code&gt; in Ruby on Rails&lt;/p&gt;

&lt;p&gt;This is a line from the &lt;code&gt;config/routes.rb&lt;/code&gt; file, the Router in a Rails application:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight ruby"&gt;&lt;code&gt;&lt;span class="c1"&gt;# defines a GET route mapped to the new action in the PostsController&lt;/span&gt;

&lt;span class="n"&gt;get&lt;/span&gt; &lt;span class="s1"&gt;'/posts/new'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="ss"&gt;to: &lt;/span&gt;&lt;span class="s1"&gt;'posts#new'&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The example of the above could be rewritten as:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight ruby"&gt;&lt;code&gt;&lt;span class="n"&gt;get&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'/posts/new'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="ss"&gt;to: &lt;/span&gt;&lt;span class="s1"&gt;'posts#new'&lt;/span&gt; &lt;span class="p"&gt;})&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The Hash is the second parameter passed to the &lt;code&gt;get(...)&lt;/code&gt; method.&lt;/p&gt;

&lt;p&gt;Back to the top&lt;/p&gt;

&lt;h2&gt;
  
  
  ✅ How to Create a Hash
&lt;/h2&gt;

&lt;p&gt;Here are three ways to create a Hash:&lt;/p&gt;

&lt;p&gt;1) Method &lt;code&gt;Hash.new&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;You can create a Hash by calling method &lt;code&gt;Hash.new&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight ruby"&gt;&lt;code&gt;&lt;span class="n"&gt;h&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="no"&gt;Hash&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;new&lt;/span&gt; &lt;span class="c1"&gt;#Define empty hash&lt;/span&gt;
&lt;span class="n"&gt;h&lt;/span&gt; &lt;span class="c1"&gt;# =&amp;gt; {}&lt;/span&gt;
&lt;span class="n"&gt;h&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;class&lt;/span&gt; &lt;span class="c1"&gt;# =&amp;gt; Hash&lt;/span&gt;

&lt;span class="n"&gt;h&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="ss"&gt;:first&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;10&lt;/span&gt;
&lt;span class="n"&gt;h&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="ss"&gt;:second&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;20&lt;/span&gt;
&lt;span class="n"&gt;h&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="ss"&gt;:third&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;30&lt;/span&gt;

&lt;span class="nb"&gt;puts&lt;/span&gt; &lt;span class="n"&gt;h&lt;/span&gt; &lt;span class="c1"&gt;# =&amp;gt; {:first=&amp;gt;10, :second=&amp;gt;20, :third=&amp;gt;30}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;2) Method &lt;code&gt;Hash[]&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;You can create a Hash by calling method &lt;code&gt;Hash[]&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Create an empty Hash:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight ruby"&gt;&lt;code&gt;&lt;span class="n"&gt;h&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="no"&gt;Hash&lt;/span&gt;&lt;span class="p"&gt;[]&lt;/span&gt;
&lt;span class="n"&gt;h&lt;/span&gt; &lt;span class="c1"&gt;# =&amp;gt; {}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Create a Hash with initial entries:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight ruby"&gt;&lt;code&gt;&lt;span class="n"&gt;h&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="no"&gt;Hash&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="ss"&gt;foo: &lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="ss"&gt;bar: &lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="ss"&gt;baz: &lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
&lt;span class="n"&gt;h&lt;/span&gt; &lt;span class="c1"&gt;# =&amp;gt; {:foo=&amp;gt;0, :bar=&amp;gt;1, :baz=&amp;gt;2}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;3) Literal form &lt;code&gt;{}&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;You can create a Hash by using its literal form (curly braces).&lt;/p&gt;

&lt;p&gt;Create an empty Hash:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight ruby"&gt;&lt;code&gt;&lt;span class="n"&gt;h&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{}&lt;/span&gt;
&lt;span class="n"&gt;h&lt;/span&gt; &lt;span class="c1"&gt;# =&amp;gt; {}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Create a Hash with initial entries:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight ruby"&gt;&lt;code&gt;&lt;span class="n"&gt;h&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="ss"&gt;foo: &lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="ss"&gt;bar: &lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="ss"&gt;baz: &lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="n"&gt;h&lt;/span&gt; &lt;span class="c1"&gt;# =&amp;gt; {:foo=&amp;gt;0, :bar=&amp;gt;1, :baz=&amp;gt;2}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Back to the top&lt;/p&gt;

&lt;h2&gt;
  
  
  ✅ How to create or update a Hash value
&lt;/h2&gt;

&lt;p&gt;We can use instance method &lt;code&gt;[]=&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight ruby"&gt;&lt;code&gt;&lt;span class="n"&gt;h&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="ss"&gt;foo: &lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="ss"&gt;bar: &lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="ss"&gt;baz: &lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="n"&gt;h&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="ss"&gt;:bat&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt; &lt;span class="c1"&gt;# =&amp;gt; 3&lt;/span&gt;
&lt;span class="n"&gt;h&lt;/span&gt; &lt;span class="c1"&gt;# =&amp;gt; {:foo=&amp;gt;0, :bar=&amp;gt;1, :baz=&amp;gt;2, :bat=&amp;gt;3}&lt;/span&gt;

&lt;span class="n"&gt;h&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="ss"&gt;:foo&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;4&lt;/span&gt; &lt;span class="c1"&gt;# =&amp;gt; 4&lt;/span&gt;
&lt;span class="n"&gt;h&lt;/span&gt; &lt;span class="c1"&gt;# =&amp;gt; {:foo=&amp;gt;4, :bar=&amp;gt;1, :baz=&amp;gt;2, :bat=&amp;gt;3}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Back to the top&lt;/p&gt;

&lt;h2&gt;
  
  
  ✅ Values in a Ruby Hash
&lt;/h2&gt;

&lt;p&gt;Values can be any Ruby &lt;code&gt;Object&lt;/code&gt;. Including:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Strings&lt;/li&gt;
&lt;li&gt;Integers &amp;amp; Floats&lt;/li&gt;
&lt;li&gt;Arrays&lt;/li&gt;
&lt;/ul&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;note&lt;/strong&gt; Keys are unique, you can only have one &lt;code&gt;:foo&lt;/code&gt; key, or one &lt;code&gt;:bar&lt;/code&gt; key. When you add the same key twice, the latter will &lt;strong&gt;override&lt;/strong&gt; the former value.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Back to the top&lt;/p&gt;

&lt;h2&gt;
  
  
  ✅ Hash Entry Order
&lt;/h2&gt;

&lt;p&gt;A Hash object presents its entries in the &lt;strong&gt;order of their creation&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;A new Hash has its &lt;strong&gt;initial ordering&lt;/strong&gt; per the given entries:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight ruby"&gt;&lt;code&gt;&lt;span class="n"&gt;h&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="no"&gt;Hash&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="ss"&gt;foo: &lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="ss"&gt;bar: &lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
&lt;span class="n"&gt;h&lt;/span&gt; &lt;span class="c1"&gt;# =&amp;gt; {:foo=&amp;gt;0, :bar=&amp;gt;1}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;New entries are added at the end:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight ruby"&gt;&lt;code&gt;&lt;span class="n"&gt;h&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="ss"&gt;:baz&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;
&lt;span class="n"&gt;h&lt;/span&gt; &lt;span class="c1"&gt;# =&amp;gt; {:foo=&amp;gt;0, :bar=&amp;gt;1, :baz=&amp;gt;2}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Updating a value does not affect the order:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight ruby"&gt;&lt;code&gt;&lt;span class="n"&gt;h&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="ss"&gt;:baz&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;
&lt;span class="n"&gt;h&lt;/span&gt; &lt;span class="c1"&gt;# =&amp;gt; {:foo=&amp;gt;0, :bar=&amp;gt;1, :baz=&amp;gt;3}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;But re-creating a deleted entry can affect the order:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight ruby"&gt;&lt;code&gt;&lt;span class="n"&gt;h&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;delete&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="ss"&gt;:foo&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="n"&gt;h&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="ss"&gt;:foo&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;5&lt;/span&gt;
&lt;span class="n"&gt;h&lt;/span&gt; &lt;span class="c1"&gt;# =&amp;gt; {:bar=&amp;gt;1, :baz=&amp;gt;3, :foo=&amp;gt;5}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Back to the top&lt;/p&gt;

&lt;h2&gt;
  
  
  ✅ How to delete a Hash entry
&lt;/h2&gt;

&lt;p&gt;The simplest way is using instance method &lt;code&gt;delete&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight ruby"&gt;&lt;code&gt;&lt;span class="n"&gt;h&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="ss"&gt;foo: &lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="ss"&gt;bar: &lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="ss"&gt;baz: &lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="n"&gt;h&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;delete&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="ss"&gt;:bar&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="c1"&gt;# =&amp;gt; 1&lt;/span&gt;
&lt;span class="n"&gt;h&lt;/span&gt; &lt;span class="c1"&gt;# =&amp;gt; {:foo=&amp;gt;0, :baz=&amp;gt;2}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Back to the top&lt;/p&gt;

&lt;h2&gt;
  
  
  ✅ How to Access Values From a Hash
&lt;/h2&gt;

&lt;p&gt;Hash value/element are &lt;strong&gt;access by particular key&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;The simplest way to retrieve a Hash value is using instance method &lt;code&gt;[]&lt;/code&gt; :&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight ruby"&gt;&lt;code&gt;&lt;span class="n"&gt;h&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="ss"&gt;foo: &lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="ss"&gt;bar: &lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="ss"&gt;baz: &lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="n"&gt;h&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="ss"&gt;:foo&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="c1"&gt;# =&amp;gt; 0&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We also can use &lt;code&gt;fetch&lt;/code&gt; method. It does same as the square bracket lookup &lt;code&gt;[]&lt;/code&gt;, but it will raise an &lt;code&gt;error&lt;/code&gt; &lt;strong&gt;if the key is not defined&lt;/strong&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight ruby"&gt;&lt;code&gt;&lt;span class="err"&gt;$&lt;/span&gt; &lt;span class="n"&gt;irb&lt;/span&gt;
&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;dictionary&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="s2"&gt;"one"&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="s2"&gt;"satu"&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="c1"&gt;#Malay language 🇲🇾&lt;/span&gt;
&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;dictionary&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;fetch&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"one"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="s2"&gt;"satu"&lt;/span&gt;
&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;dictionary&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;fetch&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"two"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="no"&gt;KeyError&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;key&lt;/span&gt; &lt;span class="n"&gt;not&lt;/span&gt; &lt;span class="ss"&gt;found: &lt;/span&gt;&lt;span class="s2"&gt;"two"&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We can prevent this error using the default value.&lt;/p&gt;

&lt;p&gt;Back to the top&lt;/p&gt;

&lt;h2&gt;
  
  
  ✅ Extract a nested Hash value
&lt;/h2&gt;

&lt;p&gt;&lt;code&gt;dig&lt;/code&gt; is handy for nested &lt;code&gt;Hash&lt;/code&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight ruby"&gt;&lt;code&gt;&lt;span class="n"&gt;h&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="ss"&gt;foo: &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="ss"&gt;bar: &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="ss"&gt;baz: &lt;/span&gt;&lt;span class="mi"&gt;11&lt;/span&gt;&lt;span class="p"&gt;}}}&lt;/span&gt;

&lt;span class="n"&gt;h&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;dig&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="ss"&gt;:foo&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="ss"&gt;:bar&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="ss"&gt;:baz&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;   &lt;span class="c1"&gt;# =&amp;gt; 11&lt;/span&gt;
&lt;span class="n"&gt;h&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;dig&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="ss"&gt;:foo&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="ss"&gt;:zot&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="ss"&gt;:xyz&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;   &lt;span class="c1"&gt;# =&amp;gt; nil&lt;/span&gt;

&lt;span class="n"&gt;g&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="ss"&gt;foo: &lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;10&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;11&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;12&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="n"&gt;g&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;dig&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="ss"&gt;:foo&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;            &lt;span class="c1"&gt;# =&amp;gt; 11&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;note:&lt;/strong&gt; This is only for &lt;strong&gt;Ruby 2.3+&lt;/strong&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Back to the top&lt;/p&gt;

&lt;h2&gt;
  
  
  ✅ Hash Keys 101
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Hash Key Equivalence
&lt;/h3&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;From Documentation&lt;/strong&gt;: Two objects are treated as the same &lt;code&gt;hash&lt;/code&gt; key when their hash value is identical and the two objects are &lt;code&gt;eql?&lt;/code&gt; to each other.&lt;br&gt;
&lt;/p&gt;
&lt;/blockquote&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight ruby"&gt;&lt;code&gt;&lt;span class="n"&gt;irb&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;g&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="ss"&gt;foo: &lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="ss"&gt;:foo&lt;/span&gt;&lt;span class="o"&gt;=&amp;gt;&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="n"&gt;irb&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;h&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="ss"&gt;foo: &lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="ss"&gt;:foo&lt;/span&gt;&lt;span class="o"&gt;=&amp;gt;&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="n"&gt;irb&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;g&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;eql?&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;h&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="kp"&gt;true&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Modifying an Active Hash Key
&lt;/h3&gt;

&lt;p&gt;Modifying a Hash key while it is in use &lt;strong&gt;damages the hash index&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;This Hash has keys that are Arrays:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight ruby"&gt;&lt;code&gt;&lt;span class="n"&gt;a0&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt; &lt;span class="ss"&gt;:foo&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="ss"&gt;:bar&lt;/span&gt; &lt;span class="p"&gt;]&lt;/span&gt;
&lt;span class="n"&gt;a1&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt; &lt;span class="ss"&gt;:baz&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="ss"&gt;:bat&lt;/span&gt; &lt;span class="p"&gt;]&lt;/span&gt;
&lt;span class="n"&gt;h&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="n"&gt;a0&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;a1&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="n"&gt;h&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;include?&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;a0&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="c1"&gt;# =&amp;gt; true&lt;/span&gt;
&lt;span class="n"&gt;h&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;a0&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="c1"&gt;# =&amp;gt; 0&lt;/span&gt;
&lt;span class="n"&gt;a0&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;hash&lt;/span&gt; &lt;span class="c1"&gt;# =&amp;gt; 110002110&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Modifying array element &lt;code&gt;a0[0]&lt;/code&gt; changes its hash value:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight ruby"&gt;&lt;code&gt;&lt;span class="n"&gt;a0&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="ss"&gt;:bam&lt;/span&gt;
&lt;span class="n"&gt;a0&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;hash&lt;/span&gt; &lt;span class="c1"&gt;# =&amp;gt; 1069447059&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;And damages the Hash index:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight ruby"&gt;&lt;code&gt;&lt;span class="n"&gt;h&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;include?&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;a0&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="c1"&gt;# =&amp;gt; false&lt;/span&gt;
&lt;span class="n"&gt;h&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;a0&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="c1"&gt;# =&amp;gt; nil&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You can repair the hash index using method &lt;code&gt;rehash&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight ruby"&gt;&lt;code&gt;&lt;span class="n"&gt;h&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;rehash&lt;/span&gt; &lt;span class="c1"&gt;# =&amp;gt; {[:bam, :bar]=&amp;gt;0, [:baz, :bat]=&amp;gt;1}&lt;/span&gt;
&lt;span class="n"&gt;h&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;include?&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;a0&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="c1"&gt;# =&amp;gt; true&lt;/span&gt;
&lt;span class="n"&gt;h&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;a0&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="c1"&gt;# =&amp;gt; 0&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;A &lt;code&gt;String&lt;/code&gt; key is always safe. That's because an unfrozen &lt;code&gt;String&lt;/code&gt; passed as a key will be replaced by a duplicated and frozen String:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight ruby"&gt;&lt;code&gt;&lt;span class="n"&gt;s&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s1"&gt;'foo'&lt;/span&gt;
&lt;span class="n"&gt;s&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;frozen?&lt;/span&gt; &lt;span class="c1"&gt;# =&amp;gt; false&lt;/span&gt;
&lt;span class="n"&gt;h&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="n"&gt;s&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="n"&gt;first_key&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;h&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;keys&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;first&lt;/span&gt;
&lt;span class="n"&gt;first_key&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;frozen?&lt;/span&gt; &lt;span class="c1"&gt;# =&amp;gt; true&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Back to the top&lt;/p&gt;

&lt;h2&gt;
  
  
  ✅ Default values
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;By default&lt;/strong&gt;, accessing a key which has not been added to the hash returns &lt;code&gt;nil&lt;/code&gt;, meaning it is &lt;em&gt;always safe to attempt to look up a key's value&lt;/em&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight ruby"&gt;&lt;code&gt;&lt;span class="n"&gt;my_hash&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{}&lt;/span&gt;

&lt;span class="n"&gt;my_hash&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="ss"&gt;:name&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="c1"&gt;# =&amp;gt; nil&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Hashes can also contain keys in &lt;code&gt;strings&lt;/code&gt;. If you try to access them normally it will just return a &lt;code&gt;nil&lt;/code&gt;, instead you access them by their &lt;strong&gt;string keys&lt;/strong&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight ruby"&gt;&lt;code&gt;&lt;span class="n"&gt;my_hash&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="s2"&gt;"name"&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="s2"&gt;"asyraf"&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="n"&gt;my_hash&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="ss"&gt;:name&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;    &lt;span class="c1"&gt;# =&amp;gt; nil&lt;/span&gt;
&lt;span class="n"&gt;my_hash&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s2"&gt;"name"&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;   &lt;span class="c1"&gt;# =&amp;gt; asyraf&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You can retrieve the default value with method &lt;code&gt;default&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight ruby"&gt;&lt;code&gt;&lt;span class="n"&gt;h&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="no"&gt;Hash&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;new&lt;/span&gt;
&lt;span class="n"&gt;h&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;default&lt;/span&gt; &lt;span class="c1"&gt;# =&amp;gt; nil&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You can set the default value by passing an argument to method &lt;code&gt;Hash.new&lt;/code&gt; or with method &lt;code&gt;default=&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight ruby"&gt;&lt;code&gt;&lt;span class="n"&gt;h&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="no"&gt;Hash&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;new&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;100&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="n"&gt;h&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;default&lt;/span&gt; &lt;span class="c1"&gt;# =&amp;gt; 100&lt;/span&gt;

&lt;span class="n"&gt;h&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;default&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;99&lt;/span&gt;
&lt;span class="n"&gt;h&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;default&lt;/span&gt; &lt;span class="c1"&gt;# =&amp;gt; 99&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Back to the top&lt;/p&gt;

&lt;h2&gt;
  
  
  ✅ How to Merge Two Ruby Hashes
&lt;/h2&gt;

&lt;p&gt;I think you can guess the method name :p. Tadaaaa, we will use &lt;code&gt;merge&lt;/code&gt; method.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight ruby"&gt;&lt;code&gt;&lt;span class="n"&gt;defaults&lt;/span&gt;    &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="ss"&gt;a: &lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="ss"&gt;b: &lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="ss"&gt;c: &lt;/span&gt;&lt;span class="mi"&gt;3&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="n"&gt;preferences&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="ss"&gt;c: &lt;/span&gt;&lt;span class="mi"&gt;4&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="n"&gt;defaults&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;merge!&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;preferences&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="c1"&gt;# {:a=&amp;gt;1, :b=&amp;gt;2, :c=&amp;gt;4}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Notice that because &lt;strong&gt;keys are unique&lt;/strong&gt;, &lt;em&gt;newer values overwrite older values&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;Back to the top&lt;/p&gt;

&lt;h2&gt;
  
  
  ✅ Multiple Hash values for one key
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight ruby"&gt;&lt;code&gt;&lt;span class="no"&gt;Malaysia&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="ss"&gt;food: &lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;
    &lt;span class="s2"&gt;"Nasi Lemak"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="s2"&gt;"Roti Canai"&lt;/span&gt;
  &lt;span class="p"&gt;],&lt;/span&gt;
  &lt;span class="ss"&gt;city: &lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;
    &lt;span class="s2"&gt;"Kuala Lumpur"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="s2"&gt;"Malacca City"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="s2"&gt;"George Town"&lt;/span&gt;
  &lt;span class="p"&gt;]&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="no"&gt;Malaysia&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="ss"&gt;:city&lt;/span&gt;&lt;span class="p"&gt;][&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Where &lt;code&gt;Malaysia[:city]&lt;/code&gt; gives you an array &amp;amp; &lt;code&gt;[1]&lt;/code&gt; gives you the 2nd element from that array.&lt;/p&gt;

&lt;p&gt;The key is a symbol &amp;amp; the values are arrays. When you access the hash, you get an array back which you access normally, like any other array.&lt;/p&gt;

&lt;p&gt;Back to the top&lt;/p&gt;

&lt;h2&gt;
  
  
  ✅ Get All Keys and Values From a Hash
&lt;/h2&gt;

&lt;p&gt;If you want a list of all the keys, good news, there is a method for that!&lt;/p&gt;

&lt;p&gt;Here it is:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight ruby"&gt;&lt;code&gt;&lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="ss"&gt;foo: &lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="ss"&gt;bar: &lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt; &lt;span class="p"&gt;}.&lt;/span&gt;&lt;span class="nf"&gt;keys&lt;/span&gt;
&lt;span class="c1"&gt;# [:foo, :bar]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;There’s also a method for values:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight ruby"&gt;&lt;code&gt;&lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="ss"&gt;foo: &lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="ss"&gt;bar: &lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt; &lt;span class="p"&gt;}.&lt;/span&gt;&lt;span class="nf"&gt;values&lt;/span&gt;
&lt;span class="c1"&gt;# [1, 2]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Back to the top&lt;/p&gt;

&lt;h2&gt;
  
  
  ✅ Check if key exists in hash
&lt;/h2&gt;

&lt;p&gt;If you want to know if a key exists in a hash, use the &lt;code&gt;key?&lt;/code&gt; method.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight ruby"&gt;&lt;code&gt;&lt;span class="n"&gt;hash_data&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="s1"&gt;'Country'&lt;/span&gt;&lt;span class="o"&gt;=&amp;gt;&lt;/span&gt;&lt;span class="s2"&gt;"Malaysia"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="s1"&gt;'Food'&lt;/span&gt;&lt;span class="o"&gt;=&amp;gt;&lt;/span&gt;&lt;span class="s2"&gt;"Nasi Lemak"&lt;/span&gt;&lt;span class="p"&gt;,}&lt;/span&gt;
&lt;span class="nb"&gt;puts&lt;/span&gt; &lt;span class="n"&gt;hash_data&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;key?&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"Country"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="c1"&gt;#true&lt;/span&gt;
&lt;span class="nb"&gt;puts&lt;/span&gt; &lt;span class="n"&gt;hash_data&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;key?&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"Code"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="c1"&gt;#false&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Back to the top&lt;/p&gt;

&lt;h2&gt;
  
  
  ✅ How to change a Hash to Array
&lt;/h2&gt;

&lt;p&gt;We can do &lt;code&gt;Hash&lt;/code&gt; 🔁 &lt;code&gt;Array&lt;/code&gt;. Converting a hash of key/value pairs into an array will produce an array containing &lt;strong&gt;nested arrays for pair&lt;/strong&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight ruby"&gt;&lt;code&gt;&lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="ss"&gt;:a&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="ss"&gt;:b&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt; &lt;span class="p"&gt;}.&lt;/span&gt;&lt;span class="nf"&gt;to_a&lt;/span&gt; &lt;span class="c1"&gt;# =&amp;gt; [[:a, 1], [:b, 2]]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In the opposite direction a &lt;code&gt;Hash&lt;/code&gt; can be created from an &lt;code&gt;array&lt;/code&gt; of the same format:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight ruby"&gt;&lt;code&gt;&lt;span class="p"&gt;[[&lt;/span&gt;&lt;span class="ss"&gt;:x&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="ss"&gt;:y&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;4&lt;/span&gt;&lt;span class="p"&gt;]].&lt;/span&gt;&lt;span class="nf"&gt;to_h&lt;/span&gt; &lt;span class="c1"&gt;# =&amp;gt; { :x =&amp;gt; 3, :y =&amp;gt; 4 }&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Similarly, Hashes can be initialized using &lt;code&gt;Hash[]&lt;/code&gt; and a list of alternating keys and values:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight ruby"&gt;&lt;code&gt;&lt;span class="no"&gt;Hash&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="ss"&gt;:a&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="ss"&gt;:b&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="c1"&gt;# =&amp;gt; { :a =&amp;gt; 1, :b =&amp;gt; 2 }&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Or from an array of arrays with two values each:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight ruby"&gt;&lt;code&gt;&lt;span class="no"&gt;Hash&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt; &lt;span class="p"&gt;[[&lt;/span&gt;&lt;span class="ss"&gt;:x&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="ss"&gt;:y&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;4&lt;/span&gt;&lt;span class="p"&gt;]]&lt;/span&gt; &lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="c1"&gt;# =&amp;gt; { :x =&amp;gt; 3, :y =&amp;gt; 4 }&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Hashes can be converted back to an &lt;code&gt;Array&lt;/code&gt; of alternating keys and values using &lt;code&gt;flatten()&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight ruby"&gt;&lt;code&gt;&lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="ss"&gt;:a&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="ss"&gt;:b&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt; &lt;span class="p"&gt;}.&lt;/span&gt;&lt;span class="nf"&gt;flatten&lt;/span&gt; &lt;span class="c1"&gt;# =&amp;gt; [:a, 1, :b, 2]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Back to the top&lt;/p&gt;




&lt;p&gt;&lt;strong&gt;Super cool&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Pretty cool, hah... 😎&lt;/p&gt;

&lt;p&gt;There are other cool stuff on Ruby Hash. But as i said, this is just a level of most of people dive in. &lt;/p&gt;

&lt;p&gt;Maybe for specific tricks like Hash iteration, sorting and other methods for future articles :P.&lt;/p&gt;

&lt;p&gt;And probably this is my notes about Ruby Hash, I will add new stuff as I get new knowledge or experience with it.&lt;/p&gt;

&lt;p&gt;Thank you for traveling with me 🚀, I hope you enjoyed the journey 🔥.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;The End&lt;/strong&gt;&lt;/p&gt;




&lt;p&gt;Resources:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://www.rubyguides.com/2020/05/ruby-hash-methods/"&gt;1&lt;/a&gt;, &lt;a href="https://rubyapi.org/3.0/o/hash"&gt;2&lt;/a&gt;, &lt;a href="https://kalkicode.com/ruby/ruby-hash"&gt;3&lt;/a&gt;, &lt;a href="http://ruby-for-beginners.rubymonstas.org/built_in_classes/hashes.html"&gt;4&lt;/a&gt;, &lt;a href="https://riptutorial.com/ruby/topic/288/hashes"&gt;5&lt;/a&gt;, &lt;a href="https://outline.com/jTTW9U"&gt;6&lt;/a&gt;, &lt;a href="https://medium.com/epfl-extension-school/introduction-to-hashes-in-ruby-and-in-rails-be8ac5d4f58a"&gt;7&lt;/a&gt;&lt;/p&gt;

</description>
      <category>ruby</category>
      <category>rails</category>
      <category>datastructure</category>
      <category>beginners</category>
    </item>
    <item>
      <title>How to Change Link Underline Color with CSS</title>
      <dc:creator>Amirul Asyraf</dc:creator>
      <pubDate>Thu, 24 Jun 2021 04:05:54 +0000</pubDate>
      <link>https://dev.to/asyraf/how-to-change-link-underline-color-with-css-m47</link>
      <guid>https://dev.to/asyraf/how-to-change-link-underline-color-with-css-m47</guid>
      <description>&lt;p&gt;Last night, when I look at &lt;a href="https://basecamp.com/" rel="noopener noreferrer"&gt;Basecamp&lt;/a&gt; website, I notice an &lt;strong&gt;interesting CSS stuff&lt;/strong&gt;. &lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn.hashnode.com%2Fres%2Fhashnode%2Fimage%2Fupload%2Fv1624435166693%2FiKMko6MUQ.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn.hashnode.com%2Fres%2Fhashnode%2Fimage%2Fupload%2Fv1624435166693%2FiKMko6MUQ.gif" alt="ezgif-2-5893d35b0bcf.gif"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;When I hover the &lt;code&gt;Nav&lt;/code&gt; link at the header, there will be a nice &lt;strong&gt;yellow highlight&lt;/strong&gt; appear &lt;em&gt;below the link&lt;/em&gt;. This is cool, tho .&lt;/p&gt;

&lt;p&gt;When I inspect the &lt;code&gt;a&lt;/code&gt; tag, they used &lt;code&gt;text-decoration&lt;/code&gt; stuff.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn.hashnode.com%2Fres%2Fhashnode%2Fimage%2Fupload%2Fv1624435577478%2Fjp_mUzv8w.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn.hashnode.com%2Fres%2Fhashnode%2Fimage%2Fupload%2Fv1624435577478%2Fjp_mUzv8w.png" alt="Screenshot 2021-06-23 at 16.06.11.png"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;I do a quick research and found this awesome, simple CSS magic to change the underline color. &lt;/p&gt;

&lt;h3&gt;
  
  
  I already do the research, you just need to grab the popcorn 🍿 and enjoy the journey 🚀.
&lt;/h3&gt;




&lt;p&gt;By default, the &lt;strong&gt;color of the underline is set the same as the text color of the link&lt;/strong&gt;. If the link is blue 🟦, the underline also a blue color 🟦. But, you know right, because of &lt;em&gt;human's creativity&lt;/em&gt;(cough, cough...)😆, sometimes we want to give the underline a different color from the text of the link. &lt;/p&gt;

&lt;p&gt;There are two ways I found to make this happen. One is using the &lt;code&gt;text-decoration&lt;/code&gt; stuff, the second one is using the old &lt;code&gt;border-bottom&lt;/code&gt; trick.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;personally, I prefer &lt;code&gt;text-decoration&lt;/code&gt; over &lt;code&gt;border-bottom&lt;/code&gt;. The right tool for the right job. I donno.&lt;/p&gt;
&lt;/blockquote&gt;




&lt;h1&gt;
  
  
  ✅ &lt;strong&gt;&lt;code&gt;text-decoration&lt;/code&gt;&lt;/strong&gt;
&lt;/h1&gt;

&lt;p&gt;&lt;code&gt;text-decoration&lt;/code&gt; is the most straightforward way to underline text.&lt;/p&gt;

&lt;h3&gt;
  
  
  &lt;code&gt;text-decoration-color&lt;/code&gt; is what we needed
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight css"&gt;&lt;code&gt;&lt;span class="nt"&gt;a&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nl"&gt;text-decoration&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;none&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="nl"&gt;color&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="no"&gt;blue&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="c"&gt;/* show underline on hover */&lt;/span&gt;
&lt;span class="nt"&gt;a&lt;/span&gt;&lt;span class="nd"&gt;:hover&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nl"&gt;text-decoration&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;underline&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="nl"&gt;text-decoration-color&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="no"&gt;red&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="nl"&gt;-webkit-text-decoration-color&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="no"&gt;red&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;note:&lt;/strong&gt; &lt;code&gt;-webkit&lt;/code&gt; is for Safari.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;&lt;iframe height="600" src="https://codepen.io/asyrafff/embed/OJpeaQK?height=600&amp;amp;default-tab=result&amp;amp;embed-version=2"&gt;
&lt;/iframe&gt;
&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;This is the only thing you needed. If you want to customize more, read more :P&lt;/p&gt;
&lt;/blockquote&gt;




&lt;h1&gt;
  
  
  🌈 &lt;strong&gt;&lt;code&gt;text-decoration&lt;/code&gt;&lt;/strong&gt; properties
&lt;/h1&gt;

&lt;p&gt;&lt;code&gt;text-decoration&lt;/code&gt; works fine by itself, but you can add a few properties to customize the way it looks:&lt;/p&gt;

&lt;h3&gt;
  
  
  1) &lt;strong&gt;&lt;code&gt;text-decoration-color&lt;/code&gt;&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;&lt;code&gt;text-decoration-color&lt;/code&gt; lets you &lt;strong&gt;change an underline’s color&lt;/strong&gt; separately from its text color.&lt;/p&gt;

&lt;p&gt;&lt;iframe height="600" src="https://codepen.io/asyrafff/embed/PoprXbZ?height=600&amp;amp;default-tab=result&amp;amp;embed-version=2"&gt;
&lt;/iframe&gt;
&lt;/p&gt;

&lt;h3&gt;
  
  
  2- &lt;strong&gt;&lt;code&gt;text-decoration-skip&lt;/code&gt;&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;This property will &lt;strong&gt;skip the edgy alphabet&lt;/strong&gt; like &lt;code&gt;g&lt;/code&gt;, &lt;code&gt;p&lt;/code&gt;, &lt;code&gt;f&lt;/code&gt;. It didn't go through it. Examples :&lt;/p&gt;

&lt;p&gt;&lt;iframe height="600" src="https://codepen.io/asyrafff/embed/abJgPpQ?height=600&amp;amp;default-tab=result&amp;amp;embed-version=2"&gt;
&lt;/iframe&gt;
&lt;/p&gt;

&lt;h3&gt;
  
  
  3- &lt;strong&gt;&lt;code&gt;text-decoration-style&lt;/code&gt;&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;&lt;code&gt;text-decoration-style&lt;/code&gt; give you a &lt;strong&gt;free underline design using different values&lt;/strong&gt;. No need to add SVG.&lt;/p&gt;

&lt;p&gt;Here are the available values that you can use:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;code&gt;dashed&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;dotted&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;double&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;solid&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;wavy&lt;/code&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;iframe height="600" src="https://codepen.io/asyrafff/embed/xxqomOQ?height=600&amp;amp;default-tab=result&amp;amp;embed-version=2"&gt;
&lt;/iframe&gt;
&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;note&lt;/strong&gt;: This example is from Mozilla MDN. You also can seperate the &lt;code&gt;text-decoration&lt;/code&gt; into &lt;code&gt;text-decoration-color&lt;/code&gt; and &lt;code&gt;text-decoration-style&lt;/code&gt;.&lt;br&gt;
&lt;/p&gt;
&lt;/blockquote&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight css"&gt;&lt;code&gt;&lt;span class="nt"&gt;a&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
 &lt;span class="nl"&gt;text-decoration&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;underline&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
 &lt;span class="nl"&gt;text-decoration-color&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="no"&gt;red&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
 &lt;span class="nl"&gt;text-decoration-style&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;wavy&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  4- &lt;strong&gt;&lt;code&gt;text-underline-offset&lt;/code&gt;&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;This is quite cool. It can be used to &lt;strong&gt;define how far the line is from the initial text.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;iframe height="600" src="https://codepen.io/asyrafff/embed/gOmNQyY?height=600&amp;amp;default-tab=result&amp;amp;embed-version=2"&gt;
&lt;/iframe&gt;
&lt;/p&gt;

&lt;h3&gt;
  
  
  5- &lt;strong&gt;&lt;code&gt;text-underline-thickness&lt;/code&gt;&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;This property is used to tell &lt;strong&gt;how big the underline is&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;&lt;iframe height="600" src="https://codepen.io/asyrafff/embed/xxqomKa?height=600&amp;amp;default-tab=result&amp;amp;embed-version=2"&gt;
&lt;/iframe&gt;
&lt;/p&gt;




&lt;h1&gt;
  
  
  ✅ &lt;strong&gt;&lt;code&gt;border-bottom&lt;/code&gt;&lt;/strong&gt;
&lt;/h1&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;disclaimer&lt;/strong&gt;: if you happy with the first trick, just go with it. This is just another same trick.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;If we want to use &lt;code&gt;border-bottom&lt;/code&gt; trick, first we need to &lt;strong&gt;remove the underline&lt;/strong&gt; with the &lt;code&gt;text-decoration&lt;/code&gt; property value of &lt;code&gt;none&lt;/code&gt;. Then we add the &lt;code&gt;border-bottom&lt;/code&gt; property with &lt;em&gt;3 short-hand CSS values&lt;/em&gt; of &lt;code&gt;3px solid red&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;3px&lt;/code&gt; = &lt;strong&gt;Variable of the underline width in pixels&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;code&gt;solid&lt;/code&gt; = &lt;strong&gt;Border style&lt;/strong&gt; (solid, dotted, or dashed)&lt;/p&gt;

&lt;p&gt;&lt;code&gt;red&lt;/code&gt; = &lt;strong&gt;Color code&lt;/strong&gt;. Hex also can, like &lt;code&gt;#999999&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;iframe height="600" src="https://codepen.io/asyrafff/embed/abJgQjq?height=600&amp;amp;default-tab=result&amp;amp;embed-version=2"&gt;
&lt;/iframe&gt;
&lt;/p&gt;




&lt;h3&gt;
  
  
  I add this cool trick into my blog
&lt;/h3&gt;

&lt;p&gt;I also add this awesome tricks into my own &lt;a href="https://asyrafff.com/" rel="noopener noreferrer"&gt;blog&lt;/a&gt;. I implement it on every &lt;code&gt;a&lt;/code&gt; tag or any link-related tag.&lt;/p&gt;

&lt;p&gt;Pretty cool, hah... 😎&lt;/p&gt;

&lt;p&gt;Thank you for traveling with me 🚀, I hope you enjoyed the journey 🔥.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;The End&lt;/strong&gt;&lt;/p&gt;




&lt;p&gt;resources:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://usefulangle.com/post/295/css-change-link-underline-color" rel="noopener noreferrer"&gt;1&lt;/a&gt; &lt;a href="https://www.w3docs.com/snippets/css/how-to-change-link-colors-in-html.html" rel="noopener noreferrer"&gt;2&lt;/a&gt; &lt;a href="https://stackoverflow.com/questions/1175383/changing-a-link-underline-color" rel="noopener noreferrer"&gt;3&lt;/a&gt; &lt;a href="http://hyperlinkcode.com/change-underline-color.php" rel="noopener noreferrer"&gt;4&lt;/a&gt; &lt;a href="https://css-tricks.com/styling-underlines-web/" rel="noopener noreferrer"&gt;5&lt;/a&gt; &lt;a href="https://daily-dev-tips.com/posts/css-styling-the-link-underline/" rel="noopener noreferrer"&gt;6&lt;/a&gt; &lt;a href="https://medium.com/visualmodo/how-to-change-link-underlines-on-a-webpage-12e438269e9a" rel="noopener noreferrer"&gt;7&lt;/a&gt; &lt;a href="https://css-tricks.com/almanac/properties/t/text-decoration-color/" rel="noopener noreferrer"&gt;8&lt;/a&gt;&lt;/p&gt;

</description>
      <category>css</category>
      <category>html</category>
      <category>howto</category>
      <category>codepen</category>
    </item>
    <item>
      <title>How to add Video in README.md file?</title>
      <dc:creator>Amirul Asyraf</dc:creator>
      <pubDate>Mon, 19 Apr 2021 23:24:10 +0000</pubDate>
      <link>https://dev.to/asyraf/how-to-add-video-in-readme-md-file-1him</link>
      <guid>https://dev.to/asyraf/how-to-add-video-in-readme-md-file-1him</guid>
      <description>&lt;p&gt;Recently, the CEO of Github, &lt;a href="https://twitter.com/natfriedman" rel="noopener noreferrer"&gt;Nat Friedman&lt;/a&gt; share some &lt;a href="https://twitter.com/natfriedman/status/1383169103028908034" rel="noopener noreferrer"&gt;small improvements&lt;/a&gt; made to Github that hopefully improve our life just a little bit.&lt;/p&gt;

&lt;p&gt;There are 5 cool improvements, but I want to highlight one that is even cooler 😎.&lt;/p&gt;

&lt;p&gt;So, what is it ??&lt;/p&gt;




&lt;h2&gt;
  
  
  Video files that you drop onto markdown files are now &lt;code&gt;automatically embedded&lt;/code&gt;! 🤘
&lt;/h2&gt;

&lt;p&gt;To me, it is cool tho. You just drag your video file on your laptop or pc to the markdown file in Github. Done.&lt;/p&gt;

&lt;p&gt;Easy peasy.&lt;/p&gt;

&lt;p&gt;Before this, I can embed the video either change it to the gif or create a new file and reference it to the README file.&lt;/p&gt;

&lt;p&gt;Thanks, Github 🚀.&lt;/p&gt;

&lt;p&gt;If you try it, the result will be like this;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.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%2Fyebcdj8k5k3mwidsg45c.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.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%2Fyebcdj8k5k3mwidsg45c.png" alt="Screenshot 2021-04-20 at 07.15.42"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;I created this &lt;a href="https://github.com/asyraffff/readme-with-video" rel="noopener noreferrer"&gt;repo&lt;/a&gt; as an example so you can look at the raw or markdown file.&lt;/p&gt;

&lt;p&gt;The end :p&lt;/p&gt;




&lt;p&gt;Follow me on &lt;a href="https://twitter.com/asyr0f" rel="noopener noreferrer"&gt;Twitter&lt;/a&gt; 😉 &lt;/p&gt;

</description>
      <category>github</category>
      <category>markdown</category>
      <category>howto</category>
    </item>
    <item>
      <title>How to add Dropdown in Markdown ??</title>
      <dc:creator>Amirul Asyraf</dc:creator>
      <pubDate>Tue, 23 Mar 2021 01:09:58 +0000</pubDate>
      <link>https://dev.to/asyraf/how-to-add-dropdown-in-markdown-o78</link>
      <guid>https://dev.to/asyraf/how-to-add-dropdown-in-markdown-o78</guid>
      <description>&lt;p&gt;Ever wonder how most of the project's README file does this stuff ??&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.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%2Fvbv4wqeo8gyv9ig1zday.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.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%2Fvbv4wqeo8gyv9ig1zday.png" alt="Screenshot 2021-03-22 at 07.19.18"&gt;&lt;/a&gt;&lt;br&gt;
&lt;strong&gt;When we click, it will open the dropdown.&lt;/strong&gt;&lt;br&gt;
 &lt;a href="https://media.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%2Fbprovnuf2ik8d7ooodab.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.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%2Fbprovnuf2ik8d7ooodab.png" alt="Screenshot 2021-03-22 at 07.19.51"&gt;&lt;/a&gt;&lt;/p&gt;




&lt;p&gt;Cool, let's learn that stuff. Btw, the syntax is so freaking easy 😂.&lt;/p&gt;

&lt;h1&gt;
  
  
  Code
&lt;/h1&gt;

&lt;p&gt;Ok, now copy this code;&lt;/p&gt;

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

&lt;span class="nt"&gt;&amp;lt;details&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;summary&amp;gt;&lt;/span&gt;How do I dropdown?&lt;span class="nt"&gt;&amp;lt;/summary&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;br&amp;gt;&lt;/span&gt;
This is how you dropdown.
&lt;span class="nt"&gt;&amp;lt;/details&amp;gt;&lt;/span&gt;&lt;span class="sb"&gt;


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

&lt;/div&gt;

&lt;p&gt;To give it a try, use this &lt;a href="https://markdown-editor.github.io/" rel="noopener noreferrer"&gt;online markdown editor&lt;/a&gt;. Then, come back to this post for more information 😊.&lt;/p&gt;




&lt;h3&gt;
  
  
  So far so good ??
&lt;/h3&gt;

&lt;p&gt;Easy right. The basic syntax is just;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;&amp;lt;details&amp;gt;&lt;/code&gt; for the dropdown &lt;strong&gt;wrapper&lt;/strong&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;&amp;lt;summary&amp;gt;&lt;/code&gt; for the dropdown &lt;strong&gt;title&lt;/strong&gt;
&lt;/li&gt;
&lt;li&gt;The dropdown &lt;strong&gt;contents&lt;/strong&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Here are another tips 👇👇👇&lt;/strong&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  1) How to start with the dropdown shown instead of close?
&lt;/h2&gt;

&lt;p&gt;This one is simple. You just need to add &lt;code&gt;open&lt;/code&gt; after the &lt;code&gt;details&lt;/code&gt; word. For example;&lt;/p&gt;

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

&lt;span class="nt"&gt;&amp;lt;details&lt;/span&gt; &lt;span class="na"&gt;open&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;summary&amp;gt;&lt;/span&gt;I automatically open&lt;span class="nt"&gt;&amp;lt;/summary&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;br&amp;gt;&lt;/span&gt;
Waaa, you see me. I thought I would be hidden ;p .
&lt;span class="nt"&gt;&amp;lt;/details&amp;gt;&lt;/span&gt;&lt;span class="sb"&gt;


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

&lt;/div&gt;

&lt;p&gt;It will automatically open the dropdown.&lt;/p&gt;

&lt;h2&gt;
  
  
  2) You do it wrong !!!
&lt;/h2&gt;

&lt;p&gt;If you try this code;&lt;/p&gt;

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

&lt;span class="nt"&gt;&amp;lt;details&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;summary&amp;gt;&lt;/span&gt;Heading&lt;span class="nt"&gt;&amp;lt;/summary&amp;gt;&lt;/span&gt;
&lt;span class="p"&gt;    +&lt;/span&gt; markdown list 1
&lt;span class="p"&gt;        +&lt;/span&gt; nested list 1
&lt;span class="p"&gt;        +&lt;/span&gt; nested list 2
&lt;span class="p"&gt;    +&lt;/span&gt; markdown list 2
&lt;span class="nt"&gt;&amp;lt;/details&amp;gt;&lt;/span&gt;&lt;span class="sb"&gt;


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

&lt;/div&gt;

&lt;p&gt;&lt;a href="https://media.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%2Ftocd58ydp80qw31eoabi.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.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%2Ftocd58ydp80qw31eoabi.png" alt="Screenshot 2021-03-22 at 19.52.40"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;You will get a weird result. Not what you expected. So, you instead do it in HTML syntax.&lt;/p&gt;

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

&lt;span class="nt"&gt;&amp;lt;details&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;summary&amp;gt;&lt;/span&gt;Heading&lt;span class="nt"&gt;&amp;lt;/summary&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;ul&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;li&amp;gt;&lt;/span&gt; markdown list 1&lt;span class="nt"&gt;&amp;lt;/li&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;ul&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;li&amp;gt;&lt;/span&gt; nested list 1&lt;span class="nt"&gt;&amp;lt;/li&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;li&amp;gt;&lt;/span&gt; nested list 2&lt;span class="nt"&gt;&amp;lt;/li&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;/ul&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;li&amp;gt;&lt;/span&gt; markdown list 2&lt;span class="nt"&gt;&amp;lt;/li&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;/ul&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;/details&amp;gt;&lt;/span&gt;


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

&lt;/div&gt;

&lt;p&gt;It works but I just want to tell you that you do it wrongly 😂. You just need to add &lt;strong&gt;space&lt;/strong&gt; between the &lt;code&gt;&amp;lt;summary&amp;gt;&lt;/code&gt; tag with the dropdown content. No need to add &lt;code&gt;HTML&lt;/code&gt; stuff. Like this;&lt;/p&gt;

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

&lt;span class="nt"&gt;&amp;lt;details&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;summary&amp;gt;&lt;/span&gt;Heading&lt;span class="nt"&gt;&amp;lt;/summary&amp;gt;&lt;/span&gt;
&lt;span class="c"&gt;&amp;lt;!--All you need is a blank line--&amp;gt;&lt;/span&gt;&lt;span class="sb"&gt;

    + markdown list 1
        + nested list 1
        + nested list 2
    + markdown list 2
&lt;/span&gt;&lt;span class="nt"&gt;&amp;lt;/details&amp;gt;&lt;/span&gt;&lt;span class="sb"&gt;


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

&lt;/div&gt;

&lt;p&gt;😉&lt;/p&gt;

&lt;h2&gt;
  
  
  3) You also can nested the dropdown 🔥
&lt;/h2&gt;

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

&lt;span class="nt"&gt;&amp;lt;details&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;summary&amp;gt;&lt;/span&gt;Well&lt;span class="nt"&gt;&amp;lt;/summary&amp;gt;&lt;/span&gt;

&lt;span class="nt"&gt;&amp;lt;details&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;summary&amp;gt;&lt;/span&gt;Try this&lt;span class="nt"&gt;&amp;lt;/summary&amp;gt;&lt;/span&gt;

 &lt;span class="nt"&gt;&amp;lt;details&amp;gt;&lt;/span&gt;
 &lt;span class="nt"&gt;&amp;lt;summary&amp;gt;&lt;/span&gt;The other one&lt;span class="nt"&gt;&amp;lt;/summary&amp;gt;&lt;/span&gt;

   &lt;span class="nt"&gt;&amp;lt;details&amp;gt;&lt;/span&gt;
   &lt;span class="nt"&gt;&amp;lt;summary&amp;gt;&lt;/span&gt;Ok, try this&lt;span class="nt"&gt;&amp;lt;/summary&amp;gt;&lt;/span&gt;
   You got me 😂
   &lt;span class="nt"&gt;&amp;lt;/details&amp;gt;&lt;/span&gt;
 &lt;span class="nt"&gt;&amp;lt;/details&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;/details&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;/details&amp;gt;&lt;/span&gt;&lt;span class="sb"&gt;


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

&lt;/div&gt;
&lt;h2&gt;
  
  
  4) Add Code into the dropdown 🚀
&lt;/h2&gt;

&lt;p&gt;Same as the usual markdown.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.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%2Fbtqe3cqma1t7sqz1atgq.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.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%2Fbtqe3cqma1t7sqz1atgq.png" alt="Screenshot 2021-03-22 at 16.35.13"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;The above image will get this;&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.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%2Fy915hrimq1erz7b2vear.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.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%2Fy915hrimq1erz7b2vear.png" alt="Screenshot 2021-03-22 at 16.35.31"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h2&gt;
  
  
  5) Make a &lt;code&gt;Collapsible Content&lt;/code&gt;
&lt;/h2&gt;

&lt;p&gt;The way we want to do this is a little bit different. This is the result;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.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%2F6ta0fuuuf1fuz1rinh8n.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.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%2F6ta0fuuuf1fuz1rinh8n.png" alt="Screenshot 2021-03-23 at 08.56.58"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;When we click it&lt;/strong&gt;, the collapsible content is superb ;p&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.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%2Feqn7lmlc17t7apk8ohsb.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.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%2Feqn7lmlc17t7apk8ohsb.png" alt="Screenshot 2021-03-23 at 08.57.18"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;You can see this &lt;a href="https://gist.github.com/ImminentFate/931bd780de7fb2aecc376e7af446c5df" rel="noopener noreferrer"&gt;gist&lt;/a&gt; to look at the code.&lt;/p&gt;
&lt;h2&gt;
  
  
  6) Add Bullet list into the Dropdown
&lt;/h2&gt;

&lt;p&gt;This is easy. YOu just add &lt;code&gt;+&lt;/code&gt; before the list to make a bullet list.&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight markdown"&gt;&lt;code&gt;

&lt;span class="nt"&gt;&amp;lt;details&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;summary&amp;gt;&lt;/span&gt;Heading1&lt;span class="nt"&gt;&amp;lt;/summary&amp;gt;&lt;/span&gt;

some text
&lt;span class="p"&gt;+&lt;/span&gt; &lt;span class="nt"&gt;&amp;lt;details&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;summary&amp;gt;&lt;/span&gt;Heading1.1&lt;span class="nt"&gt;&amp;lt;/summary&amp;gt;&lt;/span&gt;&lt;span class="sb"&gt;

    some more text
    + &amp;lt;details&amp;gt;
        &amp;lt;summary&amp;gt;Heading1.1.1&amp;lt;/summary&amp;gt;
        even more text
      &amp;lt;/details&amp;gt;
&lt;/span&gt;   &lt;span class="nt"&gt;&amp;lt;/details&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;/details&amp;gt;&lt;/span&gt;&lt;span class="sb"&gt;


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

&lt;/div&gt;

&lt;p&gt;The result will be like this;&lt;br&gt;
&lt;a href="https://media.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%2F6qdhg5ahrd8uebly2ce7.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.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%2F6qdhg5ahrd8uebly2ce7.png" alt="Screenshot 2021-03-23 at 09.02.24"&gt;&lt;/a&gt;&lt;/p&gt;




&lt;h1&gt;
  
  
  Use Case
&lt;/h1&gt;

&lt;p&gt;Recently, I create an awesome-list repository about &lt;a href="http://hotwire.dev/" rel="noopener noreferrer"&gt;Hotwire&lt;/a&gt; stuff. YOu can see the project &lt;a href="https://github.com/asyraffff/Hotwire-in-action" rel="noopener noreferrer"&gt;here&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  1) Auto open dropdown
&lt;/h3&gt;

&lt;p&gt;I use the auto-open dropdown to show the &lt;strong&gt;Hotwire&lt;/strong&gt; stack.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.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%2Fohz9xkjozibasbsh2476.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.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%2Fohz9xkjozibasbsh2476.png" alt="Screenshot 2021-03-23 at 09.04.07"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  2) Long list
&lt;/h3&gt;

&lt;p&gt;The second one is for showing the long list of Websites, Open-Source, and Resources. I just want to make my README clean. Try to open it ;p.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.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%2Fv00x4xgta3rm2nxmzj2x.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.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%2Fv00x4xgta3rm2nxmzj2x.png" alt="Screenshot 2021-03-23 at 09.07.07"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;The End&lt;/strong&gt;&lt;/p&gt;




&lt;p&gt;resources;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://gist.github.com/citrusui/07978f14b11adada364ff901e27c7f61" rel="noopener noreferrer"&gt;1&lt;/a&gt; &lt;a href="https://chrisfrew.in/blog/dropdowns-in-readmes/" rel="noopener noreferrer"&gt;2&lt;/a&gt; &lt;a href="https://github.com/tchapi/markdown-cheatsheet/blob/master/README.md" rel="noopener noreferrer"&gt;3&lt;/a&gt; &lt;a href="https://gist.github.com/joyrexus/16041f2426450e73f5df9391f7f7ae5f" rel="noopener noreferrer"&gt;4&lt;/a&gt; &lt;a href="https://github.com/asyraffff/Hotwire-in-action" rel="noopener noreferrer"&gt;5&lt;/a&gt;&lt;/p&gt;

</description>
      <category>markdown</category>
      <category>howto</category>
      <category>html</category>
      <category>beginners</category>
    </item>
    <item>
      <title>How to add Caption to your HTML Image Tag ??</title>
      <dc:creator>Amirul Asyraf</dc:creator>
      <pubDate>Wed, 17 Mar 2021 00:48:16 +0000</pubDate>
      <link>https://dev.to/asyraf/how-to-add-caption-to-your-html-image-tag-1p5j</link>
      <guid>https://dev.to/asyraf/how-to-add-caption-to-your-html-image-tag-1p5j</guid>
      <description>&lt;p&gt;Today I learned that HTML has a specific Tag that can help us doing caption stuff on our &lt;code&gt;&amp;lt;img&amp;gt;&lt;/code&gt; tag.&lt;/p&gt;

&lt;p&gt;Usually, I will do caption like this;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight html"&gt;&lt;code&gt;&lt;span class="nt"&gt;&amp;lt;div&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;img&lt;/span&gt; &lt;span class="na"&gt;src=&lt;/span&gt;&lt;span class="s"&gt;"https://wallpapercave.com/wp/wp6761049.png"&lt;/span&gt;
       &lt;span class="na"&gt;alt=&lt;/span&gt;&lt;span class="s"&gt;"BigSur wallpaper"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;center&amp;gt;&lt;/span&gt;MacOS BigSur Walpaper&lt;span class="nt"&gt;&amp;lt;/center&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;/div&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fwallpapercave.com%2Fwp%2Fwp6761049.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fwallpapercave.com%2Fwp%2Fwp6761049.png" alt="BigSur wallpaper"&gt;&lt;/a&gt;&lt;br&gt;
  &lt;/p&gt;
&lt;center&gt;MacOS BigSur Walpaper&lt;/center&gt;




&lt;p&gt;This is ok tho. The caption is there below the image. But there is a problem here: &lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;there is nothing that semantically links the image to its caption, which can cause problems for screen readers. &lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;For example, when you have 50 images and captions, which caption goes with which image? &lt;/p&gt;

&lt;p&gt;HTML has a special tag for this purpose. Introducing to you &lt;a href="https://developer.mozilla.org/en-US/docs/Web/HTML/Element/figure" rel="noopener noreferrer"&gt;&lt;code&gt;&amp;lt;figure&amp;gt;&lt;/code&gt;&lt;/a&gt; and &lt;a href="https://developer.mozilla.org/en-US/docs/Web/HTML/Element/figcaption" rel="noopener noreferrer"&gt;&lt;code&gt;&amp;lt;figcaption&amp;gt;&lt;/code&gt;&lt;/a&gt; tag 😃. These are created for exactly this purpose: &lt;strong&gt;to provide a semantic container for figures, and to clearly link the figure to the caption.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Cool. So, we can rewrite our above example like this;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight html"&gt;&lt;code&gt;&lt;span class="nt"&gt;&amp;lt;figure&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;img&lt;/span&gt; &lt;span class="na"&gt;src=&lt;/span&gt;&lt;span class="s"&gt;"https://wallpapercave.com/wp/wp6761049.png"&lt;/span&gt;
       &lt;span class="na"&gt;alt=&lt;/span&gt;&lt;span class="s"&gt;"BigSur wallpaper"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;figcaption&amp;gt;&lt;/span&gt;MacOS BigSur Walpaper&lt;span class="nt"&gt;&amp;lt;/figcaption&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;/figure&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fwallpapercave.com%2Fwp%2Fwp6761049.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fwallpapercave.com%2Fwp%2Fwp6761049.png" alt="BigSur wallpaper"&gt;&lt;/a&gt;&lt;br&gt;MacOS BigSur Walpaper
  &lt;/p&gt;




&lt;p&gt;Super cool 🚀. Another thing that I learned is it doesn't have to be an image. A figure could be &lt;code&gt;several images&lt;/code&gt;, a &lt;code&gt;code snippet&lt;/code&gt;, &lt;code&gt;audio&lt;/code&gt;, &lt;code&gt;video&lt;/code&gt;, &lt;code&gt;equations&lt;/code&gt;, a &lt;code&gt;table&lt;/code&gt;, or something else.&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;code&gt;Code Snippet&lt;/code&gt;
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight html"&gt;&lt;code&gt;&lt;span class="nt"&gt;&amp;lt;figure&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;pre&amp;gt;&lt;/span&gt;
class PrivateData
  def initialize
    @secret_key = [1,79,427].sample
  end
end

data = PrivateData.new
puts "Can I have your secret_key? #{data.secret_key rescue 'nope'}"

puts "BUT I WANTZ IT!!!!"

puts data.instance_variable_get(:@secret_key)
puts "MUHAHAHAHAHA"
  &lt;span class="nt"&gt;&amp;lt;/pre&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;figcaption&amp;gt;&lt;/span&gt;instance variables get&lt;span class="nt"&gt;&amp;lt;/figcaption&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;/figure&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;




&lt;pre&gt;&lt;br&gt;
class PrivateData&lt;br&gt;
  def initialize&lt;br&gt;
    @secret_key = [1,79,427].sample&lt;br&gt;
  end&lt;br&gt;
end

&lt;p&gt;data = PrivateData.new&lt;br&gt;
puts "Can I have your secret_key? #{data.secret_key rescue 'nope'}"&lt;/p&gt;

&lt;p&gt;puts "BUT I WANTZ IT!!!!"&lt;/p&gt;

&lt;p&gt;puts data.instance_variable_get(:@secret_key)&lt;br&gt;
puts "MUHAHAHAHAHA"&lt;br&gt;
  &lt;/p&gt;&lt;/pre&gt;
&lt;br&gt;instance variables get
  

&lt;h2&gt;
  
  
  Quotations
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight html"&gt;&lt;code&gt;&lt;span class="nt"&gt;&amp;lt;figure&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;figcaption&amp;gt;&amp;lt;cite&amp;gt;&lt;/span&gt;Edsger Dijkstra:&lt;span class="nt"&gt;&amp;lt;/cite&amp;gt;&amp;lt;/figcaption&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;blockquote&amp;gt;&lt;/span&gt;If debugging is the process of removing software bugs,
  then programming must be the process of putting them in.&lt;span class="nt"&gt;&amp;lt;/blockquote&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;/figure&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;cite&gt;Edsger Dijkstra:&lt;/cite&gt;&lt;br&gt;
  &lt;/p&gt;
&lt;blockquote&gt;If debugging is the process of removing software bugs,&lt;br&gt;
  then programming must be the process of putting them in.&lt;/blockquote&gt;




&lt;p&gt;resources;&lt;br&gt;
&lt;a href="https://developer.mozilla.org/en-US/docs/Web/HTML/Element/figure" rel="noopener noreferrer"&gt;1&lt;/a&gt; &lt;a href="https://developer.mozilla.org/en-US/docs/Web/HTML/Element/pre" rel="noopener noreferrer"&gt;2&lt;/a&gt; &lt;a href="https://html.spec.whatwg.org/multipage/grouping-content.html#the-pre-element" rel="noopener noreferrer"&gt;3&lt;/a&gt; &lt;a href="https://stackoverflow.com/questions/10128950/how-to-write-a-caption-under-an-image" rel="noopener noreferrer"&gt;4&lt;/a&gt;&lt;/p&gt;

</description>
      <category>html</category>
      <category>tutorial</category>
      <category>css</category>
      <category>howto</category>
    </item>
    <item>
      <title>How to add a link to the image in HTML</title>
      <dc:creator>Amirul Asyraf</dc:creator>
      <pubDate>Mon, 15 Mar 2021 00:18:49 +0000</pubDate>
      <link>https://dev.to/asyraf/how-to-add-a-link-to-the-image-in-html-1ba5</link>
      <guid>https://dev.to/asyraf/how-to-add-a-link-to-the-image-in-html-1ba5</guid>
      <description>&lt;p&gt;Do you ever wondering how to add a link to your already &lt;br&gt;
HTML image tag ??&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Awesome, let us get into that&lt;/strong&gt; 🔥&lt;/p&gt;



&lt;p&gt;The following example is the usual stuff we do to display image in &lt;code&gt;&amp;lt;img&amp;gt;&lt;/code&gt; tag.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight html"&gt;&lt;code&gt;&lt;span class="nt"&gt;&amp;lt;img&lt;/span&gt; &lt;span class="na"&gt;src=&lt;/span&gt;&lt;span class="s"&gt;"https://www.thecoderpedia.com/wp-content/uploads/2020/06/Programming-Memes-Programmer-while-sleeping.jpg"&lt;/span&gt; 
     &lt;span class="na"&gt;alt=&lt;/span&gt;&lt;span class="s"&gt;"cannot sleep"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fwww.thecoderpedia.com%2Fwp-content%2Fuploads%2F2020%2F06%2FProgramming-Memes-Programmer-while-sleeping.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fwww.thecoderpedia.com%2Fwp-content%2Fuploads%2F2020%2F06%2FProgramming-Memes-Programmer-while-sleeping.jpg"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;When you click the image above, it will zoom in, instead of open the image link. This is the expected result 😅.&lt;/p&gt;

&lt;h2&gt;
  
  
  But how to add a link to the image ??
&lt;/h2&gt;

&lt;p&gt;Easy peasy 😉&lt;/p&gt;

&lt;p&gt;This example builds upon the previous one, showing how to &lt;strong&gt;turn the image into a link&lt;/strong&gt;. To do so, nest the &lt;code&gt;&amp;lt;img&amp;gt;&lt;/code&gt; tag inside the &lt;code&gt;&amp;lt;a&amp;gt;&lt;/code&gt;. &lt;br&gt;
You should make the alternative text describe the resource the link is pointing to as if you were using a text link instead.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight html"&gt;&lt;code&gt;&lt;span class="nt"&gt;&amp;lt;a&lt;/span&gt; &lt;span class="na"&gt;href=&lt;/span&gt;&lt;span class="s"&gt;"https://www.thecoderpedia.com/blog/programming-memes/"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;img&lt;/span&gt; &lt;span class="na"&gt;src=&lt;/span&gt;&lt;span class="s"&gt;"https://www.thecoderpedia.com/wp-content/uploads/2020/06/Programming-Memes-Programmer-while-sleeping.jpg"&lt;/span&gt;
       &lt;span class="na"&gt;alt=&lt;/span&gt;&lt;span class="s"&gt;"Visit Meme site"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;/a&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;a href="https://www.thecoderpedia.com/blog/programming-memes/" rel="noopener noreferrer"&gt;&lt;br&gt;
  &lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fwww.thecoderpedia.com%2Fwp-content%2Fuploads%2F2020%2F06%2FProgramming-Memes-Programmer-while-sleeping.jpg"&gt;&lt;br&gt;
&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Try to click the above image and you will direct to the source link.&lt;/p&gt;

&lt;p&gt;The End ;p&lt;/p&gt;




&lt;p&gt;resources;&lt;br&gt;
&lt;a href="https://developer.mozilla.org/en-US/docs/Web/HTML/Element/img#image_link" rel="noopener noreferrer"&gt;MDN&lt;/a&gt;&lt;/p&gt;

</description>
      <category>html</category>
      <category>link</category>
      <category>image</category>
      <category>howto</category>
    </item>
  </channel>
</rss>
