<?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: Merdan Durdyyev</title>
    <description>The latest articles on DEV Community by Merdan Durdyyev (@eminarium).</description>
    <link>https://dev.to/eminarium</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%2F475174%2Fd84f5e20-18f9-4c02-9498-b3efcd0a6f5f.jpeg</url>
      <title>DEV Community: Merdan Durdyyev</title>
      <link>https://dev.to/eminarium</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/eminarium"/>
    <language>en</language>
    <item>
      <title>Concerns in Rails — A Guide and an Example</title>
      <dc:creator>Merdan Durdyyev</dc:creator>
      <pubDate>Mon, 20 Mar 2023 14:50:05 +0000</pubDate>
      <link>https://dev.to/eminarium/concerns-in-rails-a-guide-and-an-example-1e75</link>
      <guid>https://dev.to/eminarium/concerns-in-rails-a-guide-and-an-example-1e75</guid>
      <description>&lt;h2&gt;
  
  
  Welcome
&lt;/h2&gt;

&lt;p&gt;Hello dear friends, coders and enthusiasts.&lt;/p&gt;

&lt;p&gt;Today we are going to dive into the topic of &lt;strong&gt;&lt;em&gt;“Concerns”&lt;/em&gt;&lt;/strong&gt; in Ruby on Rails. Concerns are an important topic in Ruby on Rails but anyway, it’s up to a developer to decide whether to use them or not. And besides that, they need to be used carefully to avoid issues of so called &lt;em&gt;“circular dependency”&lt;/em&gt; and revealing too much info of the model to the Concern.&lt;/p&gt;

&lt;p&gt;So, let’s get started…&lt;/p&gt;




&lt;h2&gt;
  
  
  What does a Concern help with?
&lt;/h2&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;em&gt;A Concern is just a ruby module that extends ActiveSupport::Concern module.&lt;/em&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;You might have already seen the folders that are created by default when creating a new Rails project. Those folders are &lt;strong&gt;&lt;em&gt;‘app/controllers/concerns’&lt;/em&gt;&lt;/strong&gt; and &lt;strong&gt;&lt;em&gt;‘app/models/concerns’&lt;/em&gt;&lt;/strong&gt;. These folders are where concerns should be placed.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;em&gt;Using a concern lets you extract the common logic from different classes into a reusable module.&lt;/em&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;What we have to do to extract some logic into a concern is write a concern module that extends &lt;strong&gt;&lt;em&gt;“ActiveSupport::Concern”&lt;/em&gt;&lt;/strong&gt; module. So, let’s write an &lt;strong&gt;&lt;em&gt;Archiveable&lt;/em&gt;&lt;/strong&gt; concern module.&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;module&lt;/span&gt; &lt;span class="nn"&gt;Archivable&lt;/span&gt;
  &lt;span class="kp"&gt;extend&lt;/span&gt; &lt;span class="no"&gt;ActiveSupport&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="no"&gt;Concern&lt;/span&gt;

  &lt;span class="n"&gt;included&lt;/span&gt; &lt;span class="k"&gt;do&lt;/span&gt;
    &lt;span class="n"&gt;scope&lt;/span&gt; &lt;span class="ss"&gt;:visible&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="n"&gt;where&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="ss"&gt;archived: &lt;/span&gt;&lt;span class="kp"&gt;false&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="n"&gt;scope&lt;/span&gt; &lt;span class="ss"&gt;:archived&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="n"&gt;where&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="ss"&gt;archived: &lt;/span&gt;&lt;span class="kp"&gt;true&lt;/span&gt;&lt;span class="p"&gt;)&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;def&lt;/span&gt; &lt;span class="nf"&gt;archive&lt;/span&gt;
    &lt;span class="n"&gt;update_attribute&lt;/span&gt; &lt;span class="ss"&gt;:archived&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kp"&gt;true&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;p&gt;So, to use it in our models, we just need to include the above described module within our model classes.&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;Post&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="no"&gt;ApplicationRecord&lt;/span&gt;
  &lt;span class="kp"&gt;include&lt;/span&gt; &lt;span class="no"&gt;Archivable&lt;/span&gt;

  &lt;span class="n"&gt;has_many&lt;/span&gt; &lt;span class="ss"&gt;:comments&lt;/span&gt;

  &lt;span class="c1"&gt;# ...&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;

&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Comment&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="no"&gt;ApplicationRecord&lt;/span&gt;
  &lt;span class="kp"&gt;include&lt;/span&gt; &lt;span class="no"&gt;Archivable&lt;/span&gt;

  &lt;span class="n"&gt;belongs_to&lt;/span&gt; &lt;span class="ss"&gt;:post&lt;/span&gt;

  &lt;span class="c1"&gt;# ...&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;As you already might have notice, this code assumes that you have &lt;strong&gt;&lt;em&gt;‘archived’&lt;/em&gt;&lt;/strong&gt; attribute in both of the models (&lt;strong&gt;&lt;em&gt;‘Post’&lt;/em&gt;&lt;/strong&gt; and &lt;strong&gt;&lt;em&gt;‘Comment’&lt;/em&gt;&lt;/strong&gt;) to operate on. Otherwise we won’t be able to use execute the intended code, because the &lt;strong&gt;&lt;em&gt;‘Archivable’&lt;/em&gt;&lt;/strong&gt; module makes use of &lt;strong&gt;&lt;em&gt;‘archived’&lt;/em&gt;&lt;/strong&gt; attribute.&lt;/p&gt;




&lt;h2&gt;
  
  
  Two important parts of a Concern
&lt;/h2&gt;

&lt;p&gt;A concern can provide two blocks that comprise different types of methods to be used in the included class.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;em&gt;included&lt;/em&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The code inside this block is evaluated in the context of the including class. For instance, if a class &lt;strong&gt;&lt;em&gt;‘Comment‘&lt;/em&gt;&lt;/strong&gt; includes a concern, anything inside the &lt;strong&gt;&lt;em&gt;‘included’&lt;/em&gt;&lt;/strong&gt; block will be evaluated as if it was written inside the &lt;strong&gt;&lt;em&gt;“Comment”&lt;/em&gt;&lt;/strong&gt; class.&lt;/p&gt;

&lt;p&gt;You can add validations, associations, scopes, or other methods, and all of these become instance methods of the including class.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;em&gt;class_methods&lt;/em&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;All the methods added inside this block become class methods of the including class. As a second option, you can create a nested module called &lt;strong&gt;&lt;em&gt;“ClassMethods”&lt;/em&gt;&lt;/strong&gt; where you can put your class methods.&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;module&lt;/span&gt; &lt;span class="nn"&gt;Visible&lt;/span&gt;
  &lt;span class="kp"&gt;extend&lt;/span&gt; &lt;span class="no"&gt;ActiveSupport&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="no"&gt;Concern&lt;/span&gt;

  &lt;span class="c1"&gt;# This is where you place instance methods&lt;/span&gt;
  &lt;span class="n"&gt;included&lt;/span&gt; &lt;span class="k"&gt;do&lt;/span&gt;
    &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;toggle_visibility&lt;/span&gt;
      &lt;span class="n"&gt;toggle!&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="ss"&gt;:is_visible&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="c1"&gt;# This is where you place class methods&lt;/span&gt;
  &lt;span class="n"&gt;class_methods&lt;/span&gt; &lt;span class="k"&gt;do&lt;/span&gt;
    &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;get_all_visible&lt;/span&gt;
      &lt;span class="n"&gt;all&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;select&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt;&lt;span class="n"&gt;item&lt;/span&gt;&lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="n"&gt;item&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;is_visible?&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="k"&gt;end&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here’s the &lt;strong&gt;&lt;em&gt;“Visible”&lt;/em&gt;&lt;/strong&gt; concern where the two above-mentioned blocks reside and we place our instance and class methods accordingly within those blocks.&lt;/p&gt;




&lt;h2&gt;
  
  
  Good approaches to using Concerns
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;&lt;em&gt;Single Responsibility Principle&lt;/em&gt;&lt;/strong&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;A good concern should be able to work in isolation, so it must be dependency-free. It should have a very concrete and limited responsibility.&lt;/p&gt;

&lt;p&gt;Bear in mind the Single Responsibility Principle when extracting something to a concern. That being said, you need to extract just a small, reasonable code to a concern so that it stays manageable and include’able into several locations in the future.&lt;/p&gt;

&lt;p&gt;Construct a concern that, for instance, just manages &lt;strong&gt;&lt;em&gt;Archive’ability, Exportability, Notifiability&lt;/em&gt;&lt;/strong&gt; of a model, and not many of those features.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;&lt;em&gt;Using Concerns for multiple models&lt;/em&gt;&lt;/strong&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;It is a good approach to extract Concerns when they tend to be useful in several models. This way we are obeying the DRY principle and using the same piece of code in several models. It shrinks the code and makes it manageable.&lt;/p&gt;

&lt;p&gt;A good example of this might be &lt;strong&gt;&lt;em&gt;“Exportable”&lt;/em&gt;&lt;/strong&gt; module/concern. This concern can be included in several models, like: &lt;strong&gt;&lt;em&gt;Email, Document, Table, Drawing&lt;/em&gt;&lt;/strong&gt; and etc… This way, it becomes a very useful accessory to use.&lt;/p&gt;




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

&lt;p&gt;We have covered the basics of implementing concerns in Ruby on Rails and it gives you a basic idea of why’s and how to’s about using concerns.&lt;/p&gt;

&lt;p&gt;Actually no code is perfect and it’s up to you to decide how to organize things and make the code perfectly readable. You need to use your own judgment and weigh the pros and cons of either of the options.&lt;/p&gt;

&lt;p&gt;Hope to meet you in the next article !&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Stay safe, stay healthy, stay hungry !!!&lt;/strong&gt; 👋&lt;/p&gt;

</description>
      <category>ruby</category>
      <category>rails</category>
      <category>tutorial</category>
      <category>beginners</category>
    </item>
    <item>
      <title>Rails Service Objects — A Guide and an Example</title>
      <dc:creator>Merdan Durdyyev</dc:creator>
      <pubDate>Wed, 08 Feb 2023 11:34:38 +0000</pubDate>
      <link>https://dev.to/eminarium/rails-service-objects-a-guide-and-an-example-55j4</link>
      <guid>https://dev.to/eminarium/rails-service-objects-a-guide-and-an-example-55j4</guid>
      <description>&lt;h2&gt;
  
  
  Introduction
&lt;/h2&gt;

&lt;p&gt;Hello dear coders, enthusiasts and learners!&lt;/p&gt;

&lt;p&gt;Hope you are all doing fine and ready to learn some more tips about Ruby on Rails. This time we are going to mention one of the ways to refactor the code in Ruby on Rails projects.&lt;/p&gt;

&lt;p&gt;Actually, code logic that is unrelated models, or controllers can be extracted to separate parts like, Modules, Concerns, Service objects, Helpers and etc. And this time we are going to focus on Service objects.&lt;/p&gt;




&lt;h2&gt;
  
  
  Do we actually need Rails Service Objects ?
&lt;/h2&gt;

&lt;p&gt;Ruby on Rails is an excellent framework for building MVP or developing large scale applications. As years pass by, the codebase grows into a monstrous giant and you start to understand it’s time to take a different approach.&lt;/p&gt;

&lt;p&gt;You must keep unconcerned things separated, everything in its own place, follow DRY concept, skinny models, fat controllers, and etc. to keep things manageable, properly organized and sane.&lt;/p&gt;

&lt;p&gt;That’s the next step when you start extracting part of the code into Service Objects, Concerns, extra Modules, or even start migrating to microservices, to prevent the codebase from bloating and exploding one day.&lt;/p&gt;

&lt;p&gt;So, the answer is &lt;strong&gt;“YES”&lt;/strong&gt;. We do actually need Service Objects, to offload much of the workload from controllers and other parts of the code.&lt;/p&gt;




&lt;h2&gt;
  
  
  What are Services Objects in Rails?
&lt;/h2&gt;

&lt;p&gt;When you business logic can’t fit either into models, or controllers, that’s the time you need to go on with Services, to be able to separate unrelated business logic into its own Ruby object.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;&amp;gt; Services objects are just Plain Old Ruby Objects (PORO) that are intended to perform a single action at its core.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;There’s an opinionated discussion whether where to place your service objects. So, generally, service objects are placed inside either in &lt;em&gt;“lib/services”&lt;/em&gt;, or &lt;em&gt;“app/services”&lt;/em&gt; folders. You can read couple of articles or discussions on that and come to a decision of your own.&lt;/p&gt;

&lt;p&gt;So, a Service Object is a way of encapsulating a part of logic, a single action, or function, into an independent single-use object. It serves a single purpose and exposes only one public method, and is built around that method.&lt;/p&gt;

&lt;p&gt;That single public method is generally called &lt;em&gt;“call”&lt;/em&gt;, Gitlab calls it &lt;em&gt;“execute”&lt;/em&gt; in its services, some like calling it &lt;em&gt;“perform”&lt;/em&gt;. But that’s up to you how to call it, as long it is a single public method that will be executed when calling the service.&lt;/p&gt;




&lt;h2&gt;
  
  
  Creating a Service Object
&lt;/h2&gt;

