<?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>Forem: Shkuryn</title>
    <description>The latest articles on Forem by Shkuryn (@shkuryn).</description>
    <link>https://forem.com/shkuryn</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%2F65239%2Fca227d9d-e81c-465c-935e-96f72f51501b.png</url>
      <title>Forem: Shkuryn</title>
      <link>https://forem.com/shkuryn</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/shkuryn"/>
    <language>en</language>
    <item>
      <title>The Magic of Time in Rails: Journey into the Past with time_ago_in_words</title>
      <dc:creator>Shkuryn</dc:creator>
      <pubDate>Sun, 28 Jan 2024 17:41:00 +0000</pubDate>
      <link>https://forem.com/shkuryn/the-magic-of-time-in-rails-journey-into-the-past-with-timeagoinwords-11d4</link>
      <guid>https://forem.com/shkuryn/the-magic-of-time-in-rails-journey-into-the-past-with-timeagoinwords-11d4</guid>
      <description>&lt;p&gt;Web development projects take on a life of their own, and so does time. In the world of Ruby on Rails there is a wonderful method that can give character to your time - time_ago_in_words. Let's embrace this magic and create dynamic, engaging experiences for your users.&lt;br&gt;
Journey to the Past:&lt;/p&gt;

&lt;p&gt;When your site posts get older than dinosaurs, regular timestamps turn into boring numbers. However, with time_ago_in_words you can offer your users more than just "2 minutes ago" or "yesterday".&lt;/p&gt;

&lt;p&gt;How it works:&lt;/p&gt;

&lt;p&gt;This method translates ordinary timestamps into magic phrases, providing dynamic descriptions of time, such as "A moment ago," "An hour ago," or even "Last month." Your interface becomes more alive, and users feel that your messages not only exist in space, but are themselves a part of time.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;time_ago_in_words(3.minutes.from_now)                 # =&amp;gt; 3 minutes
time_ago_in_words(3.minutes.ago)                      # =&amp;gt; 3 minutes
time_ago_in_words(Time.now - 15.hours)                # =&amp;gt; about 15 hours
time_ago_in_words(Time.now)                           # =&amp;gt; less than a minute
time_ago_in_words(Time.now, include_seconds: true) # =&amp;gt; less than 5 seconds

from_time = Time.now - 3.days - 14.minutes - 25.seconds
time_ago_in_words(from_time)      # =&amp;gt; 3 days

from_time = (3.days + 14.minutes + 25.seconds).ago
time_ago_in_words(from_time)      # =&amp;gt; 3 days
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Features and Settings:&lt;/p&gt;

&lt;p&gt;The time_ago_in_words method automatically adapts to different languages and can be customized to suit your preferences. You can use it to create a friendly and interesting user experience.&lt;/p&gt;

&lt;p&gt;About alias:&lt;/p&gt;

&lt;p&gt;The time_ago_in_words method in Ruby on Rails is not alone in its magic. He has his close ally, a synonym, ready to share with him the protection of time - &lt;strong&gt;distance_of_time_in_words_to_now&lt;/strong&gt;. Both methods have the unique ability to transform cold time numbers into lively phrases, immersing your applications in the world of human time perception. So, when creating dynamic user interfaces or visualizing timestamps, you can choose one of these methods, knowing that each is capable of recreating the sense of time with a unique elegance.&lt;/p&gt;

</description>
      <category>rails</category>
      <category>ruby</category>
      <category>date</category>
    </item>
    <item>
      <title>fetch vs index methods when working with arrays in Ruby</title>
      <dc:creator>Shkuryn</dc:creator>
      <pubDate>Sat, 27 Jan 2024 18:04:22 +0000</pubDate>
      <link>https://forem.com/shkuryn/fetch-vs-index-methods-when-working-with-arrays-in-ruby-505k</link>
      <guid>https://forem.com/shkuryn/fetch-vs-index-methods-when-working-with-arrays-in-ruby-505k</guid>
      <description>&lt;p&gt;In the Ruby world, where many methods are provided for manipulating arrays, two of them - fetch and index - often raise questions among developers. Let's look at what the key differences between these methods are and in what situations it is better to use each of them.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;fetch method&lt;/strong&gt;&lt;br&gt;
The fetch method in Ruby is used to retrieve a value from an array at a given index. Here are some key features of the fetch method:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;arr = [1, 2, 3]
value = arr.fetch(5, "default_value") # returns "default_value"
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Setting the default value:&lt;br&gt;
You can specify a default value as the second argument to the fetch method. If the index is outside the array, the default value will be returned.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;arr = [1, 2, 3]
value = arr.fetch(1) # returns 2
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;index method&lt;/strong&gt;&lt;br&gt;
The index method, on the other hand, is used to find the index of a given value in an array. Let's look at the key features of the index method:&lt;/p&gt;

&lt;p&gt;Search value:&lt;br&gt;
index looks for the first occurrence of a given value in the array and returns its index. If the value is not found, nil is returned.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;arr = [1, 2, 3, 2]
index = arr.index(2) # Returns 1 (index of first occurrence of 2)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Unhandled errors:&lt;br&gt;
Unlike fetch, the index method does not handle array out-of-bounds errors. If the value is not found, nil is returned and you need to manually check for this value.&lt;/p&gt;

&lt;p&gt;Example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;arr = [1, 2, 3]
index = arr.index(5) # Returns nil
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;How to choose between fetch and index?&lt;/p&gt;

&lt;p&gt;The choice between fetch and index depends on the task you are performing. If you need to get a value by index with the ability to set a default value, use fetch. If your goal is to find the index of a specific value in an array, index is your method.&lt;/p&gt;

&lt;p&gt;Both methods provide varying levels of flexibility and can be a powerful tool in the hands of the developer. It is important to know their capabilities and choose the appropriate method for a specific task.&lt;/p&gt;

</description>
      <category>ruby</category>
      <category>arrays</category>
      <category>fetch</category>
      <category>index</category>
    </item>
  </channel>
</rss>
