<!DOCTYPE html>
<html>
	<head>
	
	<link href="//gmpg.org/xfn/11" rel="profile">
	<meta http-equiv="content-type" content="text/html; charset=utf-8">
	<link rel="canonical" href="http://jtimmer89.github.io//blog/technical-blog-week6" />
	<meta name="description" content="What are these things in Ruby and how do you use them?" />
	<meta property="og:description" content="What are these things in Ruby and how do you use them?" />
	<meta itemprop="image" content="http://jtimmer89.github.io//media/technical-blogs/AutumnLake_14.jpg" />
	<meta property="og:image" content="http://jtimmer89.github.io//media/technical-blogs/AutumnLake_14.jpg" />
	<meta property="og:title" content="Technical Blog: Blocks, Procs, and Lambdas" />
	<meta property="og:type" content="article" />
	<meta property="og:url" content="http://jtimmer89.github.io//blog/technical-blog-week6" />
	<meta property="og:site_name" content="Joe Timmer" />
	<title>Technical Blog: Blocks, Procs, and Lambdas &middot; Joe Timmer</title>
	<meta name="mobile-web-app-capable" content="yes">
	<meta name="HandheldFriendly" content="True" />
	<meta name="MobileOptimized" content="320" />
	<meta name="viewport" content="width=device-width, initial-scale=1.0" />
	<link rel="stylesheet" type="text/css" media="only screen and (min-width: 900px)" href="/assets/css/desktop.css" />
	<link rel="stylesheet" type="text/css" media="only screen and (max-width: 899px)" href="/assets/css/mobile.css" />
	<link href="/assets/css/genericons.css" type="text/css" rel="stylesheet" />
	<link href="/assets/css/syntax.css" type="text/css" rel="stylesheet" />
	<link rel="stylesheet" href="//fonts.googleapis.com/css?family=Fenix:400,400italic">
	<link rel="apple-touch-icon" href="http://jtimmer89.github.io//assets/images/icon.png">
	<link rel="shortcut icon" href="http://jtimmer89.github.io//assets/images/favicon.ico">
	<link rel="alternate" type="application/rss+xml" title="RSS" href="http://jtimmer89.github.io//atom.xml">
	<script type="text/javascript" src="/assets/js/jquery-2.1.0.min.js"></script>
	<script type="text/javascript" src="/assets/js/jquery.stellar.min.js"></script>
	
</head>

	<body>
		<header class="clean" style="background-image: url(/media/technical-blogs/AutumnLake_14.jpg); height: 100%;" data-stellar-background-ratio="0.5" data-stellar-horizontal-offset="50">
	
	<label class="menu" for="_1">
	<span class="genericon genericon-menu"></span>
</label>
<input id="_1" type="checkbox">
<div class="menu-content">
	<p class="menu-title">Joe Timmer<p>
	<div class="menu" style="background-color: #1a1a1a; z-index: -1;"></div>
	
		<a href="/">Home</a>
	
		<a href="/about">About</a>
	
	<hr>
	
		<a href="https://twitter.com/ProfSmallPine" class="social-links-menu" target="_blank" title="Follow me on twitter"><span class="genericon genericon-twitter"></span></a>
	
		<a href="https://github.com/jtimmer89" class="social-links-menu" target="_blank" title="Fork me on github"><span class="genericon genericon-github"></span></a>
	
		<a href="https://plus.google.com/+JosephTimmer" class="social-links-menu" target="_blank" title="Add me on google+"><span class="genericon genericon-googleplus-alt"></span></a>
	
		<a href="http://jtimmer89.github.io/atom.xml" class="social-links-menu" target="_blank" title="Subscribe to my feed"><span class="genericon genericon-feed"></span></a>
	
</div>
<div class="social-links" data-stellar-ratio="0.1">
	
		<a href="https://twitter.com/ProfSmallPine" target="_blank" title="Follow me on twitter"><span class="genericon genericon-twitter"></span></a>
	
		<a href="https://github.com/jtimmer89" target="_blank" title="Fork me on github"><span class="genericon genericon-github"></span></a>
	
		<a href="https://plus.google.com/+JosephTimmer" target="_blank" title="Add me on google+"><span class="genericon genericon-googleplus-alt"></span></a>
	
		<a href="http://jtimmer89.github.io/atom.xml" target="_blank" title="Subscribe to my feed"><span class="genericon genericon-feed"></span></a>
	
</div>

	<div id="post-info" data-stellar-ratio="0.7">
		<h1>Technical Blog: Blocks, Procs, and Lambdas</h1>
		
			<h2>What are these things in Ruby and how do you use them?</h2>
		
		<a class="site-title" href="http://jtimmer89.github.io/"><div class="site-icon-small" style="background-image: url(/assets/images/icon.png);"></div>Joe Timmer</a>, on 29 July 2014
	</div>
	<div id="nav-icon" style="bottom: 30px;" data-stellar-ratio="4">
		<a class="scroll" data-speed="500" href="#article"><span class="genericon genericon-expand"></span></a>
	</div>
</header>
<div id="middle">
	<div id="article">
		<h1 id="blocks">Blocks</h1>