&lt;p&gt;To create a Service object, we can create a directory for it and create a new file with following command:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$ mkdir app/services &amp;amp;&amp;amp; touch app/services/tweet_creator.rb
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;So, let’s create a new TweetCreator in a new folder called app/services:&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;# app/services/tweet_creator.rb&lt;/span&gt;
&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;TweetCreator&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="n"&gt;message&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="vi"&gt;@message&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;message&lt;/span&gt;
  &lt;span class="k"&gt;end&lt;/span&gt;

  &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;send_tweet&lt;/span&gt;
    &lt;span class="n"&gt;client&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="no"&gt;Twitter&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="no"&gt;REST&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="no"&gt;Client&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;new&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;config&lt;/span&gt;&lt;span class="o"&gt;|&lt;/span&gt;
      &lt;span class="n"&gt;config&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;consumer_key&lt;/span&gt;        &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="no"&gt;ENV&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s1"&gt;'TWITTER_CONSUMER_KEY'&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
      &lt;span class="n"&gt;config&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;consumer_secret&lt;/span&gt;     &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="no"&gt;ENV&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s1"&gt;'TWITTER_CONSUMER_SECRET'&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
      &lt;span class="n"&gt;config&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;access_token&lt;/span&gt;        &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="no"&gt;ENV&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s1"&gt;'TWITTER_ACCESS_TOKEN'&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
      &lt;span class="n"&gt;config&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;access_token_secret&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="no"&gt;ENV&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s1"&gt;'TWITTER_ACCESS_SECRET'&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
    &lt;span class="k"&gt;end&lt;/span&gt;
    &lt;span class="n"&gt;client&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;update&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="vi"&gt;@message&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;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;After you have created the &lt;strong&gt;&lt;em&gt;“TweetCreator”&lt;/em&gt;&lt;/strong&gt; Service object, you can call it anywhere in your code, with:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;TweetCreator.new(params[:message]).send_tweet
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h2&gt;
  
  
  What are examples of using a Service Object?
&lt;/h2&gt;

&lt;p&gt;There can be dozens of situations when we need to extract some part of the code into Service objects. But, just to count some of them, these are the the situations and examples:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Send a tweet to Twitter — &lt;em&gt;TweetCreator&lt;/em&gt;
&lt;/li&gt;
&lt;li&gt;Follow a user in Facebook — &lt;em&gt;FacebookUserFollower&lt;/em&gt;
&lt;/li&gt;
&lt;li&gt;To charge a customer in Ecommerce — &lt;em&gt;CustomerCharger, PaymentCharger&lt;/em&gt;
&lt;/li&gt;
&lt;li&gt;Send a post to Facebook — &lt;em&gt;FacebookPoster&lt;/em&gt;
&lt;/li&gt;
&lt;li&gt;Try a payment transaction to payment services (&lt;em&gt;Paypal, Stripe&lt;/em&gt;)&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;These are just some of the widely used scenarios of Service objects. But of course, your project can have something very different compared to these examples. So, analyze it, and if it does not fit into model or controller, move it out to a Service Object.&lt;/p&gt;




&lt;h2&gt;
  
  
  Rules for writing good Service Objects in Rails
&lt;/h2&gt;

&lt;p&gt;There are many ways you can name your Service Objects. But you need to stick to a single naming convention you chose in you own codebase to be able to easily differentiate Service Objects.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Here are some options for choosing naming conventions:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;UserCreator, PaymentCharger, WelcomeMessenger, AccountValidator&lt;/li&gt;
&lt;li&gt;CreateUser, ChargePayment, MessageUser, ValidateAccount, etc…&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Here are general rules for writing meaningful and reasonable Service objects:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Only one public method per Service object&lt;/li&gt;
&lt;li&gt;Name Service objects like regular roles in a Company &lt;em&gt;(TweetCreator, TweetReader, FacebookPoster, etc.)&lt;/em&gt;
&lt;/li&gt;
&lt;li&gt;No generic objects to perform several actions&lt;/li&gt;
&lt;li&gt;Handle exceptions inside the Service object.&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  Benefits of using a Service Object in Rails
&lt;/h2&gt;

&lt;p&gt;Service Objects are a great way to decouple your application logic from your controllers. You can use them to separate concerns and reuse them in different parts of your application. With this pattern, here are the benefits you get:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;# Clean controllers&lt;/strong&gt;&lt;br&gt;
Fat controllers, bloating code within a class, are a sleeping dragon, napping monster, that can wake up one day and cause you tons of trouble. When that happens, you might get lost in the tens or hundreds of lines of code, not understanding what it actually does. So, it is an advantage to have a clean, tidy few lines of code that whispers you what it is intended for while you are reading it.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;# Ease of testing&lt;/strong&gt;&lt;br&gt;
Applying separation of concerns, and extracting the unrelated pieces of code, so that they live on their own, allows easier testing of code and interacting with them independently.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;# Reusable service objects&lt;/strong&gt;&lt;br&gt;
Extracting some code to specific Service Objects, gives you a freedom of calling them wherever you want, in your controllers, background jobs, or other services. Thus, you get a reusable piece of code.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;# Separation of concerns&lt;/strong&gt;&lt;br&gt;
Rails controllers see Service objects and interact with them when required. This provides a way to decouple lots of logic into separate piece of code, especially if you want to migrate to microservices architecture. In that case you can extract and move those services with minimal distraction and modification.&lt;/p&gt;


&lt;h2&gt;
  
  
  Syntactic Sugar for Service objects
&lt;/h2&gt;

&lt;p&gt;Just think about it, this feels great in theory, but &lt;code&gt;TweetCreator.new(params[:message]).send_tweet&lt;/code&gt; is a lot to write. It is too verbose with lots of redundant words. We need to instantiate it and call a method to send the tweet.&lt;/p&gt;

&lt;p&gt;We need to shorten it to a single call, so that single call does it all for us. So, there is &lt;code&gt;Proc.call&lt;/code&gt; that calls it and executes itself immediately with the given parameters.&lt;/p&gt;

&lt;p&gt;A &lt;code&gt;proc&lt;/code&gt;can be &lt;code&gt;call&lt;/code&gt;-ed to execute itself with the given parameters. Which means, that if &lt;code&gt;TweetCreator&lt;/code&gt; were a proc, we could call it with &lt;code&gt;TweetCreator.call(message)&lt;/code&gt; and the result would be equivalent to &lt;code&gt;TweetCreator.new(params[:message]).call&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;We can apply the same for the Service object.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Example #1&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;First, because we probably want to reuse this behavior across all our service objects, let’s borrow from the Rails Way and create a class called &lt;code&gt;ApplicationService&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;# app/services/application_service.rb&lt;/span&gt;
&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;ApplicationService&lt;/span&gt;
  &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nc"&gt;self&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;call&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;args&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="n"&gt;block&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="n"&gt;new&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;args&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="n"&gt;block&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nf"&gt;call&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;p&gt;After that, we can inherit TweetCreator from &lt;em&gt;“ApplicationService”&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="c1"&gt;# app/services/tweet_creator.rb&lt;/span&gt;
&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;TweetCreator&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="no"&gt;ApplicationService&lt;/span&gt;
  &lt;span class="nb"&gt;attr_reader&lt;/span&gt; &lt;span class="ss"&gt;:message&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="n"&gt;message&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="vi"&gt;@message&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;message&lt;/span&gt;
  &lt;span class="k"&gt;end&lt;/span&gt;

  &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;call&lt;/span&gt;
    &lt;span class="n"&gt;client&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="no"&gt;Twitter&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="no"&gt;REST&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="no"&gt;Client&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;new&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;config&lt;/span&gt;&lt;span class="o"&gt;|&lt;/span&gt;
      &lt;span class="n"&gt;config&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;consumer_key&lt;/span&gt;        &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="no"&gt;ENV&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s1"&gt;'TWITTER_CONSUMER_KEY'&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
      &lt;span class="n"&gt;config&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;consumer_secret&lt;/span&gt;     &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="no"&gt;ENV&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s1"&gt;'TWITTER_CONSUMER_SECRET'&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
      &lt;span class="n"&gt;config&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;access_token&lt;/span&gt;        &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="no"&gt;ENV&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s1"&gt;'TWITTER_ACCESS_TOKEN'&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
      &lt;span class="n"&gt;config&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;access_token_secret&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="no"&gt;ENV&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s1"&gt;'TWITTER_ACCESS_SECRET'&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
    &lt;span class="k"&gt;end&lt;/span&gt;
    &lt;span class="n"&gt;client&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;update&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="vi"&gt;@message&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;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;And here is the magic “call”. We can call it in the controller this way:&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;TweetController&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="no"&gt;ApplicationController&lt;/span&gt;
  &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;create&lt;/span&gt;
    &lt;span class="no"&gt;TweetCreator&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;call&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;params&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="ss"&gt;:message&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;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Example #2&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Here is one more example of a Service object, that extracts process of updating a trip to a separate Service object.&lt;/p&gt;

&lt;p&gt;If we want to reuse this behavior for other service objects, we can add a new class called &lt;code&gt;BaseService&lt;/code&gt;or &lt;code&gt;ApplicationService&lt;/code&gt;and inherit from it for our &lt;code&gt;TripUpdateService&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;# app/services/base_service.rb&lt;/span&gt;
&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;BaseService&lt;/span&gt;
  &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nc"&gt;self&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;call&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;args&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="n"&gt;block&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="n"&gt;new&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;args&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="n"&gt;block&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nf"&gt;call&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;p&gt;And here is the &lt;em&gt;“TripUpdateService”&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="c1"&gt;# app/services/trip_update_service.rb&lt;/span&gt;
&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;TripUpdateService&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="no"&gt;BaseService&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="n"&gt;trip&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;params&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="vi"&gt;@trip&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;trip&lt;/span&gt;
    &lt;span class="vi"&gt;@params&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;params&lt;/span&gt;
  &lt;span class="k"&gt;end&lt;/span&gt;

  &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;call&lt;/span&gt;
    &lt;span class="n"&gt;distance_and_duration&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;calculate_trip_distance_and_duration&lt;/span&gt;
                                                   &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="vi"&gt;@params&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="ss"&gt;:start_address&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt;
                                                    &lt;span class="vi"&gt;@params&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="ss"&gt;:destination_address&lt;/span&gt;&lt;span class="p"&gt;])&lt;/span&gt;
    &lt;span class="vi"&gt;@trip&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;update&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="vi"&gt;@params&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;distance_and_duration&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
  &lt;span class="k"&gt;end&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;calculate_trip_distance_and_duration&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;start_address&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;destination_address&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="n"&gt;distance&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="no"&gt;Google&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="no"&gt;Maps&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;distance&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;start_address&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;destination_address&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="n"&gt;duration&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="no"&gt;Google&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="no"&gt;Maps&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;duration&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;start_address&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;destination_address&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="ss"&gt;distance: &lt;/span&gt;&lt;span class="n"&gt;distance&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="ss"&gt;duration: &lt;/span&gt;&lt;span class="n"&gt;duration&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;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;then we can update our controller action to call the service object correctly:&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;# app/controllers/trips_controller.rb&lt;/span&gt;
&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;TripsController&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="no"&gt;ApplicationController&lt;/span&gt;
  &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;update&lt;/span&gt;
    &lt;span class="vi"&gt;@trip&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="no"&gt;Trip&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="n"&gt;params&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="ss"&gt;:id&lt;/span&gt;&lt;span class="p"&gt;])&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="no"&gt;TripUpdateService&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;call&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="vi"&gt;@trip&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;trip_params&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
      &lt;span class="n"&gt;redirect_to&lt;/span&gt; &lt;span class="vi"&gt;@trip&lt;/span&gt;
    &lt;span class="k"&gt;else&lt;/span&gt;
      &lt;span class="n"&gt;render&lt;/span&gt; &lt;span class="ss"&gt;:edit&lt;/span&gt;
    &lt;span class="k"&gt;end&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;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;Finally we are at the end of the article about Service objects.&lt;/p&gt;

&lt;p&gt;As you might have already read, Service objects are really a helping hand to avoid code bloat and cluttering the code logic. But it is up to you whether to use it or not.&lt;/p&gt;

&lt;p&gt;If the code fits the criteria when it should be extracted to a Service, then it’s a good opportunity for you to separate concerns and make your code much more readable.&lt;/p&gt;

&lt;p&gt;Hope so much, this article helped you learn at least a tiny bit of something new about Ruby on Rails and some programming concepts.&lt;/p&gt;

&lt;p&gt;See you in the next article. Stay safe and eager to learn !!!&lt;/p&gt;

</description>
      <category>goodjob</category>
    </item>
    <item>
      <title>Check whether a number is prime - Ruby</title>
      <dc:creator>Merdan Durdyyev</dc:creator>
      <pubDate>Sat, 15 Oct 2022 10:24:26 +0000</pubDate>
      <link>https://dev.to/eminarium/check-whether-a-number-is-prime-ruby-4dc7</link>
      <guid>https://dev.to/eminarium/check-whether-a-number-is-prime-ruby-4dc7</guid>
      <description>&lt;h2&gt;
  
  
  WELCOME
&lt;/h2&gt;

&lt;p&gt;Good time of the day, dear friends, coders and enthusiasts.&lt;br&gt;
I haven't been posting for a long while, and have been primarily improving myself and working on some side projects. But the will to share what I have been learning and doing is still in me, burning wildly.&lt;br&gt;
I have decided to start sharing some small articles about algorithms, and I decided to start from small.&lt;br&gt;
Today we are going to look through an algorithm that checks whether the provided number is prime.&lt;/p&gt;


&lt;h2&gt;
  
  
  What is a prime number ?
&lt;/h2&gt;

&lt;p&gt;Before getting into writing the code, let us understand what exactly the prime numbers are? Prime numbers are those numbers which can only be divisible by itself or 1. So, we will design a code which can fulfill the property of prime numbers.&lt;br&gt;
That means, if the number is less than 2, we can immediately return 'false'.&lt;/p&gt;

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


&lt;h2&gt;
  
  
  Steps to reproduce
&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;&lt;em&gt;Return false if the provided number is less than 2 (because 0 and 1 are not considered to be prime numbers)&lt;/em&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;em&gt;Run through all the numbers starting from 2 till 'n-1', where 'n' is a number provided as a parameter, and check whether 'n' is divisible by any of them&lt;/em&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;em&gt;If we find that 'n' is divisible by any number in the range [2...n-1], then immediately break the loop and return 'false'&lt;/em&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;em&gt;If we did not return a result in step 3, then return true. That means the number is prime&lt;/em&gt;&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;


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

