RBS::Patch manages RBS (Ruby Signature) type definitions through patches. It applies incremental changes to existing RBS signatures.
override: Replace an existing method signaturedelete: Remove a method signatureappend_after: Insert a method signature after a specified methodprepend_before: Insert a method signature before a specified method
All operations use RBS annotations (e.g., %a{patch:override}), keeping patch files valid RBS syntax.
Install the gem and add to the application's Gemfile by executing:
bundle add rbs-patchIf bundler is not being used to manage dependencies, install the gem by executing:
gem install rbs-patch# Apply patches to RBS files
rbs-patch base.rbs patch1.rbs patch2.rbs
# Mix files and directories
rbs-patch lib/types/ sig/patches/
# Output goes to stdout - redirect to save
rbs-patch base.rbs patch.rbs > output.rbsrequire 'rbs/patch'
p = RBS::Patch.new
# Load from a single file
p.apply(path: Pathname("sig/user.rbs"))
# Load from a directory (all .rbs files)
p.apply(path: Pathname("sig/patches"))
# Apply from string
p.apply(<<~RBS)
class User
%a{patch:override}
def name: () -> String?
end
RBS
puts p.to_sAll patch operations use RBS annotations with the format %a{patch:operation} or %a{patch:operation:target}.
class User
%a{patch:override}
def name: () -> String? # Replaces existing method signature at the same position
endclass User
%a{patch:delete}
def email: () -> String # Removes this method from the class
endclass User
%a{patch:append_after(name)}
def nickname: () -> String? # Inserts after the 'name' method
endclass User
%a{patch:prepend_before(name)}
def id: () -> Integer # Inserts before the 'name' method
end%a{patch:override}
class User
def name: () -> String # Completely replaces the User class definition
end%a{patch:delete}
class User
end # Removes the entire User class%a{patch:append_after(User)}
class Admin
def permissions: () -> Array[String]
end # Inserts Admin class after User class%a{patch:prepend_before(User)}
class Guest
def readonly: () -> bool
end # Inserts Guest class before User classOperations work correctly within nested module structures:
module MyApp
module Models
%a{patch:append_after(User)}
class Admin
def role: () -> String
end
end
endWithout annotations, multiple class definitions are merged:
p.apply(<<~RBS)
class User
def name: () -> String
end
RBS
p.apply(<<~RBS)
class User
def email: () -> String # Adds to existing User class
end
RBS
# Result:
# class User
# def name: () -> String
# def email: () -> String
# endAfter checking out the repo, run bin/setup to install dependencies. Then, run rake test to run the tests. You can also run bin/console for an interactive prompt that will allow you to experiment.
To install this gem onto your local machine, run bundle exec rake install. To release a new version, update the version number in version.rb, and then run bundle exec rake release, which will create a git tag for the version, push git commits and the created tag, and push the .gem file to rubygems.org.
Bug reports and pull requests are welcome on GitHub at https://github.com/kozy4324/rbs-patch.
The gem is available as open source under the terms of the MIT License.