<?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: Maryna Nogtieva</title>
    <description>The latest articles on DEV Community by Maryna Nogtieva (@marynanogtieva).</description>
    <link>https://dev.to/marynanogtieva</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%2F153578%2F32480f5e-d704-419e-984c-ae37d246b54a.jpeg</url>
      <title>DEV Community: Maryna Nogtieva</title>
      <link>https://dev.to/marynanogtieva</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/marynanogtieva"/>
    <language>en</language>
    <item>
      <title>REST API - Modern Rails JSON Serializers - Which Ones To Use? </title>
      <dc:creator>Maryna Nogtieva</dc:creator>
      <pubDate>Sun, 28 Feb 2021 23:40:00 +0000</pubDate>
      <link>https://dev.to/marynanogtieva/rest-api-modern-rails-json-serializers-which-ones-to-use-2ip</link>
      <guid>https://dev.to/marynanogtieva/rest-api-modern-rails-json-serializers-which-ones-to-use-2ip</guid>
      <description>&lt;p&gt;Hello, fellow Rails developers and everyone who wants to participate in the discussion. I'm working on a small side project where I want to create a rails API.&lt;/p&gt;

&lt;p&gt;I use the TDD approach (at least I try! 😅) and code in small iterations. Now after I created the whole model, validations and basic routes, the moment of truth has come...&lt;/p&gt;

&lt;p&gt;I need to choose how my API will look like and what gem is the best to use for Serialization. &lt;/p&gt;

&lt;p&gt;After googling and reading some articles, I found that there are a lot of blogs related to &lt;a href="https://github.com/Netflix/fast_jsonapi"&gt;Fast JSON API&lt;/a&gt; which is now deprecated and kind of replaced by &lt;a href="https://github.com/jsonapi-serializer/jsonapi-serializer"&gt;JSON:API Serializer&lt;/a&gt;. These gems are following &lt;a href="https://jsonapi.org/"&gt;JSON:API specification&lt;/a&gt; and force our API response to meet this specification standard. &lt;/p&gt;

&lt;p&gt;On the other hand, we have &lt;a href="https://github.com/rails-api/active_model_serializers"&gt;Active Model Serializers&lt;/a&gt;. This gem is also deprecated but has &lt;a href="https://github.com/rails-api/active_model_serializers/tree/0-10-stable"&gt;another stable version 0.10&lt;/a&gt;.&lt;br&gt;
I also found &lt;a href="https://github.com/procore/blueprinter"&gt;Blueprinter gem&lt;/a&gt;. I've never used it before and don't know its limitations.&lt;/p&gt;

&lt;p&gt;I'm curious, what are the best practices in building robust REST APIs that are easy to use? Is it better to follow &lt;code&gt;JSON:API&lt;/code&gt; specification or create something of your own?&lt;/p&gt;

&lt;p&gt;For instance, looks like &lt;a href="https://docs.github.com/en/rest/reference/orgs"&gt;GitHub&lt;/a&gt; or &lt;a href="https://shopify.dev/docs/admin-api/rest/reference/inventory/inventoryitem"&gt;Shopify&lt;/a&gt; responses don't follow &lt;code&gt;JSON:API&lt;/code&gt; specification.&lt;/p&gt;

&lt;p&gt;And if one chooses to create responses not following &lt;code&gt;JSON:API&lt;/code&gt; standard, which gem would you prefer to use 🙃?&lt;/p&gt;

</description>
      <category>discuss</category>
      <category>serialization</category>
      <category>rails</category>
      <category>help</category>
    </item>
    <item>
      <title>Procs and Lambdas</title>
      <dc:creator>Maryna Nogtieva</dc:creator>
      <pubDate>Mon, 27 Apr 2020 17:40:02 +0000</pubDate>
      <link>https://dev.to/marynanogtieva/procs-and-lambdas-3907</link>
      <guid>https://dev.to/marynanogtieva/procs-and-lambdas-3907</guid>
      <description>&lt;p&gt;Today I was refreshing my knowledge and learning something new about &lt;strong&gt;proc&lt;/strong&gt; and &lt;strong&gt;lambda&lt;/strong&gt; in Ruby and wanted to share some info about it that, hopefully, might be useful for somebody else.&lt;/p&gt;

&lt;p&gt;In the &lt;a href="https://dev.to/marynanogtieva/ruby-blocks-3cdf"&gt;previous post&lt;/a&gt; we discussed that almost everything is an object in Ruby except blocks.&lt;br&gt;
Block in Ruby is a very useful feature that helps to remove some repetition in the code and perform various operations on data structures dynamically.&lt;/p&gt;