&lt;p&gt;If you want an immediate solution, you can use 'prime?' method provided by Ruby.&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;# Pre-defined class&lt;/span&gt;
&lt;span class="nb"&gt;require&lt;/span&gt; &lt;span class="s1"&gt;'prime'&lt;/span&gt;

&lt;span class="c1"&gt;# Initializing the numbers&lt;/span&gt;
&lt;span class="n"&gt;num1&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;17&lt;/span&gt;
&lt;span class="n"&gt;num2&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;90&lt;/span&gt;
&lt;span class="n"&gt;num3&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;29&lt;/span&gt;

&lt;span class="c1"&gt;# Printing if prime or not&lt;/span&gt;
&lt;span class="nb"&gt;puts&lt;/span&gt; &lt;span class="n"&gt;num1&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;prime?&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;num2&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;prime?&lt;/span&gt; &lt;span class="c1"&gt;# false&lt;/span&gt;
&lt;span class="nb"&gt;puts&lt;/span&gt; &lt;span class="n"&gt;num3&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;prime?&lt;/span&gt; &lt;span class="c1"&gt;# true&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Ruby even provides a method for listing all prime numbers in a specified range&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="nb"&gt;require&lt;/span&gt; &lt;span class="s1"&gt;'prime'&lt;/span&gt;

&lt;span class="no"&gt;Prime&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;each&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;27&lt;/span&gt;&lt;span class="p"&gt;)&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;prime&lt;/span&gt;&lt;span class="o"&gt;|&lt;/span&gt;
  &lt;span class="nb"&gt;p&lt;/span&gt; &lt;span class="n"&gt;prime&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;

&lt;span class="c1"&gt;#=&amp;gt; 2, 3, 5, 7, 11, 13, 17, 19, 23&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h2&gt;
  
  
  Ruby solution - 1
&lt;/h2&gt;

&lt;p&gt;Here's the actual solution to check whether the number is prime, with Ruby. Of course, assuming that the input number is positive.&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;is_prime&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;num&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="kp"&gt;false&lt;/span&gt; &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;num&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

  &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="o"&gt;..&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;num&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="nf"&gt;each&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;n&lt;/span&gt;&lt;span class="o"&gt;|&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="kp"&gt;false&lt;/span&gt; &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;num&lt;/span&gt; &lt;span class="o"&gt;%&lt;/span&gt; &lt;span class="n"&gt;n&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;
  &lt;span class="k"&gt;end&lt;/span&gt;

  &lt;span class="kp"&gt;true&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h2&gt;
  
  
  Ruby solution - 2
&lt;/h2&gt;

&lt;p&gt;And if you want it shorter, as a one liner, here's the one that returns the result faster. Because it does not check all numbers in the range of [2...n-1], it only checks the numbers between [2...sqrt(n)].&lt;br&gt;
It is considered that there are no divisors of a number higher than it's square root. So we are only checking numbers till square root of 'n'.&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;is_prime?&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;num&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="kp"&gt;false&lt;/span&gt; &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;num&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;
  &lt;span class="no"&gt;Math&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;sqrt&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;num&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;to_i&lt;/span&gt;
    &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;downto&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="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;each&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="o"&gt;|&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="kp"&gt;false&lt;/span&gt; &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;num&lt;/span&gt; &lt;span class="o"&gt;%&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;
  &lt;span class="kp"&gt;true&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Solution provided above is one of the most optimal ones. Otherwise you can also check numbers between [2...n/2], that is till the half of the provided number. But of course, square root provides us with even less numbers to run through.&lt;/p&gt;




&lt;h2&gt;
  
  
  Javascript solution - one liner
&lt;/h2&gt;

&lt;p&gt;Here you can see almost the same solution as provided in 'Ruby solution - 2', but in Javascript.&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;const&lt;/span&gt; &lt;span class="nx"&gt;isPrime&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;num&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;num&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="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="nc"&gt;Array&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;Math&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;floor&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;Math&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;sqrt&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;num&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="nf"&gt;fill&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="nf"&gt;map&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="nx"&gt;_&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt; &lt;span class="o"&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;every&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;i&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;num&lt;/span&gt; &lt;span class="o"&gt;%&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt; &lt;span class="o"&gt;!==&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This solution checks whether none of the numbers between [2...sqrt(n)] are divisible by 'n'. If it finds any divisor in that region, it returns false.&lt;/p&gt;




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

&lt;p&gt;So, this is it, guys. Hope you gained some useful insights from this post or at least were happy to see beautiful one liner code snippets in Ruby and Javascript.&lt;br&gt;
I will try to keep posting other useful solutions to common simple algorithms, so stay with Dev.to and all the developer community to keep improving.&lt;br&gt;
Stay safe and Good luck!!!&lt;/p&gt;

</description>
      <category>ruby</category>
      <category>prime</category>
      <category>algorithms</category>
      <category>javascript</category>
    </item>
    <item>
      <title>Send SMS with Ruby Mechanize gem</title>
      <dc:creator>Merdan Durdyyev</dc:creator>
      <pubDate>Mon, 07 Jun 2021 05:05:24 +0000</pubDate>
      <link>https://dev.to/eminarium/send-sms-with-ruby-mechanize-gem-oni</link>
      <guid>https://dev.to/eminarium/send-sms-with-ruby-mechanize-gem-oni</guid>
      <description>&lt;h1&gt;
  
  
  Welcome
&lt;/h1&gt;

&lt;p&gt;Good day, dear friends, coders, and enthusiasts. Here's another post about how to do something else with precious Ruby. Today, we are going to write Ruby code to send SMS via a web app, provided by a Mobile service Carrier. No matter, be it US carriers like &lt;strong&gt;AT&amp;amp;T&lt;/strong&gt;, &lt;strong&gt;Verizon&lt;/strong&gt;, &lt;strong&gt;Sprint&lt;/strong&gt;, or carriers in Russia like &lt;strong&gt;MTC&lt;/strong&gt;, &lt;strong&gt;Megafon&lt;/strong&gt;, and &lt;strong&gt;Beeline&lt;/strong&gt;, we can use their web app (if they provide one) to automate sending SMS with Ruby.&lt;/p&gt;




&lt;h1&gt;
  
  
  Mechanize gem
&lt;/h1&gt;

&lt;p&gt;Let's first make it clear, what a &lt;em&gt;gem&lt;/em&gt; is. &lt;br&gt;
&lt;strong&gt;Gem&lt;/strong&gt; :&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;is a software package that contains packed Ruby application or library.&lt;/li&gt;
&lt;li&gt;is used to extend or modify functionality in Ruby applications&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;To sum it up, we create Ruby gems, to be able to distribute them and make someone else's life easier. Other Rubyists can use them in their own code to import ready-to-use code, enhance productivity, and make development much faster.  &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Mechanize gem&lt;/strong&gt; - is a ruby library that makes automated web interaction easy. Mechanize can follow links and submit forms. Form fields can be populated and submitted.&lt;/p&gt;

&lt;p&gt;You can find more about the mechanize gem here: &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://github.com/sparklemotion/mechanize" rel="noopener noreferrer"&gt;Github page&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://github.com/sparklemotion/mechanize/blob/main/EXAMPLES.rdoc" rel="noopener noreferrer"&gt;Examples &lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://github.com/sparklemotion/mechanize/blob/main/GUIDE.rdoc" rel="noopener noreferrer"&gt;Guide&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;


&lt;h1&gt;
  
  
  Sending Test SMS
&lt;/h1&gt;

&lt;p&gt;Here is the piece of code, where you can follow and get a clear explanation of how to fill in the form fields, submit it, and send the required SMS.&lt;br&gt;
The following input fields and service URL are shown just as an example and maybe named differently in actual services.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;em&gt;&lt;strong&gt;'&lt;a href="https://customer.verizon.com/" rel="noopener noreferrer"&gt;https://customer.verizon.com/&lt;/a&gt;'&lt;/strong&gt;&lt;/em&gt; customer account URL&lt;/li&gt;
&lt;li&gt;
&lt;em&gt;&lt;strong&gt;'login'&lt;/strong&gt;&lt;/em&gt; input field&lt;/li&gt;
&lt;li&gt;
&lt;em&gt;&lt;strong&gt;'password'&lt;/strong&gt;&lt;/em&gt; input field&lt;/li&gt;
&lt;li&gt;
&lt;em&gt;&lt;strong&gt;'Sms.DestinationAddress'&lt;/strong&gt;&lt;/em&gt; input field&lt;/li&gt;
&lt;li&gt;
&lt;em&gt;&lt;strong&gt;'Sms.Text'&lt;/strong&gt;&lt;/em&gt; textarea field
&lt;/li&gt;
&lt;/ul&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight ruby"&gt;&lt;code&gt;&lt;span class="nb"&gt;require&lt;/span&gt; &lt;span class="s1"&gt;'mechanize'&lt;/span&gt;

&lt;span class="n"&gt;mech&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="no"&gt;Mechanize&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;# Ignore SSL certificate&lt;/span&gt;
&lt;span class="n"&gt;mech&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;agent&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;http&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;verify_mode&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="no"&gt;OpenSSL&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="no"&gt;SSL&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="no"&gt;VERIFY_NONE&lt;/span&gt; 

&lt;span class="c1"&gt;# The login page&lt;/span&gt;
&lt;span class="n"&gt;login_page&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;mech&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;get&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'https://customer.verizon.com/'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;  
&lt;span class="c1"&gt;# The login form&lt;/span&gt;
&lt;span class="n"&gt;login_form&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;login_page&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;forms&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="c1"&gt;# Filling in 'login' and 'password' input fields, and submitting.&lt;/span&gt;
&lt;span class="n"&gt;login_form&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s1"&gt;'login'&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s1"&gt;'888-555-4444'&lt;/span&gt;
&lt;span class="n"&gt;login_form&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s1"&gt;'password'&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s1"&gt;'ABCDEFG'&lt;/span&gt;
&lt;span class="n"&gt;login_form&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;submit&lt;/span&gt;

&lt;span class="c1"&gt;# The SMS form page&lt;/span&gt;
&lt;span class="n"&gt;sms_page&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;mech&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;get&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'https://customer.verizon.com/SMS/Send'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;    
&lt;span class="c1"&gt;# The SMS form, the first and only form&lt;/span&gt;
&lt;span class="n"&gt;sms_form&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;sms_page&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;forms&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="c1"&gt;# Entering sms number and sms message to send&lt;/span&gt;
&lt;span class="c1"&gt;# 'Sms.DestinationAddress' and 'Sms.Text' are input and textbox names&lt;/span&gt;
&lt;span class="n"&gt;sms_form&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s1"&gt;'Sms.DestinationAddress'&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s1"&gt;'555-888-2222'&lt;/span&gt;
&lt;span class="n"&gt;sms_form&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s1"&gt;'Sms.Text'&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s1"&gt;'This is test message'&lt;/span&gt;
&lt;span class="n"&gt;sms_form&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;submit&lt;/span&gt;    
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h1&gt;
  
  
  Automate SMS Sending
&lt;/h1&gt;

&lt;p&gt;What we need to do now is just add the list of recipients (their numbers and corresponding messages). I have added just two of them. But you can add lots of more.&lt;br&gt;
What I have done here is: &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;I added list of recipients;&lt;/li&gt;
&lt;li&gt;I gave the list to an 'each' block.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;You can make it even better by gathering all that code under a function that gets respective arguments and call that function for each of the recipients.&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="nb"&gt;require&lt;/span&gt; &lt;span class="s1"&gt;'mechanize'&lt;/span&gt;

&lt;span class="n"&gt;sms_list&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="ss"&gt;number: &lt;/span&gt;&lt;span class="s1"&gt;'555-666-8888'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="ss"&gt;message: &lt;/span&gt;&lt;span class="s1"&gt;'Get this SMS...'&lt;/span&gt;&lt;span class="p"&gt;},&lt;/span&gt;
    &lt;span class="c1"&gt;# And lots of lots SMS numbers and messages...&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="ss"&gt;number: &lt;/span&gt;&lt;span class="s1"&gt;'555-777-0022'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="ss"&gt;message: &lt;/span&gt;&lt;span class="s1"&gt;'Last one...'&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;]&lt;/span&gt;
&lt;span class="n"&gt;mech&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="no"&gt;Mechanize&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;# Ignore SSL certificate&lt;/span&gt;
&lt;span class="n"&gt;mech&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;agent&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;http&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;verify_mode&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="no"&gt;OpenSSL&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="no"&gt;SSL&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="no"&gt;VERIFY_NONE&lt;/span&gt; 

&lt;span class="c1"&gt;# The login page&lt;/span&gt;
&lt;span class="n"&gt;login_page&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;mech&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;get&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'https://customer.verizon.com/'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;  
&lt;span class="c1"&gt;# The login form&lt;/span&gt;
&lt;span class="n"&gt;login_form&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;login_page&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;forms&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="c1"&gt;# Filling in 'login' and 'password' input fields, and submitting.&lt;/span&gt;
&lt;span class="n"&gt;login_form&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s1"&gt;'login'&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s1"&gt;'888-555-4444'&lt;/span&gt;
&lt;span class="n"&gt;login_form&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s1"&gt;'password'&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s1"&gt;'ABCDEFG'&lt;/span&gt;
&lt;span class="n"&gt;login_form&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;submit&lt;/span&gt;

