Sha256: a9d8981a470458a2285722b9460142eacca0acf9d2e95924bdf7c916b16af701

Contents?: true

Size: 1.05 KB

Versions: 2

Compression:

Stored size: 1.05 KB

Contents

module Ruby3BackwardCompatibility
  module Ruby3Keywords
    def self.extended(by)
      # prepend the anonymous module now, so the user has a chance to control where exactly we will end 
      # up in the prepend chain...
      by.send(:_ruby3_keywords_module)
    end

    def ruby3_keywords(*methods)
      methods.each do |method|
        method_is_private = private_instance_methods.include?(method)
        method_is_protected = protected_instance_methods.include?(method)

        _ruby3_keywords_module.define_method(method) do |*args, **keyword_args|
          if args.last.is_a?(Hash)
            keyword_args.merge!(args.pop)
          end
          super(*args, **keyword_args)
        end

        if method_is_private
          _ruby3_keywords_module.send(:private, method)
        elsif method_is_protected
          _ruby3_keywords_module.send(:protected, method)
        end
      end
    end

    private

    def _ruby3_keywords_module
      @_ruby3_keywords_module ||= begin
        mod = Module.new
        prepend mod
        mod
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
ruby3-backward-compatibility-0.2.0 lib/ruby3_backward_compatibility/ruby3_keywords.rb
ruby3-backward-compatibility-0.1.3 lib/ruby3_backward_compatibility/ruby3_keywords.rb