&lt;p&gt;However, sometimes(not too often) we might need to have a way to store a block of code into a variable, which is not possible with regular Blocks in Ruby:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight ruby"&gt;&lt;code&gt;&lt;span class="c1"&gt;# This will not work and an exception will be thrown&lt;/span&gt;
&lt;span class="n"&gt;a&lt;/span&gt; &lt;span class="o"&gt;=&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="p"&gt;}&lt;/span&gt;  &lt;span class="c1"&gt;# Ruby expects a hash here between curly braces&lt;/span&gt;

&lt;span class="c1"&gt;# syntax error, unexpected keyword_do_block&lt;/span&gt;
&lt;span class="n"&gt;b&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;do&lt;/span&gt;
  &lt;span class="nb"&gt;puts&lt;/span&gt; &lt;span class="s2"&gt;"Hello"&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;h2&gt;
  
  
  Enter lambdas and procs ✨
&lt;/h2&gt;

&lt;p&gt;Lambda and proc are wrappers around a block that allow us to store a block of code into a variable and use it later. This variable will be an object - an instance of the &lt;a href="https://ruby-doc.org/core-2.7.1/Proc.html"&gt;Proc&lt;/a&gt; class.&lt;/p&gt;

&lt;p&gt;After a proc is created it can be later passed as an argument to a method.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight ruby"&gt;&lt;code&gt;&lt;span class="n"&gt;names&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s1"&gt;'Anna'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;'Alex'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;'Hellie'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;'Martin'&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
&lt;span class="n"&gt;proc_name&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="no"&gt;Proc&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="o"&gt;|&lt;/span&gt;&lt;span class="nb"&gt;name&lt;/span&gt;&lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="nb"&gt;name&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;start_with?&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'A'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="n"&gt;selected_names&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;names&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;&amp;amp;&lt;/span&gt;&lt;span class="n"&gt;proc_name&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;In the example above we can see only two names printed out&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;["Anna", "Alex"]
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;h3&gt;
  
  
  Creating lambda and proc
&lt;/h3&gt;

&lt;p&gt;Proc class has two flavors: &lt;code&gt;proc&lt;/code&gt;s and &lt;code&gt;lambda&lt;/code&gt;s. We will discuss their differences in a moment but first let's review how we can create them. There are few ways both to create and call &lt;code&gt;proc&lt;/code&gt;s and &lt;code&gt;lambda&lt;/code&gt;s:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight ruby"&gt;&lt;code&gt;&lt;span class="n"&gt;proc1&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="no"&gt;Proc&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="nb"&gt;puts&lt;/span&gt; &lt;span class="s2"&gt;"I'm proc1"&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="c1"&gt;##&amp;lt;Proc:0x00007fce97948b40@(irb):117&amp;gt;&lt;/span&gt;
&lt;span class="n"&gt;proc2&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;proc&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;"I'm proc2"&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="c1"&gt;##&amp;lt;Proc:0x00007fce9793a180@(irb):118&amp;gt;&lt;/span&gt;

&lt;span class="n"&gt;lambda1&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;lambda&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;"I'm lambda1"&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="c1"&gt;##&amp;lt;Proc:0x00007fce9792a870@(irb):120 (lambda)&amp;gt;&lt;/span&gt;
&lt;span class="n"&gt;lambda2&lt;/span&gt; &lt;span class="o"&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="nb"&gt;puts&lt;/span&gt; &lt;span class="s2"&gt;"I'm lambda2"&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="c1"&gt;##&amp;lt;Proc:0x00007fce9791bbb8@(irb):121 (lambda)&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Stabby lambda &lt;code&gt;-&amp;gt;&lt;/code&gt; is a new syntax for creating lambdas and is used more often than &lt;code&gt;lambda&lt;/code&gt; keyword.&lt;/p&gt;

&lt;h3&gt;
  
  
  Calling lambda and proc
&lt;/h3&gt;

&lt;p&gt;There are many ways to call both lambda and proc. I personally prefer &lt;code&gt;.call()&lt;/code&gt; and &lt;code&gt;.()&lt;/code&gt; syntax.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;
proc1.call()
lambda1.call()
I'm proc1
I'm lambda1


proc1.()
lambda1.()
I'm proc1
I'm lambda1

proc1.[]
lambda1.[]
I'm proc1
I'm lambda1

