<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">
  <channel>
    <title>Ensure on Artem Yegorov</title>
    <link>https://yegorov.github.io/tags/ensure/</link>
    <description>Recent content in Ensure on Artem Yegorov</description>
    <generator>Hugo</generator>
    <language>en</language>
    <lastBuildDate>Wed, 10 Jun 2026 09:43:23 +0000</lastBuildDate>
    <atom:link href="https://yegorov.github.io/tags/ensure/index.xml" rel="self" type="application/rss+xml" />
    <item>
      <title>Use &#34;ensure&#34; for cleanup actions in your Ruby methods</title>
      <link>https://yegorov.github.io/notes/use-ensure-for-cleanup-actions-in-your-ruby-methods/</link>
      <pubDate>Wed, 10 Jun 2026 09:43:23 +0000</pubDate>
      <guid>https://yegorov.github.io/notes/use-ensure-for-cleanup-actions-in-your-ruby-methods/</guid>
      <description>&lt;p&gt;When developing instance methods in Ruby, one common problem is correctly restoring the object&amp;rsquo;s state upon method completion.&lt;/p&gt;&#xA;&lt;p&gt;For example, you need to modify instance variables for only one method call and then return them to their initial state. You could use a simple and obvious solution: save the original value in a temporary variable and then restore it at the end of the method.&lt;/p&gt;&#xA;&lt;p&gt;However, this will only work until an exception is thrown. Since the variable is not restored at the end of the method, the object&amp;rsquo;s state will be corrupted. To prevent this, move the variable restoration code to the &lt;code&gt;ensure&lt;/code&gt; block, as the code in it is always executed.&lt;/p&gt;</description>
    </item>
  </channel>
</rss>