&lt;span class="c1"&gt;# The SMS form page&lt;/span&gt;
&lt;span class="n"&gt;sms_page&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;mech&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;get&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'https://customer.verizon.com/SMS/Send'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;    
&lt;span class="c1"&gt;# The SMS form, the first and only form&lt;/span&gt;
&lt;span class="n"&gt;sms_form&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;sms_page&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;forms&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="c1"&gt;# Send SMS to whole 'sms_list'&lt;/span&gt;
&lt;span class="n"&gt;sms_list&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;each&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;contact&lt;/span&gt;&lt;span class="o"&gt;|&lt;/span&gt;
    &lt;span class="c1"&gt;# Entering sms number and sms message to send&lt;/span&gt;
    &lt;span class="n"&gt;sms_form&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s1"&gt;'Sms.DestinationAddress'&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;contact&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="ss"&gt;:number&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
    &lt;span class="n"&gt;sms_form&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s1"&gt;'Sms.Text'&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;contact&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="ss"&gt;:message&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
    &lt;span class="c1"&gt;# Submitting form, sending sms&lt;/span&gt;
    &lt;span class="n"&gt;sms_form&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;submit&lt;/span&gt;                                             
&lt;span class="k"&gt;end&lt;/span&gt;

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

&lt;/div&gt;






&lt;h1&gt;
  
  
  Sum It Up
&lt;/h1&gt;

&lt;p&gt;We have looked at just a small piece of what 'mechanize' gem can actually do. It has lots of more possibilities.&lt;br&gt;
You can write a console application that surfs the given URLs and performs given operations without your attendance. So, the rest is up to you to discover. Go for it!&lt;/p&gt;

</description>
      <category>ruby</category>
      <category>webdev</category>
      <category>sms</category>
      <category>scraping</category>
    </item>
    <item>
      <title>Ruby’s precious gems</title>
      <dc:creator>Merdan Durdyyev</dc:creator>
      <pubDate>Tue, 06 Apr 2021 08:21:17 +0000</pubDate>
      <link>https://dev.to/eminarium/ruby-s-precious-gems-4bo7</link>
      <guid>https://dev.to/eminarium/ruby-s-precious-gems-4bo7</guid>
      <description>&lt;h1&gt;
  
  
  Welcome
&lt;/h1&gt;

&lt;p&gt;Hello, dear friends, Ruby enthusiasts, and coders. I am glad you found time to spend your precious time to learn a bit more about precious Ruby and its precious “Gems”. So, let’s get to it.&lt;/p&gt;




&lt;h1&gt;
  
  
  What are Ruby gems?
&lt;/h1&gt;

&lt;p&gt;A Ruby gem is a package that can be downloaded and installed as a part of your application you’re developing. They actually contain a packaged Ruby application or library. When you require an installed gem you’re adding extra functionality to your Ruby program.&lt;br&gt;
Gems can be used to extend or modify the functionality of your Ruby applications. What is great about them is that they can be distributed and shared with other Ruby coders so that they can use them in their own applications.&lt;/p&gt;




&lt;h1&gt;
  
  
  Examples of gems
&lt;/h1&gt;

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

&lt;p&gt;Gems make our life much easier, to the extent that we can save not hours, but weeks or months of coding time. By just adding the required gem to our application, we can quickly and easily integrate the feature it provides to our fresh baked application.&lt;br&gt;
Here are some of the extra functionalities, ruby gems can provide for our applications:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;em&gt;&lt;strong&gt;Authentication&lt;/strong&gt;&lt;/em&gt; (Devise, ruby-jwt, omniauth, etc)&lt;/li&gt;
&lt;li&gt;
&lt;em&gt;&lt;strong&gt;Authorization&lt;/strong&gt;&lt;/em&gt; (cancancan, pundit)&lt;/li&gt;
&lt;li&gt;
&lt;em&gt;&lt;strong&gt;Testing&lt;/strong&gt;&lt;/em&gt; (Rspec, shoulda-matchers, capybara, faker, etc.)&lt;/li&gt;
&lt;li&gt;
&lt;em&gt;&lt;strong&gt;Search&lt;/strong&gt;&lt;/em&gt; (elasticsearch)&lt;/li&gt;
&lt;li&gt;
&lt;em&gt;&lt;strong&gt;Admin panels&lt;/strong&gt;&lt;/em&gt; (activeadmin, administrate)&lt;/li&gt;
&lt;li&gt;and hundreds and thousands of more and more…&lt;/li&gt;
&lt;/ul&gt;




&lt;h1&gt;
  
  
  Where can I find them?
&lt;/h1&gt;

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

&lt;p&gt;You surely know about npm — node package manager and that it is a central point where you can search for and learn about javascript redistributable packages.&lt;br&gt;
For Ruby gems, you can go to the RubyGems website. It is the Ruby community’s gem hosting service, where you can find, learn about, install them and publish your own gems.&lt;br&gt;
There is yet another service where you can search for gems by category. It is “The ruby toolbox”, and it is a catalog of gems that keeps track of popularity and health metrics to help you choose a reliable library.&lt;/p&gt;




&lt;h1&gt;
  
  
  Managing Ruby Gems
&lt;/h1&gt;

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

&lt;p&gt;Of course, to manage the gems you use in your application you need a package manager. Something should be keeping track of the gems you installed and their versions. That’s where the Bundler comes in. Well, Bundler is a tool for dependency management. It provides a consistent environment for your projects by tracking and installing the exact gems and versions that are needed.&lt;br&gt;
Usually, you list the gems you use in a file called “Gemfile”. That’s the starting point where the Bundler analyzes and installs all the required gems to be used in the application.&lt;/p&gt;

&lt;p&gt;Here are some of the commands you would use to manage your gems in a project:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;em&gt;&lt;strong&gt;gem list&lt;/strong&gt;&lt;/em&gt;: Lists installed gems. Accepts an argument for filtering gems by name (example: gem list active)&lt;/li&gt;
&lt;li&gt;
&lt;em&gt;&lt;strong&gt;gem install  -v &lt;/strong&gt;&lt;/em&gt; : Allows you to install a specific gem version (example: gem install sinatra -v 2.0.0)&lt;/li&gt;
&lt;li&gt;
&lt;em&gt;&lt;strong&gt;bundle install&lt;/strong&gt;&lt;/em&gt;: Installs all the gems listed in the Gemfile&lt;/li&gt;
&lt;li&gt;
&lt;em&gt;&lt;strong&gt;bundle update&lt;/strong&gt;&lt;/em&gt;: Installs all the gems, and if required, updates specific gems to the latest version available.&lt;/li&gt;
&lt;/ul&gt;




&lt;h1&gt;
  
  
  Conclusion
&lt;/h1&gt;

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

&lt;p&gt;Before using a specific gem, please read its docs and analyze it thoroughly. The thing you should be considering is whether it exactly meets your requirements and whether it does not break your functionality when you decide to extend some of the features in your application.&lt;/p&gt;

&lt;p&gt;I mean it must not be a burden when you decide to extend some functionality or add a new feature to your application. If you are suspicious that it can, then search for another one that fits best for it. For that, you have to carefully read the docs it provides on its website or its GitHub Readme file. It is better if you try using it in a small sample application, before integrating it into your desired application.&lt;/p&gt;

&lt;p&gt;Here we come to the end of our article about Ruby gems, how to cook and how to eat them. I hope so much, it can be of use for anyone interested in Ruby, or maybe especially Ruby on Rails.&lt;/p&gt;

</description>
      <category>rails</category>
      <category>ruby</category>
      <category>gems</category>
      <category>beginners</category>
    </item>
    <item>
      <title>A short intro to React.js</title>
      <dc:creator>Merdan Durdyyev</dc:creator>
      <pubDate>Fri, 15 Jan 2021 12:16:13 +0000</pubDate>
      <link>https://dev.to/eminarium/a-short-intro-to-react-js-aed</link>
      <guid>https://dev.to/eminarium/a-short-intro-to-react-js-aed</guid>
      <description>&lt;h1&gt;
  
  
  Welcome
&lt;/h1&gt;

&lt;p&gt;Welcome, dear friends, dear readers, and guests.&lt;br&gt;&lt;br&gt;
As you all know, the software world is a general name for what coders do, and it has many subdivisions. The most important of them to count here are:  &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;em&gt;Backend (What is happening on the server-side)&lt;/em&gt;&lt;/li&gt;
&lt;li&gt;&lt;em&gt;Frontend (What is happening on the client-side)&lt;/em&gt;&lt;/li&gt;
&lt;li&gt;&lt;em&gt;Mobile (Dealing with mobile devices and mobile apps)&lt;/em&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Among them, Frontend and Mobile products are used and seen visually by a client/user. A user/client does not see what is happening on the server part and it does not interest him. The part the user sees mainly appears on the browser. And this part is called the “Frontend” part of the programming world and it has a strong competition between the tools that are being used for it.  &lt;/p&gt;

&lt;p&gt;Anything we see on the browser, all the web pages, all the forms that we fill in, all the “like” and “star” buttons that we press are developed by one of the “Frontend” tools.  &lt;/p&gt;

&lt;p&gt;Now, let’s take a closer look at one of the widely used tools, called “React.js”.  &lt;/p&gt;




&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fugru6nv01w8oxdlprgky.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fugru6nv01w8oxdlprgky.gif" alt="Alt Text" width="760" height="391"&gt;&lt;/a&gt;  &lt;/p&gt;

&lt;h1&gt;
  
  
  What is React.js ?
&lt;/h1&gt;

&lt;p&gt;“React.js”, is a library developed by a team at “Facebook”, used for “Frontend” development. Its first release was in May 2013. The library’s resource page is located at &lt;a href="https://reactjs.org/" rel="noopener noreferrer"&gt;https://reactjs.org/&lt;/a&gt;. According to the 6-word sentence placed at the center of its web page, it states:  &lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;A JavaScript library for building user interfaces&lt;/strong&gt;  &lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;For general statistics of its usage and popularity, you can take a look at its Github page, which is located at &lt;a href="https://github.com/facebook/react" rel="noopener noreferrer"&gt;https://github.com/facebook/react&lt;/a&gt;.  &lt;/p&gt;

&lt;p&gt;The main advantage of React.js is that it allows a developer to decompose a large UI project into smaller reusable parts. These parts can later be used within another React.js based project or a web page. These reusable parts are called &lt;strong&gt;“Components“&lt;/strong&gt;. You can separate each button, each text line, an icon, a logo, or a menu as another Component.  &lt;/p&gt;

&lt;p&gt;Another noticeable advantage of this library is that it allows composing both class-based and function-based (functional) components. As you may have already heard, functional programming is a trend nowadays. And this advantage of “React.js” also attracts those who are fans of functional programming.  &lt;/p&gt;




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

&lt;h1&gt;
  
  
  Friends and opponents
&lt;/h1&gt;

&lt;p&gt;In the world of Frontend, there are many other notable libraries and frameworks that go racing with “React.js”. Below is the list of the most used ones:  &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;em&gt;Angular&lt;/em&gt;&lt;/li&gt;
&lt;li&gt;&lt;em&gt;Vue.js&lt;/em&gt;&lt;/li&gt;
&lt;li&gt;&lt;em&gt;Backbone.js&lt;/em&gt;&lt;/li&gt;
&lt;li&gt;&lt;em&gt;Ember.js&lt;/em&gt;&lt;/li&gt;
&lt;li&gt;&lt;em&gt;jQuery&lt;/em&gt;&lt;/li&gt;
&lt;li&gt;&lt;em&gt;Svelte.js&lt;/em&gt;&lt;/li&gt;
&lt;li&gt;&lt;em&gt;Meteor.js&lt;/em&gt;&lt;/li&gt;
&lt;li&gt;&lt;em&gt;Polymer.js&lt;/em&gt;&lt;/li&gt;
&lt;li&gt;&lt;em&gt;Ext.js&lt;/em&gt;&lt;/li&gt;
&lt;li&gt;
&lt;em&gt;and etc.&lt;/em&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The most important thing we must underline is not the quantity of the Frontend tools, but their variety, diversity. For instance, there might be hundreds of auto producers. But they have priorities, advantages, and disadvantages for different countries, cultures, and different weather conditions. Some produce good cars for countries with hot weather, some produce vehicles durable in freezing cold. Some vehicles go smoothly only on plain surfaces and are very stylish, and some are all-terrain vehicles but not so stylish.  &lt;/p&gt;

&lt;p&gt;All the same, libraries have pros and cons depending on the project requirements. Some are fully compatible with “Google Chrome” or “Chrome Project” browsers, some run smoothly on “Internet Explorer”. Some are very fast to visualize but has fewer beautiful elements, others might be slower but have very attractive ready to use UI elements.  &lt;/p&gt;

&lt;p&gt;In general, the most important Frontend tools that lead the race these days are &lt;strong&gt;“React.js”&lt;/strong&gt;, &lt;strong&gt;“Angular”&lt;/strong&gt; and &lt;strong&gt;“Vue.js”&lt;/strong&gt;.  &lt;/p&gt;




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

&lt;h1&gt;
  
  
  Behind the curtains
&lt;/h1&gt;

&lt;p&gt;To make developers' life easier, React.js provides us with a syntax extension called &lt;strong&gt;JSX&lt;/strong&gt;. We call it &lt;strong&gt;“JSX — Javascript XML”&lt;/strong&gt; otherwise. With the help of JSX, we can embed HTML code into React code.  &lt;/p&gt;

&lt;p&gt;As you can see from the picture above, you can embed HTML tags directly into the React code. All the written code then is converted to a simple &lt;strong&gt;HTML, CSS, and Javascript combination&lt;/strong&gt;, with the help of a transpiler, called &lt;strong&gt;“Babel”&lt;/strong&gt;.  &lt;/p&gt;




&lt;h1&gt;
  
  
  Sum it up
&lt;/h1&gt;

&lt;p&gt;If you are planning to conquer the world of Frontend, I recommend starting with one of those three leaders. Personally, I am a fan of &lt;strong&gt;“React.js”&lt;/strong&gt;. But, according to ratings, job postings, and the number of developers using them, &lt;strong&gt;“Angular”&lt;/strong&gt; and &lt;strong&gt;“Vue.js”&lt;/strong&gt; are also on top of the hills.  &lt;/p&gt;