proc1.===
lambda1.===
I'm proc1
I'm lambda1
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;h3&gt;
  
  
  Difference between lambda and proc
&lt;/h3&gt;

&lt;p&gt;Both lambda and proc are similar on the surface but actually behave quite differently. They are similar in a way that they are instances of the same &lt;code&gt;Proc&lt;/code&gt; class.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight ruby"&gt;&lt;code&gt;&lt;span class="n"&gt;irb&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;main&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;&lt;span class="mi"&gt;126&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;proc1&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;class&lt;/span&gt;
&lt;span class="no"&gt;Proc&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="no"&gt;Object&lt;/span&gt;

&lt;span class="n"&gt;irb&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;main&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;&lt;span class="mi"&gt;127&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;lambda1&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;class&lt;/span&gt;
&lt;span class="no"&gt;Proc&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="no"&gt;Object&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;But one should not be confused by that fact. We should take a closer look at &lt;code&gt;lambda&lt;/code&gt; and &lt;code&gt;proc&lt;/code&gt;:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;&lt;code&gt;proc&lt;/code&gt; is not strict with the number of arguments passed&lt;/strong&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight ruby"&gt;&lt;code&gt;&lt;span class="n"&gt;number_of_students&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="no"&gt;Proc&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;number&lt;/span&gt;&lt;span class="o"&gt;|&lt;/span&gt;
 &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;number&lt;/span&gt;
  &lt;span class="nb"&gt;puts&lt;/span&gt; &lt;span class="n"&gt;number&lt;/span&gt;
 &lt;span class="k"&gt;else&lt;/span&gt;
  &lt;span class="nb"&gt;puts&lt;/span&gt; &lt;span class="s2"&gt;"no students"&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;# prints 20&lt;/span&gt;
&lt;span class="n"&gt;number_of_students&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="mi"&gt;20&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="c1"&gt;# prints "no students"&lt;/span&gt;
&lt;span class="n"&gt;number_of_students&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;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;&lt;code&gt;lambda&lt;/code&gt; can scold you in case you pass the wrong number of arguments.&lt;/strong&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight ruby"&gt;&lt;code&gt;&lt;span class="n"&gt;number_of_students2&lt;/span&gt; &lt;span class="o"&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;number&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;do&lt;/span&gt;
 &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;number&lt;/span&gt;
  &lt;span class="nb"&gt;puts&lt;/span&gt; &lt;span class="n"&gt;number&lt;/span&gt;
 &lt;span class="k"&gt;else&lt;/span&gt;
  &lt;span class="nb"&gt;puts&lt;/span&gt; &lt;span class="s2"&gt;"no students"&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;# prints 10&lt;/span&gt;
&lt;span class="n"&gt;number_of_students2&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="mi"&gt;10&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="c1"&gt;# throws an exception: ArgumentError (wrong number of arguments (given 0, expected 1))&lt;/span&gt;
&lt;span class="n"&gt;number_of_students2&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;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Keyword &lt;code&gt;return&lt;/code&gt; has a different behavior:&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;“return” in lambda behaves like “return” in a method&lt;/strong&gt; - you call a method from another method, get a return value, and carry on with the code below it.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;"return" in proc behaves the same way as in a block&lt;/strong&gt;. If a &lt;code&gt;proc&lt;/code&gt; with &lt;code&gt;return&lt;/code&gt; keyword is called inside a method, then code specified below &lt;code&gt;proc.call()&lt;/code&gt; will not be executed. The method will return a value that is being returned by the &lt;code&gt;proc&lt;/code&gt;.
Note,  if you declare &lt;code&gt;proc&lt;/code&gt; with &lt;code&gt;return&lt;/code&gt; in the top context and then pass it to a method as an argument, you will get an exception &lt;code&gt;LocalJumpError: unexpected return&lt;/code&gt; because we are not allowed to return from top-level context.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Let's look at some examples.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;When a lambda is declared outside of the method(passed to the method as an argument) and when another lambda declared inside a method, we will see all four lines printed out.
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="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;my_lambda&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;lambda_obj&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
  &lt;span class="n"&gt;internal_lambda&lt;/span&gt; &lt;span class="o"&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="k"&gt;return&lt;/span&gt; &lt;span class="s2"&gt;"in internal lambda"&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;"before lambda"&lt;/span&gt;
  &lt;span class="nb"&gt;puts&lt;/span&gt; &lt;span class="n"&gt;internal_lambda&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;call&lt;/span&gt;
  &lt;span class="nb"&gt;puts&lt;/span&gt; &lt;span class="n"&gt;lambda_obj&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;call&lt;/span&gt;
  &lt;span class="nb"&gt;puts&lt;/span&gt; &lt;span class="s2"&gt;"after lambda"&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;

