<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Trim Agency &#187; ruby</title>
	<atom:link href="http://old.trimagency.com/tag/ruby/feed/" rel="self" type="application/rss+xml" />
	<link>http://old.trimagency.com</link>
	<description></description>
	<lastBuildDate>Mon, 29 Jun 2026 06:32:54 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>https://wordpress.org/?v=4.1.42</generator>
	<item>
		<title>Module Decorators</title>
		<link>http://old.trimagency.com/module-decorators/</link>
		<comments>http://old.trimagency.com/module-decorators/#comments</comments>
		<pubDate>Sun, 29 Jan 2017 15:37:10 +0000</pubDate>
		<dc:creator><![CDATA[Alex Miller]]></dc:creator>
				<category><![CDATA[Today I Learned]]></category>
		<category><![CDATA[ruby]]></category>

		<guid isPermaLink="false">http://trimagency.com/?p=2487</guid>
		<description><![CDATA[<p>Why decorate objects instead of using inheritance? It’s a more dynamic, flexible and transparent alternative to subclassing. There’s several different ways of using the decorator pattern in Ruby, but I’m just going to show the module/extend approach. You can read about other decorator techniques in Dan Croak’s blog post. Use a module and extend to [&#8230;]</p>
<p>The post <a rel="nofollow" href="http://old.trimagency.com/module-decorators/">Module Decorators</a> appeared first on <a rel="nofollow" href="http://old.trimagency.com">Trim Agency</a>.</p>
]]></description>
				<content:encoded><![CDATA[<p>Why decorate objects instead of using inheritance?  </p>
<p>It’s a more dynamic, flexible and transparent alternative to subclassing.  There’s several different ways of using the decorator pattern in Ruby, but I’m just going to show the module/<code>extend</code> approach.  You can read about other decorator techniques in <a href="https://robots.thoughtbot.com/evaluating-alternative-decorator-implementations-in">Dan Croak’s blog post</a>.</p>
<p>Use a module and <a href="">extend</a> to add instance methods on an object.  This will still maintain the ability to use super as demonstrated in this trivial example:</p>
<pre class="brush: ruby; title: ; notranslate">
class Report
  def number_of_pages
    10
  end
end

module CustomerAdmendment
  def number_of_pages
    super + 5
  end
end

module AdminAdmendment
  def number_of_pages
    super + 1
  end
end

report = Report.new
report.number_of_pages # =&gt; 10
report.extend(CustomerAdmendment).number_of_pages # =&gt; 15
report.extend(AdminAdmendment).number_of_pages  # =&gt; 16
</pre>
<p>With the module/<code>extend</code> technique we can maintain the original interface and delegate through all the decorators.</p>
<p>The post <a rel="nofollow" href="http://old.trimagency.com/module-decorators/">Module Decorators</a> appeared first on <a rel="nofollow" href="http://old.trimagency.com">Trim Agency</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://old.trimagency.com/module-decorators/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Ruby&#8217;s Safe Navigation Operator</title>
		<link>http://old.trimagency.com/rubys-safe-navigation-operator/</link>
		<comments>http://old.trimagency.com/rubys-safe-navigation-operator/#comments</comments>
		<pubDate>Fri, 20 Jan 2017 22:13:45 +0000</pubDate>
		<dc:creator><![CDATA[Alex Miller]]></dc:creator>
				<category><![CDATA[Today I Learned]]></category>
		<category><![CDATA[ruby]]></category>

		<guid isPermaLink="false">http://trimagency.com/?p=2413</guid>
		<description><![CDATA[<p>nil&#8216;s in Ruby can be annoying. Thankfully, Ruby version 2.3.0 gave us a safe navigation operator, something that other languages like C# and Swift already had. It&#8217;s very much like ActiveSupport&#8217;s try method, which checks if a receiver responds to a method before calling that method. If the receive doesn&#8217;t respond to the method, nil [&#8230;]</p>
<p>The post <a rel="nofollow" href="http://old.trimagency.com/rubys-safe-navigation-operator/">Ruby&#8217;s Safe Navigation Operator</a> appeared first on <a rel="nofollow" href="http://old.trimagency.com">Trim Agency</a>.</p>
]]></description>
				<content:encoded><![CDATA[<p><code>nil</code>&#8216;s in Ruby can be annoying. Thankfully, Ruby version 2.3.0 gave us a <a href="https://bugs.ruby-lang.org/issues/11537">safe navigation operator</a>, something that other languages like C# and Swift already had.  It&#8217;s very much like <a href="http://api.rubyonrails.org/v4.2.5/classes/Object.html#method-i-try-21">ActiveSupport&#8217;s <code>try</code> method</a>, which checks if a receiver responds to a method before calling that method.  If the receive doesn&#8217;t respond to the method, <code>nil</code> is returned.</p>
<h2>Usage</h2>
<p>Here is a trivial example to demonstrate how this works.  Let&#8217;s say you need to call <code>upcase</code> an object attribute that may or may not be defined. At some point, this might happen:</p>
<pre class="brush: ruby; title: ; notranslate">
person = Struct.new(:first_name)
bob = person.new
=&gt; #&lt;struct first_name=nil&gt;

bob.first_name.upcase
# =&gt; undefined method `upcase' for nil:NilClass
</pre>
<p>Before Ruby 2.3.0, we might have solved this problem with something like:</p>
<pre class="brush: ruby; title: ; notranslate">
bob.first_name.upcase if bob.first_name
=&gt; nil
</pre>
<p>Using the <i>Safe Navigation Operator</i> (<code>&#038;.</code>) we can do this:</p>
<pre class="brush: ruby; title: ; notranslate">
bob.first_name&amp;.upcase
=&gt; nil
</pre>
<p>In this example, calling <code>first_name</code> on the <code>bob</code> instance returns <code>nil</code>, so <code>&#038;.</code> halts the method chain right there.</p>
<p>The post <a rel="nofollow" href="http://old.trimagency.com/rubys-safe-navigation-operator/">Ruby&#8217;s Safe Navigation Operator</a> appeared first on <a rel="nofollow" href="http://old.trimagency.com">Trim Agency</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://old.trimagency.com/rubys-safe-navigation-operator/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