&lt;p&gt;One thing to note, before diving into any of them, is the learning process. The complexity of learning each of them is different. According to the analysis I have conducted, arguments noted in blogs, and some statistics, “React.js” has an easy and fast learning path.  &lt;/p&gt;

&lt;p&gt;Besides that, after learning React.js, you can easily shift to &lt;strong&gt;React Native&lt;/strong&gt;. React Native also has a large user community and provides you with tools to develop cross-platform (both Android and iOS) mobile apps.  &lt;/p&gt;

&lt;p&gt;Good luck to you starting your journey in the Frontend world. Stay home, stay healthy, stay hungry for learning!  &lt;/p&gt;

</description>
      <category>beginners</category>
      <category>codenewbie</category>
      <category>reactnative</category>
      <category>react</category>
    </item>
    <item>
      <title>A short intro into classes in Ruby</title>
      <dc:creator>Merdan Durdyyev</dc:creator>
      <pubDate>Wed, 25 Nov 2020 16:08:50 +0000</pubDate>
      <link>https://dev.to/eminarium/a-short-intro-into-classes-in-ruby-4cgn</link>
      <guid>https://dev.to/eminarium/a-short-intro-into-classes-in-ruby-4cgn</guid>
      <description>&lt;p&gt;Welcome, dear friends, readers, and coders! This article will be dedicated to classes in the Ruby programming language.  &lt;/p&gt;

&lt;h1&gt;
  
  
  OOP (Object Oriented Programming)
&lt;/h1&gt;

&lt;p&gt;Before starting classes, let’s define what OOP is. In short, OOP, or Object Oriented Programming is a concept used in almost all programming languages that let us define anything in the digital world by making it resemble a real-world object. So, what does that mean?  &lt;/p&gt;

&lt;p&gt;As we all know, any object in the real-world has attributes and movements. Otherwise, the attributes of an object are its properties, and its movements are its actions or methods. This way, we can model any type of object in a digital world by providing it with real-world object features.  &lt;/p&gt;

&lt;p&gt;Let’s try to define a car:  &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Properties :&lt;/strong&gt;  &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;em&gt;color&lt;/em&gt;&lt;/li&gt;
&lt;li&gt;&lt;em&gt;price&lt;/em&gt;&lt;/li&gt;
&lt;li&gt;&lt;em&gt;brand&lt;/em&gt;&lt;/li&gt;
&lt;li&gt;&lt;em&gt;model&lt;/em&gt;&lt;/li&gt;
&lt;li&gt;&lt;em&gt;manufacture date&lt;/em&gt;&lt;/li&gt;
&lt;li&gt;
&lt;em&gt;and etc.&lt;/em&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Methods:&lt;/strong&gt;  &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;em&gt;start&lt;/em&gt;&lt;/li&gt;
&lt;li&gt;&lt;em&gt;stop&lt;/em&gt;&lt;/li&gt;
&lt;li&gt;&lt;em&gt;turn&lt;/em&gt;&lt;/li&gt;
&lt;li&gt;&lt;em&gt;accelerate&lt;/em&gt;&lt;/li&gt;
&lt;li&gt;&lt;em&gt;brake&lt;/em&gt;&lt;/li&gt;
&lt;li&gt;
&lt;em&gt;and etc.&lt;/em&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;This way we defined a real-world object with its features and now we can model it in a digital world  &lt;/p&gt;




&lt;h1&gt;
  
  
  Classes/Objects and Methods
&lt;/h1&gt;

&lt;p&gt;As we mentioned earlier, we clarify its properties/attributes and actions/methods when we define objects. We have two conflicting words here. They are “Class” and “Object”.  &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;“Class”: a drawing, a draft project of an object, a block of code defining and object.&lt;/li&gt;
&lt;li&gt;“Object”: A working, a living model of a block of code that was defined as a “Class”.
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;We can compare it to preparing drafts before manufacturing a car, or preparing drawings before building a building, or writing draft notes before writing a novel itself.  &lt;/p&gt;




&lt;h1&gt;
  
  
  Examples of classes and objects
&lt;/h1&gt;

&lt;p&gt;So now, let’s define some classes and start using them as 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="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Car&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="n"&gt;car_brand&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;car_model&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
      &lt;span class="vi"&gt;@brand&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;car_brand&lt;/span&gt;
      &lt;span class="vi"&gt;@model&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;car_model&lt;/span&gt;
   &lt;span class="k"&gt;end&lt;/span&gt;   

   &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;info&lt;/span&gt;
      &lt;span class="nb"&gt;puts&lt;/span&gt; &lt;span class="s2"&gt;"Car info"&lt;/span&gt;
      &lt;span class="nb"&gt;puts&lt;/span&gt; &lt;span class="s2"&gt;"Brand : "&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="vi"&gt;@brand&lt;/span&gt;
      &lt;span class="nb"&gt;puts&lt;/span&gt; &lt;span class="s2"&gt;"Model : "&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="vi"&gt;@model&lt;/span&gt;
   &lt;span class="k"&gt;end&lt;/span&gt;   

   &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;start&lt;/span&gt;
      &lt;span class="nb"&gt;puts&lt;/span&gt; &lt;span class="s2"&gt;"Car was started!"&lt;/span&gt;
   &lt;span class="k"&gt;end&lt;/span&gt;

   &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;stop&lt;/span&gt;
      &lt;span class="nb"&gt;puts&lt;/span&gt; &lt;span class="s2"&gt;"Car was stopped!"&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;p&gt;You can see, in the code above, that we have defined a class named “Car”, with its properties and methods.&lt;br&gt;&lt;br&gt;
Properties of the class are “brand” and “model”. You might have noticed that they are brought to use with the help of “initialize” method.&lt;br&gt;&lt;br&gt;
Besides those, we have several methods named “info”, “start”, and “stop” respectively. All of them are performing different tasks. Now, if we want to use this, we need to create an instance of the class, and it becomes a new 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="n"&gt;tesla_model_s&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="no"&gt;Car&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="s2"&gt;"Tesla"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"Model S"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="n"&gt;opel_astra&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="no"&gt;Car&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="s2"&gt;"Opel"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"Astra"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="n"&gt;toyota_camry&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="no"&gt;Car&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="s2"&gt;"Toyota"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"Camry"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="n"&gt;toyota_camry&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;info&lt;/span&gt;
&lt;span class="c1"&gt;# "Car info"&lt;/span&gt;
&lt;span class="c1"&gt;# "Brand : Toyota"&lt;/span&gt;
&lt;span class="c1"&gt;# "Model : Camry"&lt;/span&gt;

&lt;span class="n"&gt;opel_astra&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;start&lt;/span&gt;
&lt;span class="c1"&gt;# "Car was started!"&lt;/span&gt;

&lt;span class="n"&gt;opel_astra&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;stop&lt;/span&gt;
&lt;span class="c1"&gt;# "Car was stopped!"&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;I think now it is clear what we wanted to explain earlier about classes and objects. “Car” class was put into production and several new objects were created. This means, “tesla_model_s”, “opel_astra”, “toyota_camry” are unique objects. All of them have properties of different values and methods.&lt;/p&gt;




&lt;h1&gt;
  
  
  “Built-in” and “User-defined” classes
&lt;/h1&gt;

&lt;p&gt;In one of my previous articles about Ruby, we have mentioned &lt;strong&gt;built-in&lt;/strong&gt; and &lt;strong&gt;user-defined&lt;/strong&gt; functions, in an article about functions.&lt;br&gt;
The same as in functions, there are &lt;strong&gt;built-in&lt;/strong&gt; classes. If they are not covering our requirements, and if we need our own classes (like “Car” class), then we write our own &lt;strong&gt;user-defined&lt;/strong&gt; classes.  &lt;/p&gt;

&lt;p&gt;Here are some examples of &lt;strong&gt;built-in&lt;/strong&gt; classes:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;String&lt;/li&gt;
&lt;li&gt;Math&lt;/li&gt;
&lt;li&gt;Float&lt;/li&gt;
&lt;li&gt;Array&lt;/li&gt;
&lt;li&gt;Time
&lt;/li&gt;
&lt;/ul&gt;




&lt;h1&gt;
  
  
  Goodbye
&lt;/h1&gt;

&lt;p&gt;Here we finalize our short article about classes and OOP in Ruby. Hundreds of pages can be written about OOP, classes, objects, and the object-oriented architecture in whole. But we restrict ourselves with the minimum written here. You can get tons of extra info on the web if you want to dive deeper. So, never stop learning. Good luck!&lt;/p&gt;

</description>
      <category>ruby</category>
      <category>oop</category>
      <category>codenewbie</category>
      <category>tutorial</category>
    </item>
    <item>
      <title>Functions in Ruby</title>
      <dc:creator>Merdan Durdyyev</dc:creator>
      <pubDate>Tue, 24 Nov 2020 16:05:56 +0000</pubDate>
      <link>https://dev.to/eminarium/functions-in-ruby-30a9</link>
      <guid>https://dev.to/eminarium/functions-in-ruby-30a9</guid>
      <description>&lt;h1&gt;
  
  
  Welcome
&lt;/h1&gt;

&lt;p&gt;Welcome back dear readers, coders, and guests! This time we are going to go through functions and how to use them in our beloved Ruby!&lt;br&gt;
First of all, we have to underline that it is with the help of functions that we can reduce thousands of lines of code to maybe hundreds or tens or even less. They let us decrease code size, optimize it, and simplify.&lt;br&gt;
Depending on the situation we can use so-called &lt;strong&gt;“built-in functions”&lt;/strong&gt; or &lt;strong&gt;”user-defined functions“&lt;/strong&gt;. We will mainly focus on the &lt;strong&gt;“user-defined-functions”&lt;/strong&gt; in this article.  &lt;/p&gt;


&lt;h1&gt;
  
  
  What is a function?
&lt;/h1&gt;

&lt;p&gt;They can be called building blocks of large programs. They let us simplify large blocks of repetitive code. By naming them correspondingly, we can call them later in any part of the program. Without them, we would have to write the same block of code many times within the program.  &lt;/p&gt;

&lt;p&gt;To cut it short, a function is a named block of code. Let’s dive a little deeper in the details:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;&lt;em&gt;A function has a unique name&lt;/em&gt;&lt;/strong&gt;;&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;&lt;em&gt;A function can get input data&lt;/em&gt;&lt;/strong&gt;;&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;&lt;em&gt;A function can return a value&lt;/em&gt;&lt;/strong&gt;.
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;A function and a procedure&lt;/strong&gt;: The word &lt;strong&gt;‘function’&lt;/strong&gt; actually comes from maths. If we pay attention to a function in maths, they always return a result. Because of this, in earlier programming languages, some blocks of code were called &lt;strong&gt;“a function”&lt;/strong&gt;, and some &lt;strong&gt;“a procedure”&lt;/strong&gt;, depending on whether it returns a result or not. You can look for it in &lt;strong&gt;“Pascal”&lt;/strong&gt; programming language. But in modern programming languages, the difference smoothly disappeared and now they are all called functions.  &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Input data&lt;/strong&gt;: Depending on what you are trying to achieve, you sometimes need initial data to calculate something or perform some kind of operations. For instance:  &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;em&gt;You need the guest's name to say hello to him by his name&lt;/em&gt;;&lt;/li&gt;
&lt;li&gt;
&lt;em&gt;You need monthly income and expenditure reports to find out whether the company is doing good&lt;/em&gt;;&lt;/li&gt;
&lt;li&gt;&lt;em&gt;You need quantity and unit prices of products to calculate weekly sale report in a store.&lt;/em&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The same logic works in programming. You sometimes need some kind of initial data to calculate something. We call them &lt;strong&gt;“parameters”&lt;/strong&gt;.  &lt;/p&gt;

&lt;p&gt;When we call a function and pass them initial data that we previously called &lt;strong&gt;“parameters”&lt;/strong&gt; while defining the function, are now called &lt;strong&gt;“arguments”&lt;/strong&gt;. When we list initial data while defining a function, they are called &lt;strong&gt;“parameters”&lt;/strong&gt;. And when we list the initial data while calling the function, they are called &lt;strong&gt;“arguments”&lt;/strong&gt;.  &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Output/result&lt;/strong&gt;: As we already mentioned above, some code blocks return a result, and some don’t. We generally use &lt;strong&gt;“return”&lt;/strong&gt; reserved worked (keyword) to return a result from the function.  &lt;/p&gt;


&lt;h1&gt;
  
  
  Function Examples
&lt;/h1&gt;

&lt;p&gt;We need to use the &lt;strong&gt;'def'&lt;/strong&gt; keyword to define a function in Ruby.&lt;br&gt;&lt;br&gt;
Now let’s see some blocks of code about how to define functions:&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;# No input data, no output&lt;/span&gt;
&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;say_hello&lt;/span&gt;
   &lt;span class="nb"&gt;puts&lt;/span&gt; &lt;span class="s2"&gt;"Hello Ruby"&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;

&lt;span class="n"&gt;say_hello&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;  &lt;span class="c1"&gt;# "Hello Ruby"&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The function above does not accept input and does not return the output result. Now, let’s give it an input data:&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;# Gets input data but no returns no output&lt;/span&gt;
&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;say_hello&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;guest_name&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;"Hello &lt;/span&gt;&lt;span class="si"&gt;#{&lt;/span&gt;&lt;span class="n"&gt;guest_name&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;

&lt;span class="n"&gt;say_hello&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"John"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;  &lt;span class="c1"&gt;# "Hello John"&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now let’s make it accept input and return a result:&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;# Accepts input and returns an output&lt;/span&gt;
&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;say_hello&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;guest_name&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
   &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="s2"&gt;"Hello &lt;/span&gt;&lt;span class="si"&gt;#{&lt;/span&gt;&lt;span class="n"&gt;guest_name&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;

&lt;span class="n"&gt;say_hello&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"John"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;  &lt;span class="c1"&gt;# Nothing appears on the screen&lt;/span&gt;