&lt;span class="n"&gt;lambda_obj&lt;/span&gt; &lt;span class="o"&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="k"&gt;return&lt;/span&gt; &lt;span class="s2"&gt;"in outside lambda"&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="n"&gt;my_lambda&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;lambda_obj&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



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

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;before lambda
in internal lambda
in outside lambda
after lambda
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;When a proc is declared outside the method in a top-level scope an error will be thrown.
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight ruby"&gt;&lt;code&gt;&lt;span class="n"&gt;outside_proc&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="no"&gt;Proc&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="k"&gt;return&lt;/span&gt; &lt;span class="s2"&gt;"From outside proc"&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;my_proc&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;proc_obj&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;"before proc"&lt;/span&gt;
  &lt;span class="nb"&gt;puts&lt;/span&gt; &lt;span class="n"&gt;proc_obj&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="nb"&gt;puts&lt;/span&gt; &lt;span class="s2"&gt;"after_proc"&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;

&lt;span class="n"&gt;my_proc&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;outside_proc&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



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

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;before proc
LocalJumpError: unexpected return
    from (irb):98:in `block in irb_binding'
    from (irb):102:in `my_proc'
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;When proc is declared inside the method, it will be binded to the method scope, and lines that come after calling proc will not be printed out.
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="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;my_proc&lt;/span&gt;
  &lt;span class="n"&gt;proc_obj&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="no"&gt;Proc&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="k"&gt;return&lt;/span&gt; &lt;span class="s2"&gt;"From inside proc"&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;"before proc"&lt;/span&gt;
  &lt;span class="nb"&gt;puts&lt;/span&gt; &lt;span class="n"&gt;proc_obj&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="c1"&gt;# this will not be printed out&lt;/span&gt;
  &lt;span class="nb"&gt;puts&lt;/span&gt; &lt;span class="s2"&gt;"after_proc"&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;

&lt;span class="n"&gt;my_proc&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



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

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;before proc
"From inside proc"
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;h3&gt;
  
  
  Ruby has functional programming features?
&lt;/h3&gt;

&lt;p&gt;Ruby is one of the purest object-oriented languages but it is spiced up with functional programming flavors. I have some experience with JavaScript and my examples will reference this language for comparison purposes.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Closures"&gt;Closures&lt;/a&gt;.
In JavaScript closure allows the inner function to have access to the lexical scope of the outer function. 
Procs in Ruby are also bonded to the variable and have access to the context/scope where that variable was declared. Let's see an example:
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight ruby"&gt;&lt;code&gt;&lt;span class="n"&gt;salary&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;1000&lt;/span&gt;
&lt;span class="n"&gt;bonus&lt;/span&gt;  &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;500&lt;/span&gt;

&lt;span class="n"&gt;salary_and_bonus&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="no"&gt;Proc&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="nb"&gt;puts&lt;/span&gt; &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="si"&gt;#{&lt;/span&gt;&lt;span class="n"&gt;salary&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;bonus&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;print_salary_with_bonus&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;my_proc&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
  &lt;span class="n"&gt;bonus&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;700&lt;/span&gt;
  &lt;span class="n"&gt;my_proc&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;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Will the result of calling &lt;code&gt;#print_salary_with_bonus&lt;/code&gt; method be a &lt;code&gt;1500&lt;/code&gt; or &lt;code&gt;1700&lt;/code&gt; ?&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;irb(main):116:0&amp;gt; print_salary_with_bonus(salary_and_bonus)
1500
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Because we declared our &lt;code&gt;salary_and_bonus&lt;/code&gt; proc in the outer scope it had access (remembered) to everything else declared in the same context. Therefore, our output printed &lt;code&gt;1500&lt;/code&gt;.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Composition. &lt;a href="https://ruby-doc.org/core-2.7.1/Proc.html#method-i-3E-3E"&gt;&amp;gt;&amp;gt; operator in Ruby&lt;/a&gt;. 
A composition is an assembly of multiple functions combined together into one function to perform multiple operations on one piece of data.
Basically, composition allows us to combine two or more functions into one function. &lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;code&gt;Proc#&amp;gt;&amp;gt;&lt;/code&gt; was introduced in Ruby &lt;code&gt;2.6&lt;/code&gt; and it allows us to perform some fun tasks. Let's imagine that we went to an online store and decided to purchase one item from there.&lt;br&gt;
Here is how we would calculate its final price by using a composition:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight ruby"&gt;&lt;code&gt;&lt;span class="no"&gt;WHOLESALE_PRICE&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;400&lt;/span&gt;

