Project

final

0.0
No release in over 3 years
Low commit activity in last 3 years
The final library enables you to declare your classes as final. This prevents your class from being subclassed or having its methods redefined.
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
 Dependencies

Development

>= 0
~> 3.9
 Project Readme

Ruby

Description

A Ruby module that can make your classes final. A final class cannot be subclassed, and its instance methods cannot be redefined.

Installation

gem install final

Adding the trusted cert

gem cert --add <(curl -Ls https://raw.githubusercontent.com/djberg96/final/main/certs/djberg96_pub.pem)

Synopsis

require 'final'

class Alpha
  include Final

  def foo
    puts "Hello"
  end
end

# Error! You cannot subclass Alpha.
class Beta < Alpha; end

# Error! You cannot redefine an existing method.
class Alpha
  def foo
    puts "Hi"
  end
end

# Not an error. You can add new methods to an existing class.
class Alpha
  def bar
    puts "World"
  end
end

Developer's Notes

This library is merely a proof of concept that was published as the result of a tweet made by James Edward Gray II, where he commented that Ruby core classes should never be subclassed. Knowing that static languages like Java implement the "final" keyword as a way to prevent subclassing, I wanted to see if something similar could be done for Ruby. Turns out it can.

I do not, however, necessarily advocate actually using this library in production code. Freezing a class this way is contrary to the very nature of Ruby and dynamic languages in general. If you're going to use it, you had better have a very good reason for doing so!

Caveats

Only instance methods are prevented from redefinition. Singleton methods are not final. This change may happen in a future release.

License

Apache-2.0

Copyright

(C) 2003-2021 Daniel J. Berger All Rights Reserved.

Warranty

This package is provided "as is" and without any express or implied warranties, including, without limitation, the implied warranties of merchantability and fitness for a particular purpose.

Author

Daniel J. Berger