&lt;span class="c1"&gt;# To display the result on the screen,&lt;/span&gt;
&lt;span class="c1"&gt;# we have to pass the output result to &lt;/span&gt;
&lt;span class="c1"&gt;# 'puts' function&lt;/span&gt;
&lt;span class="nb"&gt;puts&lt;/span&gt; &lt;span class="n"&gt;say_hello&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"John"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;  &lt;span class="c1"&gt;# "Hello John" is displayed on the screen.&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;As we see in the last example, &lt;strong&gt;‘guest_name’&lt;/strong&gt; is a parameter, and &lt;strong&gt;‘John’&lt;/strong&gt; is an argument.&lt;/p&gt;




&lt;h1&gt;
  
  
  Parameters/Arguments and output result
&lt;/h1&gt;

&lt;p&gt;If we are providing multiple data to the function, we list them and separate them with the help of a comma. The most important part is that we have to list them in the same order when we are calling the function, as we listed them when defining the function. If we first provided the first name and then the last name, we list them in the same order when calling the function, first name, and then the last name. For example:&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;say_hello&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;first_name&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;last_name&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;age&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;"Hello &lt;/span&gt;&lt;span class="si"&gt;#{&lt;/span&gt;&lt;span class="n"&gt;first_name&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="s2"&gt; &lt;/span&gt;&lt;span class="si"&gt;#{&lt;/span&gt;&lt;span class="n"&gt;last_name&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;, You are &lt;/span&gt;&lt;span class="si"&gt;#{&lt;/span&gt;&lt;span class="n"&gt;age&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="s2"&gt; years old"&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;

&lt;span class="n"&gt;say_hello&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"John"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"Brown"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;25&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="c1"&gt;# "Hello John Brown, You are 25 years old"&lt;/span&gt;

&lt;span class="n"&gt;say_hello&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;18&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"John"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"Brown"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="c1"&gt;# "Hello 18 John, You are Brown years old"&lt;/span&gt;
&lt;span class="c1"&gt;# In this example, the argument order is wrong.&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In strictly typed programming languages, together with the parameter name, you provide its type. Be it an &lt;strong&gt;‘integer’&lt;/strong&gt;, a &lt;strong&gt;‘string’&lt;/strong&gt;, a &lt;strong&gt;‘character’&lt;/strong&gt;, or a &lt;strong&gt;‘boolean’&lt;/strong&gt;, you will have to indicate it together with the parameter name. In some programming languages, a compiler warns or returns an error when the wrong type of parameters are provided to the function.&lt;/p&gt;

&lt;p&gt;In modern programming languages like &lt;strong&gt;“Ruby”&lt;/strong&gt;, &lt;strong&gt;“Python”&lt;/strong&gt; and etc., you don’t have to provide types with parameter names. It’s on the shoulders of the developer to provide the right type of data to the function.&lt;/p&gt;

&lt;p&gt;We don’t always return a result from a function. But when we do, we usually use the &lt;strong&gt;‘return’&lt;/strong&gt; keyword. But in Ruby, the last part of the function block is returned as a result. It is surely a savior for the programmer. But not all the Ruby programmers get used to it and they continue using the &lt;strong&gt;‘return’&lt;/strong&gt; keyword to return a result. Here is a sample code:&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;print_bill&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;unit_price&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;quantity&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
   &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;unit_price&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;quantity&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;

&lt;span class="c1"&gt;# 4 items sold with price 3.5 USD.&lt;/span&gt;
&lt;span class="nb"&gt;puts&lt;/span&gt; &lt;span class="n"&gt;print_bill&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mf"&gt;3.5&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="c1"&gt;# Output is '14.0'.&lt;/span&gt;

&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;print_bill_2&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;unit_price&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;quantity&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
   &lt;span class="s2"&gt;"lala lala"&lt;/span&gt;
   &lt;span class="n"&gt;unit_price&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;quantity&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;

&lt;span class="c1"&gt;# 4 items with unit price 3.5 are sold.&lt;/span&gt;
&lt;span class="nb"&gt;puts&lt;/span&gt; &lt;span class="n"&gt;print_bill_2&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mf"&gt;3.5&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="c1"&gt;#  Output is '14.0'&lt;/span&gt;
&lt;span class="c1"&gt;# The last line has 'unit_price*quantity', so it is the result.&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h1&gt;
  
  
  “Built-in” functions
&lt;/h1&gt;

&lt;p&gt;To make developers’ life easier, all programming languages have ready to use built-in functions. With the help of them, we can write code faster. It would have been a disaster without them.  &lt;/p&gt;

&lt;p&gt;All the programming languages have built-in methods/functions for maths calculations and to operate on Strings. Let’s look at some of them:&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;# Make all characters capital - 'capitalize'&lt;/span&gt;
&lt;span class="s2"&gt;"hello"&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;capitalize&lt;/span&gt;  &lt;span class="c1"&gt;# "HELLO"&lt;/span&gt;

&lt;span class="c1"&gt;# Converts data to string - 'to_s'&lt;/span&gt;
&lt;span class="mi"&gt;25&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;to_s&lt;/span&gt;  &lt;span class="c1"&gt;# "25"&lt;/span&gt;

&lt;span class="c1"&gt;# Returns the square root of a given number - 'sqrt'&lt;/span&gt;
&lt;span class="no"&gt;Math&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;sqrt&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;25&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;  &lt;span class="c1"&gt;# 5&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;There are tons of built-in functions/methods like the ones we have just mentioned. Their number depends on the programming language. You can take a look at them on the official documentation web pages of the corresponding programming languages.&lt;/p&gt;




&lt;h1&gt;
  
  
  The End
&lt;/h1&gt;

&lt;p&gt;Finally, we are at the end of the article about functions in Ruby. It means, later on, our code will be shorter, simpler, more productive, and more professional. It adds beauty at the same time. So, never stop learning and optimizing, refining, and refactoring your code. Move one step further by learning something new every other day. Goodbye for now. Good Luck!&lt;/p&gt;

</description>
      <category>ruby</category>
      <category>tutorial</category>
      <category>codenewbie</category>
      <category>beginners</category>
    </item>
    <item>
      <title>Ranges in Ruby</title>
      <dc:creator>Merdan Durdyyev</dc:creator>
      <pubDate>Mon, 23 Nov 2020 15:02:28 +0000</pubDate>
      <link>https://dev.to/eminarium/ranges-in-ruby-254n</link>
      <guid>https://dev.to/eminarium/ranges-in-ruby-254n</guid>
      <description>&lt;h1&gt;
  
  
  Welcome
&lt;/h1&gt;

&lt;p&gt;Welcome back, dear friends! Welcome newcomers, coders, and enthusiasts! Today we are going to have a look at how to use Ranges in our beloved Ruby PL. To note first, Ranges are a data structure that is not present in many programming languages. It became popular in modern programming languages and it just represents a slice of a previously defined and ordered set of elements.&lt;/p&gt;

&lt;h1&gt;
  
  
  What is a Range?
&lt;/h1&gt;

&lt;p&gt;We come across Ranges in many daily situations. For instance:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Month range: between &lt;strong&gt;“April — September”&lt;/strong&gt;
&lt;/li&gt;
&lt;li&gt;Number range: between &lt;strong&gt;“5–8”&lt;/strong&gt;
&lt;/li&gt;
&lt;li&gt;Letter range: between &lt;strong&gt;“d — h”&lt;/strong&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The existence of such a class releases us from the burden of creating an Array of ordered elements in some cases. If we need a slice of elements, say from 20-th to 30-th one, we don’t have to define them as Array. Listing all the required elements will take time. In the examples above, we are free from defining those letters, numbers, months, and month dates as separate Arrays. We just show the starting element and ending element and separate it as a Range.&lt;/p&gt;

&lt;p&gt;To define Ranges, we put dots between the starting and ending elements. Here is what they mean:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;(3..8)&lt;/strong&gt;: “two dots”, includes the ending element, (3, 4, 5, 6, 7, 8)&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;(3…8)&lt;/strong&gt;: “three dots”, does not include the ending element, (3, 4, 5, 6, 7)&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Here are some examples of these:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;(1..5)        #==&amp;gt; 1, 2, 3, 4, 5
(1...5)       #==&amp;gt; 1, 2, 3, 4
('a'..'d')    #==&amp;gt; 'a', 'b', 'c', 'd'
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h1&gt;
  
  
  Create a new Range
&lt;/h1&gt;

&lt;p&gt;To create a new Range we just write the starting and ending element values and put two or three dots between them. We have already covered what those dots mean in the section above. To print the Range you can use the “to_a” method. This way you convert the Range to an Array.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;(-1..-5).to_a      #=&amp;gt; []
(-5..-1).to_a      #=&amp;gt; [-5, -4, -3, -2, -1]
('a'..'e').to_a    #=&amp;gt; ["a", "b", "c", "d", "e"]
('a'...'e').to_a   #=&amp;gt; ["a", "b", "c", "d"]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The interesting part of this is that you can do something like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;range1 = ('bar'..'bav').to_a

puts "#{range1}"
# Output: ["bar", "bas", "bat", "bau", "bav"]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;As you see, Ruby PL automagically understands what values to insert in between. Bravo “Ruby”!&lt;/p&gt;

&lt;h1&gt;
  
  
  Range methods
&lt;/h1&gt;

&lt;p&gt;There are some widely used methods with Ranges. Here they are:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;include?&lt;/strong&gt; : Returns “true” or “false”. Checks whether the requested element is present in the given Range.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;max&lt;/strong&gt;: Returns the maximum element in the Range.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;min&lt;/strong&gt;: Returns the minimum element in the Range.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;reverse&lt;/strong&gt;: Reverses the Range.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Let’s see some examples of how to use them:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;# 'include?' method
puts ("a".."z").include?("g")   #=&amp;gt; true
puts ("a".."z").include?("A")   #=&amp;gt; false
puts ("a".."z").include?("cc")  #=&amp;gt; false

digits = 0..9
puts digits.include?(5) # true

# 'max' and 'min' methods
puts (10..20).max    #=&amp;gt; 20
puts (10..20).min    #=&amp;gt; 10

# 'reverse' method
puts (1..5).to_a.reverse

# 'first' method
(10..20).first     #=&amp;gt; 10
(10..20).first(3)  #=&amp;gt; [10, 11, 12]

# 'last' method
(10..20).last      #=&amp;gt; 20
(10...20).last     #=&amp;gt; 20
(10..20).last(3)   #=&amp;gt; [18, 19, 20]
(10...20).last(3)  #=&amp;gt; [17, 18, 19]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h1&gt;
  
  
  Iterating over Range elements
&lt;/h1&gt;

&lt;p&gt;As we already have seen in Arrays and Hashes, we can iterate through them using the ‘each’ method. It means we can also use that method for iterating over a Range. We can use this code snippet to print all the elements of a Range:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;numbers = (15..20) 

numbers.each do |num|
  puts "Next number = #{num}"
end

# Next number = 15
# Next number = 16
# Next number = 17
# Next number = 18
# Next number = 19
# Next number = 20
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Exactly the same way, you can iterate over a letter Range.&lt;/p&gt;

&lt;h1&gt;
  
  
  Using Ranges in conditional checks
&lt;/h1&gt;

&lt;p&gt;If we take a look at how we use Ranges in conditionals, we can recognize that they are mostly used in the “case-when” conditional structure. To be precise, we check if the subject item is in between the given Range. In other programming languages, the same conditional structure is otherwise called ‘switch-case’, and using that we can only check for equality to a certain value, not an inclusion in the Range. But in Ruby, we can check if a subject value falls in the given Range. For instance:&lt;br&gt;
&lt;/p&gt;

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

result = case grade
   when 0...50 then "Sorry, you failed!"
   when 51..60 then "That was hard, but you passed!"
   when 61..70 then "Not bad!"
   when 71..80 then "Satisfactory!"
   when 81..100 then "Great, you are a Superman!"
   else "Incorrect grade !"
end

puts result
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h1&gt;
  
  
  Conclusion
&lt;/h1&gt;

&lt;p&gt;Dear friends, here we came to the end of the article. One thing we have to remind ourselves once more is that the “Range” class is present in not so many programming languages. This shows to what extend Ruby developers thought of software developers' comfort.&lt;br&gt;
Ruby philosophy has such a point: “Developer happiness first”. This, once again underlines that Ruby is for human beings :-)&lt;br&gt;
So much comfort is provided for us. So let’s learn the programming language that puts developers' happiness first.&lt;br&gt;
See you in the next article. Goodbye!&lt;/p&gt;

</description>
      <category>ruby</category>
      <category>beginners</category>
      <category>codenewbie</category>
      <category>tutorial</category>
    </item>
    <item>
      <title>Hashes in Ruby</title>
      <dc:creator>Merdan Durdyyev</dc:creator>
      <pubDate>Sun, 22 Nov 2020 23:43:19 +0000</pubDate>
      <link>https://dev.to/eminarium/hashes-in-ruby-2mhk</link>
      <guid>https://dev.to/eminarium/hashes-in-ruby-2mhk</guid>
      <description>&lt;h1&gt;
  
  
  Welcome
&lt;/h1&gt;

&lt;p&gt;Good day, dear friends, coders, and enthusiasts! Let’s walk through one more thing in our beloved Ruby PL. Come and learn something new and add up to our knowledge base. This time it will be Hashes and how we use them in Ruby.  &lt;/p&gt;

&lt;h1&gt;
  
  
  What is a Hash?
&lt;/h1&gt;