&lt;span class="n"&gt;base_price&lt;/span&gt;     &lt;span class="o"&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;price&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="no"&gt;WHOLESALE_PRICE&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;price&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="n"&gt;handling_price&lt;/span&gt; &lt;span class="o"&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;price&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;price&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;price&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="mf"&gt;0.1&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="n"&gt;total_tax&lt;/span&gt;      &lt;span class="o"&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;price&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;price&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;price&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="mf"&gt;0.13&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="n"&gt;calculate_final_price&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;base_price&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;handling_price&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;total_tax&lt;/span&gt;
&lt;span class="n"&gt;final_price&lt;/span&gt;           &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;calculate_final_price&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="mi"&gt;100&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="c1"&gt;# final price =&amp;gt; 621.5&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Interestingly,  &lt;code&gt;Proc#&amp;gt;&amp;gt;&lt;/code&gt; in ruby switches the order of a function call comparing to composition in Haskel or Math.&lt;br&gt;
Usually, when we use a composition the order of data processing goes from right to left &lt;code&gt;f(g(x))&lt;/code&gt; - first we process &lt;code&gt;x&lt;/code&gt; and then &lt;code&gt;g&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;With &lt;code&gt;Proc#&amp;gt;&amp;gt;&lt;/code&gt; we process data from left to right. However, &lt;code&gt;Proc#&amp;lt;&amp;lt;&lt;/code&gt; allows us to process data the same way as it's done in functional programming 🙂 . I suggest checking this &lt;a href="https://blog.stanko.io/function-composition-ruby-8f91aea21e5f"&gt;blog&lt;/a&gt; for more details on that.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://thoughtbot.com/blog/proc-composition-in-ruby"&gt;Here&lt;/a&gt; is a nice article to learn more about composition in Ruby in detail.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Function currying.
If you learned any functional programming you probably encountered &lt;code&gt;currying&lt;/code&gt; paradigm before. &lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;With the help of currying, you can take a function with 2 arguments, call this function with one argument. This is called a partial application. Then call the partial application with the final argument to complete the function and evaluate the result.&lt;/p&gt;

&lt;p&gt;If you are wondering how currying in ruby can be useful "Applying Higher-order Functions: Partial Application and Currying" part of this &lt;a href="https://www.sitepoint.com/functional-programming-techniques-with-ruby-part-ii/"&gt;article&lt;/a&gt; would be a very good read to get more knowledge on that.&lt;/p&gt;

&lt;p&gt;Let's try to take the previous example of an item's price calculation and try to use &lt;a href="https://ruby-doc.org/core-2.6.1/Proc.html#method-i-curry"&gt;Proc#curry&lt;/a&gt; for that purpose:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight ruby"&gt;&lt;code&gt;&lt;span class="no"&gt;WHOLESALE_PRICE&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;400&lt;/span&gt;

&lt;span class="n"&gt;calculate_price&lt;/span&gt; &lt;span class="o"&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="nb"&gt;method&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;price&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;additional_cost&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;do&lt;/span&gt;
  &lt;span class="n"&gt;price&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;send&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;method&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;additional_cost&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;add&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;calculate_price&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;curry&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="o"&gt;+&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="n"&gt;base_price&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;add&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="no"&gt;WHOLESALE_PRICE&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;100&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="c1"&gt;# =&amp;gt; 500&lt;/span&gt;
&lt;span class="n"&gt;handling_price&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;add&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;base_price&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;base_price&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="mf"&gt;0.1&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="c1"&gt;# =&amp;gt; 550.0&lt;/span&gt;

&lt;span class="n"&gt;tax&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;handling_price&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="mf"&gt;0.13&lt;/span&gt;
&lt;span class="n"&gt;final_price_with_tax&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;add&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;handling_price&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;tax&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="c1"&gt;# =&amp;gt; 621.5&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Thanks for reading, I hope you found this information useful 🙂&lt;/p&gt;