<p>Blocks are, wait for it, blocks of code surrounded by curly braces or encapsulated in the do..end keywords. Let’s take a closer look at this. Many methods in Ruby iterate through an array or hash. As you iterate, you can do a block of code on each value. You can also use a block written outside of a method inside of the method by using the yield keyword. Let’s look at some examples to better understand what these words mean.</p>

<div id="article-img"><a href="/media/technical-blogs/blocks.png" target="_blank"><img src="/media/technical-blogs/blocks.png" alt="Example block" width="60%" /></a>
<blockquote style="border-left: 10px solid #D5D5D5;">Example block</blockquote></div>

<p>Line 1 creates an array with 5 numbers. Line 3 uses a block to multiply each value in the array by 2. Lines 5 - 7 do the same thing but with the different do..end syntax. Lines 9 to 13 show the use use of the yield keyword. The double_num method is ran, and at the yield keyword, the block of code after the call to the method on line 13 is run multiplying the number 3 by 2 and returning 6.</p>

<h2 id="procs">Procs</h2>

<p>I found that the best way to describe a proc, given our look into blocks, is a saved block. Blocks are not objects in Ruby. As such, they cannot be stored in a variable and used again. You would have to write a block each time you wanted to use it. Procs solve this problem. Let’s take a look at some code. </p>

<div id="article-img"><a href="/media/technical-blogs/procs.png" target="_blank"><img src="/media/technical-blogs/procs.png" alt="Example proc" width="60%" /></a>
<blockquote style="border-left: 10px solid #D5D5D5;">Example proc</blockquote></div>

<p>As before, we are taking the array with 5 values on line 1. Line 3 is creating a proc called double_nums that doubles a given value. Here, we are using the Proc.new syntax to create the reusable proc. Line 5 is an example that uses the proc through the &amp; symbol which calls the proc.</p>

<h2 id="lambdas">Lambdas</h2>

<p>Lambdas take a step from the old proc playbook. They are pretty similar with some syntax differences. They are also an object, and they offer the ability to be used repeatedly. The main difference relates to how the proc or lambda interacts within a method. When a lambda returns, it passes control back to the calling method. A proc does not. It ends once it is called without returning to the method. That might be confusing. Some code will help illustrate the syntax differences and the difference between procs and lambdas.</p>

<div id="article-img"><a href="/media/technical-blogs/lambdas.png" target="_blank"><img src="/media/technical-blogs/lambdas.png" alt="Example lambda vs. proc" width="60%" /></a>
<blockquote style="border-left: 10px solid #D5D5D5;">Example lambda vs. proc</blockquote></div>

<p>Lines 3 - 6 go with the theme we have been running with so far. The double-nums-lambda method on line 3 creates a lambda using the keyword lambda and the block of code. On line 5, it is passed in to the array, effectively doubling each number in array. Lines 10 - 24 illustrate one of the key ways procs and lambdas are different. The method say-hello-proc returns “Hi, I’m a proc” because it ends after the proc is called. The method say-hello-lambda returns “Hi, I’m a lambda” because it returns to that final statement after the lambda with the proc greeting is called.</p>

	</div>
	
	<div id="comments">
		
	
	<script src="https://apis.google.com/js/plusone.js"></script>
	<div id="g-comments" class="g-comments" data-href="http://jtimmer89.github.io//blog/technical-blog-week6" data-width="700" data-first_party_property="BLOGGER" data-view_type="FILTERED_POSTMOD"></div>
	<script>
		var width = $('#comments').width() * 0.9;
		width = Math.round(width);
		$("#g-comments").attr({ "data-width": width });
	</script>


	</div>
	
</div>

	
	
	
	
	
	

<footer class="clean" style="background-image: url(/media/cultural-blogs/MountainView_19.jpg); height: 75%; min-height: 500px;" data-stellar-background-ratio="0.5" data-stellar-horizontal-offset="50" data-stellar-vertical-offset="50">
	<div id="nav-icon" style="top: 60px;" data-stellar-ratio="0.8">
		<a class="scroll" data-speed="500" href="#article"><span class="genericon genericon-collapse"></span></a>
	</div>
	<div id="post-info" data-stellar-ratio="0.5" data-stellar-vertical-offset="120">
		<h3>Read next</h3>
		<a href="/blog/cultural-blog-week7">
			<h1>Cultural Blog: Personal Values</h1>
			
				<h2>How do your values affect the way you learn and interact?</h2>
			
		</a>
	</div>
	<p class="copyright">&copy;2014, <a href="http://camporez.com" target="_blank">Camporez</a>. <a href="http://creativecommons.org/licenses/by-nc/2.5" target="_blank">Some rights reserved</a>.</p>
</footer>
<script src="/assets/js/smooth-scroll.js"></script>

		<script>
		var width = $(window).width()
		if ( width > 899 )
		$( function(){
		    $.stellar({
		    	responsive: true,
			verticalScrolling: true,
			horizontalOffset: 0,
			verticalOffset: 0,
			positionProperty: 'transform',
		    });
		});
		</script>
	</body>
</html>