&lt;p&gt;The same as Arrays, Hashes are a set of elements with certain features. At a first sight, Hashes kind of resemble Arrays in some aspect. As you know, elements in Arrays each have an ordering number, i.e. index. On the other side, Hash elements have keys, that are unique for each of the Hash elements. So, each element in Hash is a pair of key and its value. Let’s take a look at the example:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Names of countries and their shortcodes (TKM=&amp;gt;Turkmenistan, ESP=&amp;gt;Spain, NZ =&amp;gt;New Zealand and etc.)&lt;/li&gt;
&lt;li&gt;Word and its translation in a dictionary (cat =&amp;gt; кошка, dog =&amp;gt; собака and etc.)&lt;/li&gt;
&lt;li&gt;Domain address and its IP (&lt;a href="http://www.medium.com" rel="noopener noreferrer"&gt;www.medium.com&lt;/a&gt; =&amp;gt; 192.159.043.129 and etc.)&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;As you see in the example above, each pair is accepted as an element in Hash. If we accept the element index as part of the element in Arrays, they can now resemble Hash elements.&lt;br&gt;&lt;br&gt;
According to the example above:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;NZ&lt;/strong&gt; — is a key / &lt;strong&gt;New Zealand&lt;/strong&gt; — is a value&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;cat&lt;/strong&gt; — is a key / &lt;strong&gt;кошка&lt;/strong&gt; — is a value&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;&lt;a href="http://www.medium.com" rel="noopener noreferrer"&gt;www.medium.com&lt;/a&gt;&lt;/strong&gt; — is a key / &lt;strong&gt;192.159.043.129&lt;/strong&gt; — is a value.
and etc.
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;It means that, when we refer to the ‘cat’ key, we can get its value ‘кошка’. When referring to the key ‘NZ’ key, we can get its value ‘New Zealand’. We can conclude that the ordering in Hashes is not important. The important part is that the elements have unique keys and when that we can access element (pair) values by referring to the keys.  &lt;/p&gt;

&lt;p&gt;One more thing to note is that each key in a Hash must be unique. When we refer to the ‘NZ’ key, it must return its ‘New Zealand’ value.  &lt;/p&gt;

&lt;p&gt;But values can in different elements can be the same. Values do not have to be unique. For instance:  &lt;/p&gt;

&lt;p&gt;{NZ =&amp;gt; ‘New Zealand’, NZE =&amp;gt; ‘New Zealand’, ES =&amp;gt; ‘Spain’, ESP =&amp;gt; ‘Spain’, NZL =&amp;gt; ‘New Zealand’, SPN =&amp;gt; ‘Spain’}  &lt;/p&gt;

&lt;p&gt;What’s important is that each key must be unique, so that the Hash knows which pair we refer to when we provide a key to its element. But values can be the same in many of its elements.&lt;/p&gt;
&lt;h1&gt;
  
  
  Creating a new Hash element
&lt;/h1&gt;

&lt;p&gt;Hashes also have special brackets. We use curly brackets to define a Hash. We also use colons to separate key and value pairs.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;# An empty hash 
{}

# Smartphones and developer companies
phones = {Mi_Note_9: 'Xiao Mi', Galaxy_A5: "Samsung", Iphone_10: 'Apple', Diamond_S34: 'Samsung', Mi_Super_X: 'Xiao Mi'}