</description>
      <category>ruby</category>
      <category>beginners</category>
      <category>programming</category>
    </item>
    <item>
      <title>Ruby Blocks</title>
      <dc:creator>Maryna Nogtieva</dc:creator>
      <pubDate>Mon, 13 Apr 2020 21:16:38 +0000</pubDate>
      <link>https://dev.to/marynanogtieva/ruby-blocks-3cdf</link>
      <guid>https://dev.to/marynanogtieva/ruby-blocks-3cdf</guid>
      <description>&lt;h2&gt;
  
  
  Ruby Block - what is that, and what does it do?
&lt;/h2&gt;

&lt;p&gt;It has long been said that "Everying is an object in Ruby". Well, it is almost true. &lt;/p&gt;

&lt;p&gt;Despite such classes as &lt;a href="https://ruby-doc.org/core-2.7.1/Class.html"&gt;Class&lt;/a&gt;, &lt;a href="https://ruby-doc.org/core-2.7.1/Module.html"&gt;Module&lt;/a&gt;, as well as many others eventually being objects, there's an exception to the rule.&lt;br&gt;
As you probably guessed, &lt;strong&gt;block&lt;/strong&gt; is not an object. We cannot assign it to a variable or call a method &lt;a href="https://ruby-doc.org/core-2.7.1/Object.html#method-i-class"&gt;class&lt;/a&gt; on it.&lt;/p&gt;

&lt;p&gt;It is a chunk of code that we want to execute dynamically by putting it between &lt;code&gt;do...end&lt;/code&gt; or between curly braces &lt;code&gt;{...}&lt;/code&gt;. The benefit of blocks is that we don't need to store some statements into a method. Blocks serve as &lt;strong&gt;part of a method signature&lt;/strong&gt; (method signature includes method name, list of arguments and a block) and must start on the same line after method arguments.&lt;/p&gt;

&lt;p&gt;We often use blocks when we read or write into files, or when we want to perform certain operations while iterating via a collection, such as an Array or a Hash.&lt;/p&gt;

&lt;p&gt;As an example let's take a look into &lt;a href="https://ruby-doc.org/core-2.7.1/Integer.html#method-i-times"&gt;times&lt;/a&gt; method of the Integer class.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight ruby"&gt;&lt;code&gt;&lt;span class="c1"&gt;# Block written in one line&lt;/span&gt;
&lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;times&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;"Some code within a block"&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="c1"&gt;# Block written in multiple lines&lt;/span&gt;
&lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;times&lt;/span&gt; &lt;span class="k"&gt;do&lt;/span&gt;
  &lt;span class="nb"&gt;puts&lt;/span&gt; &lt;span class="s2"&gt;"Some code within a block"&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;





&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;# Output in both cases
Some code within a block
Some code within a block
Some code within a block
Some code within a block
Some code within a block
=&amp;gt; 5
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;As per the implementation of the &lt;code&gt;times&lt;/code&gt; method, an output &lt;code&gt;Some code within a block&lt;/code&gt; will be printed out five times and integer &lt;code&gt;5&lt;/code&gt; will be returned after that.&lt;/p&gt;

&lt;p&gt;A block has to start on the same line as the method name and its parameters if any.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight ruby"&gt;&lt;code&gt;&lt;span class="c1"&gt;# This is wrong&lt;/span&gt;
&lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;times&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;"Some code within a block"&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;h2&gt;
  
  
  Difference between &lt;code&gt;do..end&lt;/code&gt; and curly braces &lt;code&gt;{}&lt;/code&gt;.
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;According to the &lt;a href="https://github.com/github/rubocop-github/blob/master/STYLEGUIDE.md"&gt;Ruby Style Guide&lt;/a&gt; it is recommended to use &lt;code&gt;do...end&lt;/code&gt; for multi-line blocks and &lt;code&gt;{...}&lt;/code&gt; for single-line blocks.&lt;/li&gt;
&lt;li&gt;Curly braces &lt;code&gt;{...}&lt;/code&gt; syntax has higher precedence than &lt;code&gt;do...end&lt;/code&gt; syntax. We can observe when a block is passed to a method without using parentheses &lt;code&gt;()&lt;/code&gt;:
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight ruby"&gt;&lt;code&gt;&lt;span class="c1"&gt;# Using {...}&lt;/span&gt;
&lt;span class="nb"&gt;puts&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;3&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;number&lt;/span&gt;&lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="n"&gt;number&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;even?&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;=&amp;gt;&lt;/span&gt; &lt;span class="kp"&gt;nil&lt;/span&gt; &lt;span class="c1"&gt;# method "puts" returns nil&lt;/span&gt;

