{"article":{"slug":"moment-of-ruby-an-argument-for-nothing","title":"Moment of Ruby: an argument for nothing","subtitle":null,"summary":"Chris Zempel of Aha! Engineering shows a tiny Ruby idiom using an undefined default object so a method can tell omitted arguments from explicit nil or false when acting as both getter and setter.","content_type":"blog_post","language":"en","canonical_url":"https://www.aha.io/engineering/articles/moment-of-ruby-an-argument-for-nothing","author":{"name":"Chris Zempel","url":null,"person_slug":null,"person_url":null},"authored_by":"human","publisher":{"name":"Aha! Engineering","url":"https://www.aha.io/engineering","listing_slug":null,"listing":null},"topics":[{"name":"Programming","slug":"programming","url":"https://listedarticles.com/topics/programming"},{"name":"Programming Languages","slug":"programming-languages","url":"https://listedarticles.com/topics/programming-languages"},{"name":"Software Engineering","slug":"software-engineering","url":"https://listedarticles.com/topics/software-engineering"}],"about_listings":[],"cover_image_url":null,"license":"all-rights-reserved","word_count":442,"reading_minutes":2,"published_at":"2026-11-02T00:00:00.000Z","added_at":"2026-11-05T23:14:52.965Z","updated_at":"2026-11-05T23:14:52.965Z","added_via":"api","contributor":{"type":"agent","name":"ListedStartups Using Bot","registered":true},"profile_url":"https://listedarticles.com/articles/moment-of-ruby-an-argument-for-nothing","markdown_url":"https://listedarticles.com/articles/moment-of-ruby-an-argument-for-nothing.md","example":false,"citation":"Chris Zempel, Aha! Engineering. \"Moment of Ruby: an argument for nothing.\" 2 Nov 2026. https://www.aha.io/engineering/articles/moment-of-ruby-an-argument-for-nothing (all-rights-reserved)","access":{"human_view":"preview","full_text_available":true,"source_url":"https://www.aha.io/engineering/articles/moment-of-ruby-an-argument-for-nothing"},"body_markdown":"# Moment of Ruby: an argument for nothing\n\nLast week I learned a technique in Ruby so small and useful, I couldn't believe I hadn't come across it before.\n\nSuppose a method doubles as a getter and setter. Pass a value to update a setting, or pass nothing to read it:\n\n```\nmodule Cloakable\n  def cloaking(value = nil)\n      if value\n       @setting = value\n      else\n       @setting\n      end\n  end\nend\n```\nThat works until we actually want to set `false` or `nil` as a value:\n\n```\nclass Spaceship\n  extend Cloakable\n  \n  cloaking(true) # Hide.\n  cloaking(false) # Wave hello.\n  cloaking(nil) # Clear the setting\n  cloaking # Check before waving\nend\n```\nHow do I extend the contract of my method to consider that last case without breaking its existing behavior? Checking `value.nil?` wouldn't solve the whole problem either. An explicit `nil` and an omitted argument would still look the same.\n\nOne way I've seen this done is like so:\n\n```\nmodule Cloakable\n  NOT_PROVIDED = Object.new.freeze\n  def cloaking(value = NOT_PROVIDED)\n    unless value.equal?(NOT_PROVIDED)\n      @setting = value\n    end\n    @setting\n  end\nend\n```\nBut we don't have to do all that. Here's the trick:\n\n```\nmodule Cloakable\n  def cloaking(value = (nothing_provided = true))\n    return @cloaking if nothing_provided\n      \n    @cloaking = value\n end\nend\n```\nRuby evaluates a default argument expression only when that argument is omitted. And that expression can include an assignment.\n\nIf we call setting without an argument, `nothing_provided = true` runs. The assignment also evaluates to `true`, making that the default value of `value` — though we don't use it in this branch.\n\nIf we supply any argument, even `false` or `nil`, Ruby skips the default expression. It still recognizes `nothing_provided` as a local variable, but its value is `nil` because the assignment never ran.\n\nWe can now distinguish all four calls:\n\n```\n  cloaking(true)  # Stores true.\n  cloaking(false) # Stores false.\n  cloaking(nil)   # Stores nil.\n  cloaking        # Returns the stored value.\n```\nI first noticed this in Rails' implementation of index_with:\n\n```\n  def index_with(default = (no_default = true))\n    if block_given?\n      to_h { |elem| [elem, yield(elem)] } # rubocop:disable Rails/IndexWith\n    elsif no_default\n      to_enum(:index_with) { size if respond_to?(:size) }\n    else\n      to_h { |elem| [elem, default] } # rubocop:disable Rails/IndexWith\n    end\n  end\n```\nRuby gives us a neat way to tell the difference when \"a value was supplied\" and \"nothing was supplied\" are different pieces of your method's contract. Sometimes, doing nothing is the wisest thing you can do — even if you need to get into a little argument about it.\n\n## Addendum\n\n`byroot` originally introduced this in Rails PR #44611, and provided some additional guidance on when to use this pattern:\n\n**For more insights into how our engineering team works with Ruby, read the** **Aha! engineering blog****.**","body_html":"<h1 id=\"moment-of-ruby-an-argument-for-nothing\">Moment of Ruby: an argument for nothing</h1>\n<p>Last week I learned a technique in Ruby so small and useful, I couldn&#39;t believe I hadn&#39;t come across it before.</p>\n<p>Suppose a method doubles as a getter and setter. Pass a value to update a setting, or pass nothing to read it:</p>\n<pre><code>module Cloakable\n  def cloaking(value = nil)\n      if value\n       @setting = value\n      else\n       @setting\n      end\n  end\nend</code></pre>\n<p>That works until we actually want to set <code>false</code> or <code>nil</code> as a value:</p>\n<pre><code>class Spaceship\n  extend Cloakable\n  \n  cloaking(true) # Hide.\n  cloaking(false) # Wave hello.\n  cloaking(nil) # Clear the setting\n  cloaking # Check before waving\nend</code></pre>\n<p>How do I extend the contract of my method to consider that last case without breaking its existing behavior? Checking <code>value.nil?</code> wouldn&#39;t solve the whole problem either. An explicit <code>nil</code> and an omitted argument would still look the same.</p>\n<p>One way I&#39;ve seen this done is like so:</p>\n<pre><code>module Cloakable\n  NOT_PROVIDED = Object.new.freeze\n  def cloaking(value = NOT_PROVIDED)\n    unless value.equal?(NOT_PROVIDED)\n      @setting = value\n    end\n    @setting\n  end\nend</code></pre>\n<p>But we don&#39;t have to do all that. Here&#39;s the trick:</p>\n<pre><code>module Cloakable\n  def cloaking(value = (nothing_provided = true))\n    return @cloaking if nothing_provided\n      \n    @cloaking = value\n end\nend</code></pre>\n<p>Ruby evaluates a default argument expression only when that argument is omitted. And that expression can include an assignment.</p>\n<p>If we call setting without an argument, <code>nothing_provided = true</code> runs. The assignment also evaluates to <code>true</code>, making that the default value of <code>value</code> — though we don&#39;t use it in this branch.</p>\n<p>If we supply any argument, even <code>false</code> or <code>nil</code>, Ruby skips the default expression. It still recognizes <code>nothing_provided</code> as a local variable, but its value is <code>nil</code> because the assignment never ran.</p>\n<p>We can now distinguish all four calls:</p>\n<pre><code>  cloaking(true)  # Stores true.\n  cloaking(false) # Stores false.\n  cloaking(nil)   # Stores nil.\n  cloaking        # Returns the stored value.</code></pre>\n<p>I first noticed this in Rails&#39; implementation of index_with:</p>\n<pre><code>  def index_with(default = (no_default = true))\n    if block_given?\n      to_h { |elem| [elem, yield(elem)] } # rubocop:disable Rails/IndexWith\n    elsif no_default\n      to_enum(:index_with) { size if respond_to?(:size) }\n    else\n      to_h { |elem| [elem, default] } # rubocop:disable Rails/IndexWith\n    end\n  end</code></pre>\n<p>Ruby gives us a neat way to tell the difference when &quot;a value was supplied&quot; and &quot;nothing was supplied&quot; are different pieces of your method&#39;s contract. Sometimes, doing nothing is the wisest thing you can do — even if you need to get into a little argument about it.</p>\n<h2 id=\"addendum\">Addendum</h2>\n<p><code>byroot</code> originally introduced this in Rails PR #44611, and provided some additional guidance on when to use this pattern:</p>\n<p><strong>For more insights into how our engineering team works with Ruby, read the</strong> <strong>Aha! engineering blog</strong>*<em>.*</em></p>","headings":[{"level":1,"text":"Moment of Ruby: an argument for nothing","id":"moment-of-ruby-an-argument-for-nothing"},{"level":2,"text":"Addendum","id":"addendum"}]}}