# Products and quantities
products = {monitor: 4, keyboard: 13, watch: 38, mouse: 10, door: 7}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;As you see in the example above, ‘Xiao Mi’ or ‘Samsung’ companies have many phones, that is, values are the same in many elements, but the keys are all unique. So, when we refer to the unique key ‘Diamond_s34’, it returns its element ‘Samsung’.&lt;br&gt;
In the example above, in each pair divided by comma, the one on the left is the key and the one on the right is the value of each element.&lt;br&gt;&lt;br&gt;
You can store any kind of object as a key or as a value in the Hash element.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;months = Hash.new
months = {"1": "January", "2": "February", "3": "March"
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h1&gt;
  
  
  Accessing and Updating Hash elements
&lt;/h1&gt;

&lt;p&gt;To access elements in the Hash, we use square brackets [], as it was in Arrays. We write the key we want to get in the square brackets.  &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;If there is such a key in the Hash, then you get its value.&lt;/li&gt;
&lt;li&gt;If there is no such key in the Hash, then that key and its value are added to the Hash.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;So let's access existing elements and add some new to the Hash.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;# Fruits and prices
fruits = {ananas: 12.7, banana: 4.9}
puts fruits[:ananas]  # 12.7
puts fruits[:apple] # 'nil', because there is no such key yet

# This way we can add new element to the Hash
fruits[:apple] = 3.6
fruits[:lemon] = 5.35

# New Hash {ananas: 12.7, banana: 4.9, apple: 3.6, lemon: 5.35}
puts fruits[:lemon] # 5.35
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If we want to search for a specific element in the Hash, we have two options. We can search for its key or its value. Depending on what we search for, we use corresponding methods for it. These methods return ‘true’ or ‘false’. Otherwise, they let you know whether such key or value exists in the Hash. &lt;br&gt;
Here they are:  &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;has_key?(key), key?(key)&lt;/strong&gt; : Checks whether we have such a key in the Hash.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;has_value?(value), value?(value)&lt;/strong&gt; : Checks whether we have such a value in the Hash.
&lt;/li&gt;
&lt;/ul&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;countries = {ES: 'Spain', TM: 'Turkmenistan', NZ: 'New Zealand'}
puts countries.has_key?(:ES) # true
puts countries.key?(:GB) # false
puts countries.has_value?('Turkmenistan') # true
puts countries.value?('Great Britain') # false
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;h1&gt;
  
  
  Length/Size of the Hash
&lt;/h1&gt;

&lt;p&gt;As in Arrays, to calculate the number of elements or pairs in Hashes, we use ‘size’ and ‘length’ methods.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;fruits = {ananas: 12.7, banana: 4.9, apple: 3.6, lemon: 5.35}
puts fruits.length # 4
puts fruits.size   # 4
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h1&gt;
  
  
  Add or remove elements from a Hash
&lt;/h1&gt;

&lt;p&gt;We already did this kind of manipulation in one of the examples above to add a new element to a Hash. If we provide a key that is not yet present in the Hash, then we automatically add it as a new value into it. We just have to define its value as well. &lt;br&gt;
For instance:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;countries = {ES: 'Spain', TM: 'Turkmenistan'}
puts countries[ES] # 'Spain'
countries[:NZ] = 'New Zealand'
# {ES: 'Spain', TM: 'Turkmenistan', NZ: 'New Zealand'}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We use ‘delete’ methods to remove an existing element from the Hash. We provide the key of the element to be removed as a parameter to that method.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;countries = {ES: 'Spain', TM: 'Turkmenistan', NZ: 'New Zealand'}
countries.delete(:ES) # removes pair (ES: 'Spain') 
puts countries[:ES] # nil, because we already removed it.
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h1&gt;
  
  
  Iterate through Hash elements
&lt;/h1&gt;

&lt;p&gt;You might remember using ‘each’ method to iterate through Array elements. We can use the same methods to iterate through each pair in the Hash.&lt;br&gt;
Let’s take a look at the code below that outputs each pair on a new line.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;person = {name: 'John', height: 180, weight: 85, hair: 'black'} 

person.each do |key, value|   
   puts "John's #{key} = #{value}" 
end
# John's name = John
# John's height = 180
# John's weight = 85
# John's hair = black
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h1&gt;
  
  
  Getting all the keys or values from the Hash
&lt;/h1&gt;

&lt;p&gt;If we want to print out only the keys or only values from the Hash, then we use these methods:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;keys&lt;/strong&gt;: returns list of Hash keys&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;values&lt;/strong&gt;: returns list of Hash values
Let’s take a look at the currencies example:
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;currencies = {USA: 'USD', Turkey: 'TL', Turkmenistan: 'TMM'}
puts currencies.keys # [USA, Turkey, Turkmenistan]
puts currencies.values# ['USD', 'TL', 'TMM']
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h1&gt;
  
  
  Conclusion
&lt;/h1&gt;

&lt;p&gt;Hey, friends. We came to the end of the article about Hashes. So now we know a bit more about Ruby PL and one more complex data structure. You might have similarities between Arrays and Hashes and at the same time recognized that they are somewhat completely different.&lt;br&gt;
You get more professional and strong in a battle, as you learn and use more tools. Sometimes I compare it to programming tools. And as we learn and use more tools, we get to write more professional code. So now we have one more tool in our set.&lt;br&gt;
Let’s develop together and write clean and beautiful code. Good luck!&lt;/p&gt;

</description>
      <category>ruby</category>
      <category>codenewbie</category>
      <category>beginners</category>
      <category>tutorial</category>
    </item>
    <item>
      <title>Arrays in Ruby</title>
      <dc:creator>Merdan Durdyyev</dc:creator>
      <pubDate>Wed, 18 Nov 2020 14:59:05 +0000</pubDate>
      <link>https://dev.to/eminarium/arrays-in-ruby-1kp8</link>
      <guid>https://dev.to/eminarium/arrays-in-ruby-1kp8</guid>
      <description>&lt;h1&gt;
  
  
  Welcome
&lt;/h1&gt;

&lt;p&gt;Welcome, dear readers, coders, and enthusiasts!&lt;br&gt;
Here we are with our next article. To be honest, the more I write (blog), the more I learn, repeat, and get more professional. As you all know, “The best way to learn something is to teach it”. So I try to write more about what I learn and what I do. And it gives me an opportunity to revise what I have learned and nail it deeper in my mind.&lt;br&gt;&lt;br&gt;
So, this time we are here to learn how to use and deal with Arrays in our beloved Ruby PL.&lt;/p&gt;
&lt;h1&gt;
  
  
  Arrays
&lt;/h1&gt;

&lt;p&gt;An array is a data structure that is present in almost all programming languages. In short, Array — is a collection of items that are placed in a certain order. You can store grades of students, product prices, product names and etc. in an Arrays. This is just an example. You can store more complex objects, data structures in an Array.&lt;br&gt;&lt;br&gt;
In Ruby PL, compared to some other programming languages, we can store various typed data in an Array. I.e. you can store Numeric, String, Boolean, and some other kind of complex objects. In regular programming languages, you can only store one type of object, like only String or only Number. The main point is to treat them in the given order. All items in an Array have an index number.&lt;br&gt;&lt;br&gt;
To summarize:  &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;em&gt;Each of the elements in an array has an index number&lt;/em&gt;&lt;/li&gt;
&lt;li&gt;&lt;em&gt;Indexing the elements starts with 0 (in an Array with ’n’ elements, the first element has index ‘0’, and the last element has an index number ‘n-1’)&lt;/em&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;h1&gt;
  
  
  Creating a new Array
&lt;/h1&gt;

&lt;p&gt;To create a new Array it is enough to place it in square brackets. For instance:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;users = []  # Empty array

users = ["Arthur", "John", "Ford"] # 3 element Array

# If the Array consists of only 'String' data
users = %w(Arthur, John, Ford)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;There is another way to create an Array. It is by using the ‘Array’ keyword.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;numbers = Array.new # Empty array

# Array with 3 nil elements
other_numbers = Array.new(3) # [nil, nil, nil]

# Array with defined elements
the_numbers = Array.new([15, 20, 25]) # [15, 20, 25]

# Another Array with defined numbers
five_tens = Array.new(5, 10) 
# [10, 10, 10, 10, 10]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;As you can see, you can create empty Arrays and Arrays with predefined elements. If you want, you can create an Array with a defined number of elements that are all equal to a certain value, as shown in the last example.&lt;/p&gt;

&lt;h1&gt;
  
  
  Accessing and updating elements
&lt;/h1&gt;

&lt;p&gt;As we already have noted, all of the Array elements have an index number. What we always have to keep in mind is that the numbering starts with ‘0’.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;em&gt;in an array with ‘5’ elements, the first element has an index ‘0’, the last element has an index number ‘4’;&lt;/em&gt;&lt;/li&gt;
&lt;li&gt;&lt;em&gt;in an array with ‘10’ elements, the first element has an index ‘0’, the last element has an index number ‘9’;&lt;/em&gt;&lt;/li&gt;
&lt;li&gt;&lt;em&gt;in an array with ‘m’ elements, first element has an index ‘0’, the last element has an index number ‘m-1’;&lt;/em&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;To access array elements we use square brackets. If we want to get the element with an index number 3 in an array named ‘grades’, we access its element using ‘grades[3]’.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;seasons = ["Winter", "Sprint", "Summer", "Autumn"] # 4 elements

puts seasons[0] # The first element, with an indeks '0'
puts seasons[3] # The last element, with an index '3'

# Updating elements
seasons[0] = "Cold winter"
seasons[2] = "Hot summer"
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Besides using index numbers, Ruby has made it easier and gave us built-in “first” and “last” methods. As you already have understood, these methods get you elements with indices ‘0’ and ‘n-1’.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;cities = ["Boston", "Seoul", "Delhi", "Dubai"]

puts cities.first # "Boston"
puts cities.last # "Dubai"
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h1&gt;
  
  
  Inserting and removing elements
&lt;/h1&gt;

&lt;p&gt;We have two groups of commands to use when we want to insert and remove elements from an Array. One group methods insert and remove from the end of an Array. The other group methods insert and remove elements from the start of an Array.&lt;/p&gt;

&lt;p&gt;Here they are:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;em&gt;push (insert to the end of an Array)&lt;/em&gt;&lt;/li&gt;
&lt;li&gt;&lt;em&gt;pop (remove from the end of an Array)&lt;/em&gt;&lt;/li&gt;
&lt;li&gt;— — -&lt;/li&gt;
&lt;li&gt;&lt;em&gt;unshift (insert to the start of an Array)&lt;/em&gt;&lt;/li&gt;
&lt;li&gt;
&lt;em&gt;shift (remove from the start of an Array)&lt;/em&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;planets = ["Mars", "Earth"]

# 'push' &amp;amp; 'pop' methods
planets.push "Neptune" # ["Mars", "Earth", "Neptune"]
planets.push "Saturn" # ["Mars", "Earth", "Neptune", "Saturn"]

# Remove last element (pop)
planets.pop # ["Mars", "Earth", "Neptune"]
planets.pop # ["Mars", "Earth"]

# 'unshift' &amp;amp; 'shift' methods
planets.unshift "Neptune" # ["Neptune", "Mars", "Earth"]
planets.unshift "Saturn" # ["Saturn", "Neptune", "Mars", "Earth"]
planets.shift # ["Neptune", "Mars", "Earth"]
planets.shift # ["Mars", "Earth"]

# delete_at
colors = ["yellow", "white", "blue", "black"]
colors.delete_at 2 
# Remove element with index '2', that is 'blue'
# ["yellow", "white, "black"]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Methods that insert new elements return a new state of the Array. Methods that remove an element from an Array, just return the removed element.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;kids = ["Larry", "Brandon", "May", "John"]

first_kid = kids.shift
# first_kid =&amp;gt; "Larry"
# kids = ["Brandon", "May", "John"]

last_kid = chagalar.pop
# last_kid=&amp;gt; "John"
# kids = ["Brandon", "May"]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;An Array has another method to add elements to it. It is actually a short version of ‘push’ method. You can use it with “&amp;lt;&amp;lt;” symbols.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;colors = ["yellow", "white"]

colors&amp;lt;&amp;lt;"blue"  # ["yellow", "white", "blue"]
colors&amp;lt;&amp;lt;"black"  # ["yellow", "white", "blue", "black"]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h1&gt;
  
  
  Size of an Array
&lt;/h1&gt;

&lt;p&gt;“Ruby” PL has three methods to get the number of elements in an Array. It returns the size of the Array. Many programming languages use either of these methods. But developers of Ruby decided to include them all together. Here the are:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;length&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;size&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;count&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;empty?&lt;/strong&gt; (checks whether the Array is empty or not, returns “true” or “false”)
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;colors = ["yellow", "white", "blue", "black"]
puts colors.length # 4
puts colors.size # 4
puts colors.count # 4
puts colors.empty? # false
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h1&gt;
  
  
  Iterating through Array elements
&lt;/h1&gt;

&lt;p&gt;To iterate through Array elements we have two mostly used methods. They are “each” and “each_with_index” methods.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;each&lt;/strong&gt; (iterates through Array elements)&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;each_with_index&lt;/strong&gt; (additionally, gives access to element index)
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;colors = ["yellow", "white", "blue", "black", "red"]

colors.each { |element| puts element } 
# yellow
# white
# blue
# black
# red

colors.each_with_index { |elem, index| puts "#{index} - #{elem}" } 
# 0 - yellow
# 1 - white
# 2 - blue
# 3 - black
# 4 - red
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h1&gt;
  
  
  Extra methods
&lt;/h1&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;sort&lt;/strong&gt; (Ordering): Used to order Array elements.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;uniq&lt;/strong&gt; (non-repeating elements): returns only non-repeating, unique elements of the Array.
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;numbers = [1, 7, 2, 3, 3, 6, 5, 5]

puts numbers.sort 
# [1, 2, 3, 3, 5, 5, 6, 7]

numbers = numbers.uniq
# =&amp;gt; [1, 2, 3, 5, 6, 7]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;join&lt;/strong&gt;: joins Array elements into a single String.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;split&lt;/strong&gt;: splits “String” type element into Array elements.
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;letters = %w(a b c d)

puts letters.join # "abcd"

puts "a b c".split 
# ["a", "b", "c"]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h1&gt;
  
  
  Conclusion
&lt;/h1&gt;

&lt;p&gt;Dear readers. Here we come to the end of the article about Ruby Arrays. Array methods are not only restricted to the ones listed here, in the article. We could have counted and written many more of them. No need to load the learner with loads at once. So if you want a bit more, just go to the documentation where you can find much more about Ruby and Arrays.&lt;br&gt;
So, this is all for now. Stay hungry, stay healthy guys!&lt;/p&gt;

</description>
      <category>ruby</category>
      <category>beginners</category>
      <category>codenewbie</category>
      <category>tutorial</category>
    </item>
    <item>
      <title>Using Modules in Ruby</title>
      <dc:creator>Merdan Durdyyev</dc:creator>
      <pubDate>Tue, 10 Nov 2020 05:31:41 +0000</pubDate>
      <link>https://dev.to/eminarium/using-modules-in-ruby-32jg</link>
      <guid>https://dev.to/eminarium/using-modules-in-ruby-32jg</guid>
      <description>&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Faboubggm38vfsi5uq074.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Faboubggm38vfsi5uq074.png" alt="Alt Text" width="300" height="327"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Welcome back dear friends, dear readers, coders, and enthusiasts!&lt;/p&gt;

&lt;p&gt;Welcome to our next article where we present the preciousness of our beloved Ruby PL. This time we are going to look through the “Modules” topic in Ruby. We will also strengthen our learning by showing some examples of how to use Modules in Ruby.&lt;/p&gt;

&lt;p&gt;....................................................&lt;/p&gt;

&lt;h1&gt;
  
  
  What are the Modules?
&lt;/h1&gt;

&lt;p&gt;In short, a Module is a way to gather classes, methods, and constants under one roof. To expand the topic a bit wider, this gives us two widely used methodologies in programming.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Collection&lt;/strong&gt; — &lt;em&gt;otherwise named as a ‘namespace’, gives us an opportunity to gather classes, methods, and constants under a single roof.&lt;/em&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Mixin&lt;/strong&gt; — &lt;em&gt;a mechanism to include a ready module into another class and use its content (methods, values, constants) as its own. Thus we can expand the possibilities of that class.&lt;/em&gt;&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;After mentioning its advantages in general, let’s dive deeper into it and look at the above-mentioned items in detail. We will also provide explanations with code examples where possible.&lt;/p&gt;

&lt;p&gt;....................................................&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2F93ei1qugi25jv74nidl6.jpeg" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2F93ei1qugi25jv74nidl6.jpeg" alt="Alt Text" width="700" height="466"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h1&gt;
  
  
  1. Collection
&lt;/h1&gt;

&lt;p&gt;Imagine such a situation. You decided to collect together operators, functions, and all the related data into one place. Why should we not make it a module?&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;module Trig
   PI = 3.1416 # A Constant  

   Trig.sin(x)
      #...
   end   

   Trig.cos(x)
      #...
   end
   ...
   ...
end
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We can use the above-defined methods and constants somewhere else. Let’s use this module to perform trigonometric calculations. Here is an example:&lt;br&gt;
&lt;/p&gt;

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

# Here we call a constant named 'PI' from module 'Trig'
area = Trig.PI*radius**2 # PI*R^2 formula

# Let's calculate sine and cosine of the angle.
# Here we call 'sin' and 'cos' methods from
# 'Trig' module.

angle = 45
sine = Trig.sin(angle)
cosine = Trig.cos(angle)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If these examples are a bit easy for you, we can take a look at more complex ones. Now, let’s take a look at a module that comprises several classes in it.&lt;/p&gt;

&lt;p&gt;You decided to develop a ‘Customer’ class. It has a few properties and methods. Depending on the context, a customer can act differently. So let’s write a class that has a bit different methods and properties depending on the type of customer in a particular situation, and place them in different modules.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;module Bank
   class Customer # A bank customer.
      attr_accessor :balance, first_name, last_name

      def put_cash(sum)
         puts "#{sum} amount of cash was put."
      end      

      def withdraw(sum)
         puts "#{sum} amount was withdrawn."
      end
   end
end

module Shop
   class Customer # A shop customer
      attr_accessor :products

      def pay_for_products
         products.each do |product|
            puts "#{product} bought."
         end
      end
      ...
      ...
   end
end
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;I hope you paid attention to how we separated the issues and placed two different customers in two different modules. Both of the modules have ‘Customer’ class, but they behave differently. In other words, they have different methods and properties. If we have defined both classes in the same namespace, we would have had a conflict. And that would be a name conflict at least, not to mention the others. Ruby would not have allowed us to write the same-named class within the same namespace. So we just place them in different modules and no conflicts comes up.&lt;/p&gt;

&lt;p&gt;Now we can easily call and use the ‘Customer’ class and its methods in the following examples:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;# Example - 1
bank_customer = Bank::Customer.new
bank_customer.balance = 25
bank_customer.first_name = "John"
bank_customer.last_name = "Brown"
bank_customer.withdraw(50)

# Example - 2
shop_customer = Dukan::Customer.new
shop_customer.products = ["apple", "fig", "bread", "milk"]
shop_customer.pay_for_products
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;As you see in the examples above, by placing classes, modules, and constants into different modules we can avoid name conflicts and have them being used within the right context.&lt;/p&gt;

&lt;p&gt;Now let’s take a look at how we can use them as Mixins.&lt;/p&gt;

&lt;p&gt;...................................................&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Ff94yvaaerlzstg6z5vtl.jpeg" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Ff94yvaaerlzstg6z5vtl.jpeg" alt="Alt Text" width="700" height="466"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h1&gt;
  
  
  2. Mixins
&lt;/h1&gt;

&lt;h3&gt;
  
  
  Example A : Class relationships
&lt;/h3&gt;

&lt;p&gt;There is a bit complex relationship matter called ‘inheritance’ in the programming world. It is a case when a class is a child/subclass of another one, that we call as ‘parent’. This way a child class possesses the features and methods of a parent class. Here is an example of this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;# Define a class for animals.
class Animal
   attr_accessor :name, :category  

   def make_noise
      puts 'Making noise...'
   end   

   def move
      puts 'Moving...'
   end
end
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The next classes to be defined are somewhat part of, or subpart of an ‘Animal’ class. And they will possess the features and methods of an ‘Animal’ class. So, let’s define classes ‘Fish’ and ‘Bird’ and make them as child classes of an ‘Animal’ class. ‘Ruby’ PL makes it this way:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;# 'Fish' class
class Fish &amp;lt; Animal
   ...
end

# 'Bird' class
class Bird &amp;lt; Animal
   ...
end
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now we can call methods of an ‘Animal’ class and use its properties from within ‘Fish’ and ‘Bird’ classes.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;a_fish = Fish.new
a_fish.name = 'Ariel'
a_fish.make_noise # 'Making noise...'

a_bird = Bird.new
a_bird.name = 'Dove'
a_bird.move # 'Moving...'
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Example B : Mixins
&lt;/h3&gt;

&lt;p&gt;Now, getting inspired by the examples above, let’s write something a bit different. A film, an advertisement, a clip are all Videos. We can ‘play’ them and ‘pause’ them all.&lt;/p&gt;

&lt;p&gt;Besides that, we can download them onto our PC or mobile device. Suppose we can download only video, only audio, or both at the same time. We have ‘Download’ module for this.&lt;/p&gt;

&lt;p&gt;So let’s define ‘Video’ and ‘Download’ modules for this purpose.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;# 'Video' module
module Video
   def play
      puts 'Playing ...'
   end   

   def pause
      puts 'Paused...'
   end
end

# 'Download' module
module Download
   def download_video
      puts 'Downloading only video...'
   end

   def download_audio
      puts 'Downloading only audio...'
   end   

   def download_both
      puts 'Download audio &amp;amp; video...'
   end
end
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now let’s define other 3 classes and make methods of ‘Video’ and ‘Download’ modules available to them.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;# 'Film' class
class Film
   attr_accessor :title
   include Video
   include Download
end

# 'Advertisement' class
class Advertisement
   include Video
   include Download
end

# 'Clip' class
class Clip
   include Video
   include Download
end
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now we can use features of ‘Video’ and ‘Download’ modules after making them available within those lately defined classes. Here is an example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;# 'Film' class
silence = Film.new
silence.title= 'The Silence of Lambs'
silence.play  # 'Playing ...'
silence.download_both # Download audio and video

# 'Advertisement' class
cola_ad = Advertisement.new
cola_ad.play  # 'Playing ...'
cola_ad.download_audio # Download only audio

# 'Clip' class
drake_clip = Clip.new
drake_clip.play  # 'Playing ...'
drake_clip.download_video # Download only video
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;So we defined ‘Video’ and ‘Download’ as modules, and not classes. Lately, we made their features available to the other three classes. We used ‘include’ keyword for that.&lt;/p&gt;

&lt;h3&gt;
  
  
  Differences between Example A and Example B
&lt;/h3&gt;

&lt;p&gt;The relationship between a Parent and a Child class is very important in programming. We implement it with a mechanism called ‘inheritance’ and avoid writing excess code. Thus we support DRY (Don’t Repeat Yourself).&lt;/p&gt;

&lt;p&gt;Both ‘A’ and ‘B’ examples are doing almost the same thing but there are few differences between them. A Ruby class can not inherit from multiple classes at the same time. In other words, a class can not have multiple parent classes at the same time. So we could not inherit from ‘Video’ and ‘Download’ classes at the same time. For this reason, we implemented ‘Video’ and ‘Download’ as modules and used them as Mixins in ‘Film’, ‘Advertisement’, and ‘Clip’ classes.&lt;/p&gt;

&lt;p&gt;.................................................&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fz0824cxud8eu265h3noq.jpeg" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fz0824cxud8eu265h3noq.jpeg" alt="Alt Text" width="700" height="466"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h1&gt;
  
  
  Conclusion
&lt;/h1&gt;

&lt;p&gt;And now, we introduced our beloved Ruby PL closer and learned how to implement Modules in it. Besides that, we learned in which cases we should implement something as a Class or as a Module.&lt;/p&gt;

&lt;p&gt;One of the famous modules in Ruby PL are ‘Enumerable’ and ‘Comparable’ modules. By including these modules into your custom classes, you add comparison and numerating functionality to your own classes. This way you can compare two instances of your own class and iterate through its elements if it is a type of collection.&lt;/p&gt;

&lt;p&gt;Hope to meet you soon in the next article, dear friends.&lt;br&gt;
Stay healthy, wealthy, and wise!&lt;/p&gt;

</description>
      <category>ruby</category>
      <category>beginners</category>
      <category>codenewbie</category>
      <category>tutorial</category>
    </item>
  </channel>
</rss>