&lt;span class="c1"&gt;# Using do...end&lt;/span&gt;
&lt;span class="nb"&gt;puts&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;].&lt;/span&gt;&lt;span class="nf"&gt;select&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;number&lt;/span&gt;&lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="n"&gt;number&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;even?&lt;/span&gt; &lt;span class="k"&gt;end&lt;/span&gt;
&lt;span class="c1"&gt;#&amp;lt;Enumerator:0x00007fefbebca498&amp;gt;&lt;/span&gt;
&lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="kp"&gt;nil&lt;/span&gt; &lt;span class="c1"&gt;# method "puts" returns nil&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;In the first case, we see &lt;code&gt;2&lt;/code&gt; is printed back because the method &lt;code&gt;puts&lt;/code&gt; accepts everything till the end of the block as one parameter.&lt;br&gt;
In the second case, &lt;a href="https://ruby-doc.org/core-2.7.1/Enumerator.html"&gt;enumerator&lt;/a&gt; is returned instead because the method &lt;code&gt;puts&lt;/code&gt; does not see &lt;code&gt;do..end&lt;/code&gt; as a part of a parameter.&lt;/p&gt;
&lt;h2&gt;
  
  
  Block Parameters
&lt;/h2&gt;

&lt;p&gt;A block can have parameters that are stored between vertical lines called pipes &lt;code&gt;||&lt;/code&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight ruby"&gt;&lt;code&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;times&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;number&lt;/span&gt;&lt;span class="o"&gt;|&lt;/span&gt;
  &lt;span class="nb"&gt;puts&lt;/span&gt; &lt;span class="n"&gt;number&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;





&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;# Output printed
0
1
=&amp;gt; 2 # returned value of the "times" method
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;h2&gt;
  
  
  Block Variable Scope
&lt;/h2&gt;

&lt;p&gt;Block variables (parameters between the pipes &lt;code&gt;||&lt;/code&gt;) can behave differently from variables that are coming from the method scope. Let's review some examples:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;A variable is declared in the method and the same variable is used inside a block. We can see that block has access to the &lt;code&gt;x&lt;/code&gt;:
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="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;block_variable_example&lt;/span&gt;
  &lt;span class="n"&gt;x&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;
  &lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;times&lt;/span&gt; &lt;span class="k"&gt;do&lt;/span&gt;
    &lt;span class="nb"&gt;puts&lt;/span&gt; &lt;span class="s2"&gt;"x inside the block: &lt;/span&gt;&lt;span class="si"&gt;#{&lt;/span&gt;&lt;span class="n"&gt;x&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="nb"&gt;puts&lt;/span&gt; &lt;span class="s2"&gt;"x outside the block: &lt;/span&gt;&lt;span class="si"&gt;#{&lt;/span&gt;&lt;span class="n"&gt;x&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;/code&gt;&lt;/pre&gt;&lt;/div&gt;





&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;# Call method
block_variable_example

# Output
x inside the block: 1
x inside the block: 1
x inside the block: 1
x outside the block: 1

=&amp;gt; nil
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;A variable is declared in the method and the same variable is being re-assigned inside the block. Same as in the first example, variable inside a block is the same as the one outside:
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="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;block_variable_example&lt;/span&gt;
  &lt;span class="n"&gt;x&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;
  &lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;times&lt;/span&gt; &lt;span class="k"&gt;do&lt;/span&gt;
    &lt;span class="n"&gt;x&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;
    &lt;span class="nb"&gt;puts&lt;/span&gt; &lt;span class="s2"&gt;"x inside the block: &lt;/span&gt;&lt;span class="si"&gt;#{&lt;/span&gt;&lt;span class="n"&gt;x&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="nb"&gt;puts&lt;/span&gt; &lt;span class="s2"&gt;"x outside the block: &lt;/span&gt;&lt;span class="si"&gt;#{&lt;/span&gt;&lt;span class="n"&gt;x&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;/code&gt;&lt;/pre&gt;&lt;/div&gt;





&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;# Call method
block_variable_example

# Output
x inside the block: 2
x inside the block: 2
x inside the block: 2
x outside the block: 2

