{"id":7015,"date":"2014-05-17T00:20:44","date_gmt":"2014-05-17T00:20:44","guid":{"rendered":"https:\/\/unknownerror.org\/index.php\/2014\/05\/17\/differences-between-named-scopeslamdas-and-procs-collection-of-common-programming-errors\/"},"modified":"2014-05-17T00:20:44","modified_gmt":"2014-05-17T00:20:44","slug":"differences-between-named-scopeslamdas-and-procs-collection-of-common-programming-errors","status":"publish","type":"post","link":"https:\/\/unknownerror.org\/index.php\/2014\/05\/17\/differences-between-named-scopeslamdas-and-procs-collection-of-common-programming-errors\/","title":{"rendered":"Differences between Named Scopes,Lamdas and Procs-Collection of common programming errors"},"content":{"rendered":"<p><strong>1. Proc doesn&#8217;t check the parameters passed, but lambda does<\/strong><\/p>\n<pre><code> proc1 = Proc.new { |a, b| a + 5 }\n\n proc1.call(2)      # call with only one parameter\n =&gt; 7               # no error, unless the block uses the 2nd parameter\n\n lambda1 = lambda { |a, b| a + 5 }\n\n lambda1.call(2)\n =&gt; ArgumentError: wrong number of arguments (1 for 2)\n<\/code><\/pre>\n<p>Proc will throw error only if the block uses the second param.<\/p>\n<pre><code> proc2 = Proc.new { |a, b| a + b }\n\n proc2.call(2)      # call with only one parameter\n =&gt; TypeError: nil can't be coerced into Fixnum\n<\/code><\/pre>\n<p><strong>2. Proc returns from the calling method, while lambda doesn&#8217;t<\/strong><\/p>\n<pre><code> def meth1\n   Proc.new { return 'return from proc' }.call\n   return 'return from method'\n end\n\n puts meth1\n =&gt; return from proc\n\n def meth2\n   lambda { return 'return from lambda' }.call\n   return 'return from method'\n end\n\n puts meth2\n =&gt; return from method\n<\/code><\/pre>\n<p>If they are not called inside a method,<\/p>\n<pre><code> proc1 = Proc.new { return \"Thank you\" } \n\n proc1.call \n =&gt; LocalJumpError: unexpected return\n\n lambda1 = lambda { return \"Thank you\" } \n\n lambda1.call\n =&gt; \"Thank you\"\n<\/code><\/pre>\n<p><strong>3. Scopes\/Named scopes are a feature of <code>Rails<\/code><\/strong><\/p>\n<p>It is used to specify commonly used queries which can be referenced as method calls on the association objects or models<\/p>\n<p>eg, in user.rb:<\/p>\n<pre><code>scope :developers, -&gt; { where(:role =&gt; 'developer') }\n<\/code><\/pre>\n<p>You can use it as<\/p>\n<pre><code>@developers = User.developers\n<\/code><\/pre>\n<p>Scopes are chainable, so you can do queries like<\/p>\n<pre><code>@developers = User.developers.where(:age =&gt; 40)\n<\/code><\/pre>\n<p>The scope defined in the example, is exactly same as defining a class method, like below.<\/p>\n<pre><code>def self.developers\n  where(:role =&gt; 'developer')\nend\n<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>1. Proc doesn&#8217;t check the parameters passed, but lambda does proc1 = Proc.new { |a, b| a + 5 } proc1.call(2) # call with only one parameter =&gt; 7 # no error, unless the block uses the 2nd parameter lambda1 = lambda { |a, b| a + 5 } lambda1.call(2) =&gt; ArgumentError: wrong number of [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-7015","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/7015","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/comments?post=7015"}],"version-history":[{"count":0,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/7015\/revisions"}],"wp:attachment":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/media?parent=7015"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/categories?post=7015"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/tags?post=7015"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}