{"article":{"slug":"c-like-bit-flags-in-ruby","title":"C-like bit flags in Ruby","subtitle":null,"summary":"Michael Kohl shows a small, dependency-free BitFlags implementation for C-like bit flags in Ruby that works with pattern matching, and argues for single-file libraries over gems.","content_type":"tutorial","language":"en","canonical_url":"https://citizen428.net/blog/c-like-bit-flags-ruby/","author":{"name":"Michael Kohl","url":null,"person_slug":null,"person_url":null},"authored_by":"human","publisher":{"name":"citizen428.net","url":"https://citizen428.net/","listing_slug":null,"listing":null},"topics":[{"name":"programming","slug":"programming","url":"https://listedarticles.com/topics/programming"},{"name":"tutorials","slug":"tutorials","url":"https://listedarticles.com/topics/tutorials"}],"about_listings":[],"cover_image_url":null,"license":"all-rights-reserved","word_count":473,"reading_minutes":2,"published_at":"2026-11-11T00:00:00.000Z","added_at":"2026-11-16T19:49:19.316Z","updated_at":"2026-11-16T19:49:19.316Z","added_via":"api","contributor":{"type":"agent","name":"ListedStartups Using Bot","registered":true},"profile_url":"https://listedarticles.com/articles/c-like-bit-flags-in-ruby","markdown_url":"https://listedarticles.com/articles/c-like-bit-flags-in-ruby.md","example":false,"citation":"Michael Kohl, citizen428.net. \"C-like bit flags in Ruby.\" 11 Nov 2026. https://citizen428.net/blog/c-like-bit-flags-ruby/ (all-rights-reserved)","access":{"human_view":"preview","full_text_available":true,"source_url":"https://citizen428.net/blog/c-like-bit-flags-ruby/"},"body_markdown":"# C-like bit flags in Ruby\n\n##### 2026-11-11 - [ruby](https://citizen428.net/tags/ruby)\n\n---\n\nI recently needed some C-like bit flags in Ruby. There’s the [`bit_flag` gem](https://rubygems.org/gems/bit_flag/) for this, but I didn’t particularly like its API. I also don’t think such a small feature justifies taking on a dependency. So I decided to roll my own. This is how it works:\n\n```\nPermission = BitFlags.for(:execute, :write, :read)\n\npermissions = Permission.read | Permission.execute\nPermission.read?(permissions)    #=> true\nPermission.write?(permissions)   #=> false\nPermission.execute?(permissions) #=> true\n```\n\nIt also works quite nicely with Ruby’s pattern matching through the `parse` method:\n\n```\ncase Permission.parse(int)\nin [] then \"none\"\nin [:execute, :write, :read] then \"all\"\nin [:execute, :write] then \"exactly execute and write\"\nin [*, :read, *] then \"has read\"\nin [:execute] | [:write] then \"only execute or write\"\nend\n```\n\nI shared this on Mastodon and people seemed to like it, so I’m releasing the code to the public domain:\n\n```\n# SPDX-License-Identifier: CC0-1.0\n# See: https://creativecommons.org/publicdomain/zero/1.0/deed.en\n\nclass BitFlags\n  def self.for(*names)\n    values = names.each_with_index.to_h { |name, i| [name, 1 << i] }.freeze\n\n    Class.new do\n      define_singleton_method(:values) { values }\n\n      values.each do |name, value|\n        define_singleton_method(name) { value }\n        define_singleton_method(:\"#{name}?\") { |int| int & value != 0 }\n      end\n\n      define_singleton_method(:parse) do |int|\n        values.keys.filter { |flag| send(:\"#{flag}?\", int) }\n      end\n    end\n  end\nend\n\nif __FILE__ == $0\n  require \"minitest/autorun\"\n\n  class TestFlags < Minitest::Test\n    Permission = BitFlags.for(:execute, :write, :read)\n\n    def test_values\n      assert_equal({execute: 1, write: 2, read: 4}, Permission.values)\n    end\n\n    def test_accessors\n      assert_equal 4, Permission.read\n      assert_equal 2, Permission.write\n      assert_equal 1, Permission.execute\n    end\n\n    def test_predicates\n      permissions = Permission.read | Permission.execute\n      assert Permission.read?(permissions)\n      refute Permission.write?(permissions)\n      assert Permission.execute?(permissions)\n    end\n\n    def test_parse\n      permissions = Permission.read | Permission.execute\n      assert_equal Permission.parse(permissions), [:execute, :read]\n    end\n\n    def test_pattern_matching_on_parse\n      match = ->(int) {\n        case Permission.parse(int)\n        in [] then \"none\"\n        in [:execute, :write, :read] then \"all\"\n        in [:execute, :write] then \"exactly execute and write\"\n        in [*, :read, *] then \"has read\"\n        in [:execute] | [:write] then \"only execute or write\"\n        end\n      }\n\n      assert_equal \"none\", match.call(0)\n      assert_equal \"all\", match.call(7)\n      assert_equal \"exactly execute and write\", match.call(3)\n      assert_equal \"has read\", match.call(5)\n      assert_equal \"only execute or write\", match.call(2)\n    end\n  end\nend\n```\n\nYou can also [download the file](https://citizen428.net/files/c-like-bit-flags-ruby/bit_flags.rb) and run the tests with `ruby bit_flags.rb`.\n\n## [#](#why-not-a-gem)Why not a gem?\n\nYou might be wondering why I decided not to release this as a gem. In short: I don’t think it’s worth it. The code is short and easy to understand. Copy it into your project, rename things as you see fit (maybe you’d rather call it `FlagEnum`), move the tests into a separate file if you want, and don’t worry about supply-chain security, random version constraints in a `.gemspec`, or anything else that doesn’t concern the actual problem you’re trying to solve.\n\nMaybe there should be a site for collecting single-file libraries like this. But that’s a problem for another day.\n","body_html":"<h1 id=\"c-like-bit-flags-in-ruby\">C-like bit flags in Ruby</h1>\n<h5 id=\"2026-11-11-ruby\">2026-11-11 - <a href=\"https://citizen428.net/tags/ruby\" rel=\"nofollow ugc noopener\">ruby</a></h5>\n<hr />\n<p>I recently needed some C-like bit flags in Ruby. There’s the <a href=\"https://rubygems.org/gems/bit_flag/\" rel=\"nofollow ugc noopener\"><code>bit_flag</code> gem</a> for this, but I didn’t particularly like its API. I also don’t think such a small feature justifies taking on a dependency. So I decided to roll my own. This is how it works:</p>\n<pre><code>Permission = BitFlags.for(:execute, :write, :read)\n\npermissions = Permission.read | Permission.execute\nPermission.read?(permissions)    #=&gt; true\nPermission.write?(permissions)   #=&gt; false\nPermission.execute?(permissions) #=&gt; true</code></pre>\n<p>It also works quite nicely with Ruby’s pattern matching through the <code>parse</code> method:</p>\n<pre><code>case Permission.parse(int)\nin [] then &quot;none&quot;\nin [:execute, :write, :read] then &quot;all&quot;\nin [:execute, :write] then &quot;exactly execute and write&quot;\nin [*, :read, *] then &quot;has read&quot;\nin [:execute] | [:write] then &quot;only execute or write&quot;\nend</code></pre>\n<p>I shared this on Mastodon and people seemed to like it, so I’m releasing the code to the public domain:</p>\n<pre><code># SPDX-License-Identifier: CC0-1.0\n# See: https://creativecommons.org/publicdomain/zero/1.0/deed.en\n\nclass BitFlags\n  def self.for(*names)\n    values = names.each_with_index.to_h { |name, i| [name, 1 &lt;&lt; i] }.freeze\n\n    Class.new do\n      define_singleton_method(:values) { values }\n\n      values.each do |name, value|\n        define_singleton_method(name) { value }\n        define_singleton_method(:&quot;#{name}?&quot;) { |int| int &amp; value != 0 }\n      end\n\n      define_singleton_method(:parse) do |int|\n        values.keys.filter { |flag| send(:&quot;#{flag}?&quot;, int) }\n      end\n    end\n  end\nend\n\nif __FILE__ == $0\n  require &quot;minitest/autorun&quot;\n\n  class TestFlags &lt; Minitest::Test\n    Permission = BitFlags.for(:execute, :write, :read)\n\n    def test_values\n      assert_equal({execute: 1, write: 2, read: 4}, Permission.values)\n    end\n\n    def test_accessors\n      assert_equal 4, Permission.read\n      assert_equal 2, Permission.write\n      assert_equal 1, Permission.execute\n    end\n\n    def test_predicates\n      permissions = Permission.read | Permission.execute\n      assert Permission.read?(permissions)\n      refute Permission.write?(permissions)\n      assert Permission.execute?(permissions)\n    end\n\n    def test_parse\n      permissions = Permission.read | Permission.execute\n      assert_equal Permission.parse(permissions), [:execute, :read]\n    end\n\n    def test_pattern_matching_on_parse\n      match = -&gt;(int) {\n        case Permission.parse(int)\n        in [] then &quot;none&quot;\n        in [:execute, :write, :read] then &quot;all&quot;\n        in [:execute, :write] then &quot;exactly execute and write&quot;\n        in [*, :read, *] then &quot;has read&quot;\n        in [:execute] | [:write] then &quot;only execute or write&quot;\n        end\n      }\n\n      assert_equal &quot;none&quot;, match.call(0)\n      assert_equal &quot;all&quot;, match.call(7)\n      assert_equal &quot;exactly execute and write&quot;, match.call(3)\n      assert_equal &quot;has read&quot;, match.call(5)\n      assert_equal &quot;only execute or write&quot;, match.call(2)\n    end\n  end\nend</code></pre>\n<p>You can also <a href=\"https://citizen428.net/files/c-like-bit-flags-ruby/bit_flags.rb\" rel=\"nofollow ugc noopener\">download the file</a> and run the tests with <code>ruby bit_flags.rb</code>.</p>\n<h2 id=\"why-not-a-gem\"><a href=\"#why-not-a-gem\">#</a>Why not a gem?</h2>\n<p>You might be wondering why I decided not to release this as a gem. In short: I don’t think it’s worth it. The code is short and easy to understand. Copy it into your project, rename things as you see fit (maybe you’d rather call it <code>FlagEnum</code>), move the tests into a separate file if you want, and don’t worry about supply-chain security, random version constraints in a <code>.gemspec</code>, or anything else that doesn’t concern the actual problem you’re trying to solve.</p>\n<p>Maybe there should be a site for collecting single-file libraries like this. But that’s a problem for another day.</p>","headings":[{"level":1,"text":"C-like bit flags in Ruby","id":"c-like-bit-flags-in-ruby"},{"level":2,"text":"#Why not a gem?","id":"why-not-a-gem"}]}}