=&amp;gt; nil
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;A variable &lt;code&gt;x&lt;/code&gt; is declared in the method and the block parameter has the same name &lt;code&gt;x&lt;/code&gt;. It appears that variable inside the block is different from the one that was declared in the method:
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="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;block_variable_example&lt;/span&gt;
  &lt;span class="n"&gt;x&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;
  &lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;times&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;x&lt;/span&gt;&lt;span class="o"&gt;|&lt;/span&gt;
    &lt;span class="nb"&gt;puts&lt;/span&gt; &lt;span class="s2"&gt;"x inside the block: &lt;/span&gt;&lt;span class="si"&gt;#{&lt;/span&gt;&lt;span class="n"&gt;x&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="nb"&gt;puts&lt;/span&gt; &lt;span class="s2"&gt;"x outside the block: &lt;/span&gt;&lt;span class="si"&gt;#{&lt;/span&gt;&lt;span class="n"&gt;x&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="c1"&gt;# Call method&lt;/span&gt;
&lt;span class="n"&gt;block_variable_example&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;





&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;# Output
x inside the block: 0
x inside the block: 1
x inside the block: 2
x outside the block: 1

=&amp;gt; nil
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;h2&gt;
  
  
  What is &lt;code&gt;yield&lt;/code&gt; keyword?
&lt;/h2&gt;

&lt;p&gt;If you want to execute a block inside a method &lt;code&gt;yield&lt;/code&gt; is your friend. In many countries, there's a traffic sign &lt;code&gt;yield&lt;/code&gt; which means you need to give right of way to another car. &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Similar logic applies here. During a method execution when Ruby parser sees &lt;code&gt;yield&lt;/code&gt; it gives the right of way to the code within a block and executes it. After that, it comes back to the method scope and executes the remaining code. &lt;/li&gt;
&lt;li&gt;In case there are situations when a block is going to be optional when calling your method it is better to use the &lt;code&gt;Kernel#block_given?&lt;/code&gt; method so that no exception is thrown.
Here are some examples of using &lt;code&gt;yield&lt;/code&gt; keyword:
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="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;my_yield_method&lt;/span&gt;
  &lt;span class="nb"&gt;puts&lt;/span&gt; &lt;span class="s2"&gt;"Code before yield"&lt;/span&gt;

  &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="nb"&gt;block_given?&lt;/span&gt;
    &lt;span class="k"&gt;yield&lt;/span&gt;
  &lt;span class="k"&gt;else&lt;/span&gt;
    &lt;span class="nb"&gt;puts&lt;/span&gt; &lt;span class="s2"&gt;"No block was given"&lt;/span&gt;
  &lt;span class="k"&gt;end&lt;/span&gt;

  &lt;span class="nb"&gt;puts&lt;/span&gt; &lt;span class="s2"&gt;"Code after yield"&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;

&lt;span class="c1"&gt;# =================== Call method with a block&lt;/span&gt;
&lt;span class="n"&gt;my_yield_method&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;"Code inside a block"&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;





&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;# Output
Code before yield
Code inside a block
Code after yield

=&amp;gt;nil
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;





&lt;div class="highlight"&gt;&lt;pre class="highlight ruby"&gt;&lt;code&gt;&lt;span class="c1"&gt;# =================== Call method without a block&lt;/span&gt;
&lt;span class="n"&gt;my_yield_method&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;





&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;# Output
Code before yield
No block was given
Code after yield

=&amp;gt; nil
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;yield&lt;/code&gt; keyword can have arguments that are going to be passed to a block
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="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;my_yield_method_with_params&lt;/span&gt;
  &lt;span class="n"&gt;str&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s2"&gt;"Awesome parameter"&lt;/span&gt;

  &lt;span class="nb"&gt;puts&lt;/span&gt; &lt;span class="s2"&gt;"Code before yield"&lt;/span&gt;
  &lt;span class="k"&gt;yield&lt;/span&gt; &lt;span class="n"&gt;str&lt;/span&gt; &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="nb"&gt;block_given?&lt;/span&gt;
  &lt;span class="nb"&gt;puts&lt;/span&gt; &lt;span class="s2"&gt;"Code after yield"&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;

&lt;span class="c1"&gt;# =================== Call method with a block&lt;/span&gt;
&lt;span class="n"&gt;my_yield_method_with_params&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;param&lt;/span&gt;&lt;span class="o"&gt;|&lt;/span&gt;
  &lt;span class="nb"&gt;puts&lt;/span&gt; &lt;span class="s2"&gt;"This is what we got: &lt;/span&gt;&lt;span class="si"&gt;#{&lt;/span&gt;&lt;span class="n"&gt;param&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;/code&gt;&lt;/pre&gt;&lt;/div&gt;





&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;# Output:
Code before yield
This is what we got: Awesome parameter
Code after yield

=&amp;gt; nil
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;I hope you found this information helpful 🙂&lt;/p&gt;

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