From 53833c0f660239eeb572dd33d4a1fac503c4834a Mon Sep 17 00:00:00 2001 From: Nobuyoshi Nakada Date: Sun, 27 Dec 2020 17:05:37 +0900 Subject: [PATCH 01/14] Support Hash.ruby2_keywords_hash? --- lib/ruby2_keywords.rb | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/lib/ruby2_keywords.rb b/lib/ruby2_keywords.rb index 8c48333..a41cc2a 100644 --- a/lib/ruby2_keywords.rb +++ b/lib/ruby2_keywords.rb @@ -21,3 +21,14 @@ def ruby2_keywords end end end + +class Hash + unless respond_to?(:ruby2_keywords_hash?) + begin + $VERBOSE, verbose = nil, $VERBOSE + proc {|*a, **h| h}.call( + ensure + $VERBOSE = verbose + end + end +end From 46ed72d40db163f9edbddbe6e5706794484ac5bb Mon Sep 17 00:00:00 2001 From: Antonio Terceiro Date: Fri, 3 Apr 2020 14:50:29 -0300 Subject: [PATCH 02/14] Add explicit license file Fixes #4 --- LICENSE | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) create mode 100644 LICENSE diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..3b9d383 --- /dev/null +++ b/LICENSE @@ -0,0 +1,22 @@ +Copyright 2019-2020 Nobuyoshi Nakada, Yusuke Endoh + +Redistribution and use in source and binary forms, with or without +modification, are permitted provided that the following conditions are met: + +1. Redistributions of source code must retain the above copyright notice, this + list of conditions and the following disclaimer. + +2. Redistributions in binary form must reproduce the above copyright notice, + this list of conditions and the following disclaimer in the documentation + and/or other materials provided with the distribution. + +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND +ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED +WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE +DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE +FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL +DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR +SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER +CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, +OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. From 92e74341dffc9a41d7671ea82709ba2e091ef4e8 Mon Sep 17 00:00:00 2001 From: Nobuyoshi Nakada Date: Sun, 27 Dec 2020 17:43:35 +0900 Subject: [PATCH 03/14] Added BSD-2-Clause to the licenses of the gemspec --- ruby2_keywords.gemspec | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ruby2_keywords.gemspec b/ruby2_keywords.gemspec index 623c58c..952d8cd 100644 --- a/ruby2_keywords.gemspec +++ b/ruby2_keywords.gemspec @@ -5,7 +5,7 @@ Gem::Specification.new do |s| s.version = version s.summary = "Shim library for Module#ruby2_keywords" s.homepage = "https://github.com/ruby/ruby2_keywords" - s.licenses = ["Ruby"] + s.licenses = ["Ruby", "BSD-2-Clause"] s.authors = ["Nobuyoshi Nakada"] s.require_paths = ["lib"] s.files = [ From 23981c5296aec6c5dbe104b8adc7ca0e85cb4313 Mon Sep 17 00:00:00 2001 From: Yusuke Endoh Date: Mon, 28 Dec 2020 14:07:40 +0900 Subject: [PATCH 04/14] Add an example for Module#define_method (#7) --- README.md | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/README.md b/README.md index f3b56aa..a2c3401 100644 --- a/README.md +++ b/README.md @@ -42,6 +42,15 @@ ruby2_keywords def oldstyle_keywords(options = {}) end ``` +You can do the same for a method defined by `Module#define_method`: + +```ruby +define_method :delegating_method do |*args, &block| + other_method(*args, &block) +end +ruby2_keywords :delegating_method +``` + ## Contributing Bug reports and pull requests are welcome on GitHub at https://bugs.ruby-lang.org. From 52c15f0e55dfdcb8204e92c85a4dd5d524549533 Mon Sep 17 00:00:00 2001 From: Yusuke Endoh Date: Thu, 7 Jan 2021 17:39:52 +0900 Subject: [PATCH 05/14] Use private_method_defined? instead of respond_to? `Module.respond_to?(:ruby2_keywords, true)` does NOT check if `Module#ruby2_keywords` is available. It worked well because there is toplevel `ruby2_keywords` method, but using `private_method_defined?` is better, I think. Also, this fixes a syntactic error. --- lib/ruby2_keywords.rb | 11 ++++------- 1 file changed, 4 insertions(+), 7 deletions(-) diff --git a/lib/ruby2_keywords.rb b/lib/ruby2_keywords.rb index a41cc2a..7d16a08 100644 --- a/lib/ruby2_keywords.rb +++ b/lib/ruby2_keywords.rb @@ -1,5 +1,5 @@ class Module - unless respond_to?(:ruby2_keywords, true) + unless private_method_defined?(:ruby2_keywords, true) private def ruby2_keywords(name, *) # nil @@ -23,12 +23,9 @@ def ruby2_keywords end class Hash - unless respond_to?(:ruby2_keywords_hash?) - begin - $VERBOSE, verbose = nil, $VERBOSE - proc {|*a, **h| h}.call( - ensure - $VERBOSE = verbose + unless method_defined?(:ruby2_keywords_hash?) + def ruby2_keywords_hash? + false end end end From cbecd4307612f6794962a701cb16ac620872c1f9 Mon Sep 17 00:00:00 2001 From: Nobuyoshi Nakada Date: Tue, 19 Jan 2021 12:13:21 +0900 Subject: [PATCH 06/14] Added version guard against the default gem --- ruby2_keywords.gemspec | 1 + 1 file changed, 1 insertion(+) diff --git a/ruby2_keywords.gemspec b/ruby2_keywords.gemspec index 952d8cd..849bd1f 100644 --- a/ruby2_keywords.gemspec +++ b/ruby2_keywords.gemspec @@ -1,4 +1,5 @@ version = IO.popen(%W[git -C #{__dir__} describe --tags --match v[0-9]*], &:read)[/\Av?(\d+(?:\.\d+)*)/, 1] +abort "Version must not reach 1" if version[/\d+/].to_i >= 1 Gem::Specification.new do |s| s.name = "ruby2_keywords" From a841a82a1ff485ab6dd5759f6f31dff17de45b65 Mon Sep 17 00:00:00 2001 From: Nobuyoshi Nakada Date: Tue, 19 Jan 2021 14:41:53 +0900 Subject: [PATCH 07/14] README: fix Contributing and License --- README.md | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index a2c3401..4cc0282 100644 --- a/README.md +++ b/README.md @@ -53,8 +53,15 @@ ruby2_keywords :delegating_method ## Contributing -Bug reports and pull requests are welcome on GitHub at https://bugs.ruby-lang.org. +Bug reports and pull requests are welcome on [GitHub] or +[Ruby Issue Tracking System]. ## License -The gem is available as open source under the terms of the [2-Clause BSD License](https://opensource.org/licenses/BSD-2-Clause). +The gem is available as open source under the terms of the +[Ruby License] or the [2-Clause BSD License]. + +[GitHub]: https://github.com/ruby/ruby2_keywords/ +[Ruby Issue Tracking System]: https://bugs.ruby-lang.org +[Ruby License]: https://www.ruby-lang.org/en/about/license.txt +[2-Clause BSD License]: https://opensource.org/licenses/BSD-2-Clause From 2ee450c041cb1a3b15580c3963b778b33926503c Mon Sep 17 00:00:00 2001 From: Nobuyoshi Nakada Date: Tue, 19 Jan 2021 18:53:19 +0900 Subject: [PATCH 08/14] Package LICENSE file The source gemspec file is useless after building the gem file. --- ruby2_keywords.gemspec | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ruby2_keywords.gemspec b/ruby2_keywords.gemspec index 849bd1f..47a7583 100644 --- a/ruby2_keywords.gemspec +++ b/ruby2_keywords.gemspec @@ -10,8 +10,8 @@ Gem::Specification.new do |s| s.authors = ["Nobuyoshi Nakada"] s.require_paths = ["lib"] s.files = [ + "LICENSE", "README.md", "lib/ruby2_keywords.rb", - "ruby2_keywords.gemspec", ] end From 51c47c060d9678ae2c28bcf415bc87346cba1860 Mon Sep 17 00:00:00 2001 From: Nobuyoshi Nakada Date: Tue, 19 Jan 2021 19:19:09 +0900 Subject: [PATCH 09/14] Define Hash.ruby2_keywords_hash? singleton method --- lib/ruby2_keywords.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/ruby2_keywords.rb b/lib/ruby2_keywords.rb index 7d16a08..e93fb5e 100644 --- a/lib/ruby2_keywords.rb +++ b/lib/ruby2_keywords.rb @@ -22,9 +22,9 @@ def ruby2_keywords end end -class Hash +class << Hash unless method_defined?(:ruby2_keywords_hash?) - def ruby2_keywords_hash? + def ruby2_keywords_hash?(hash) false end end From 52b8acf6a89de00f44c8854f0e30c2be4a3d7cb3 Mon Sep 17 00:00:00 2001 From: Nobuyoshi Nakada Date: Tue, 19 Jan 2021 19:19:59 +0900 Subject: [PATCH 10/14] Define Hash.ruby2_keywords_hash singleton method --- lib/ruby2_keywords.rb | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/lib/ruby2_keywords.rb b/lib/ruby2_keywords.rb index e93fb5e..671847b 100644 --- a/lib/ruby2_keywords.rb +++ b/lib/ruby2_keywords.rb @@ -28,4 +28,10 @@ def ruby2_keywords_hash?(hash) false end end + + unless method_defined?(:ruby2_keywords_hash) + def ruby2_keywords_hash(hash) + hash.dup + end + end end From 5093cd212b44d1fbd8ef1c6b3f2bfa8f3427de16 Mon Sep 17 00:00:00 2001 From: Nobuyoshi Nakada Date: Tue, 19 Jan 2021 19:21:06 +0900 Subject: [PATCH 11/14] Added least documents --- lib/ruby2_keywords.rb | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/lib/ruby2_keywords.rb b/lib/ruby2_keywords.rb index 671847b..9e29bec 100644 --- a/lib/ruby2_keywords.rb +++ b/lib/ruby2_keywords.rb @@ -1,5 +1,9 @@ class Module unless private_method_defined?(:ruby2_keywords, true) + # call-seq: + # ruby2_keywords(method_name, ...) + # + # Does nothing. private def ruby2_keywords(name, *) # nil @@ -9,6 +13,10 @@ def ruby2_keywords(name, *) main = TOPLEVEL_BINDING.receiver unless main.respond_to?(:ruby2_keywords, true) + # call-seq: + # ruby2_keywords(method_name, ...) + # + # Does nothing. def main.ruby2_keywords(name, *) # nil end @@ -16,6 +24,10 @@ def main.ruby2_keywords(name, *) class Proc unless method_defined?(:ruby2_keywords) + # call-seq: + # proc.ruby2_keywords -> proc + # + # Does nothing and just returns the receiver. def ruby2_keywords self end @@ -24,12 +36,20 @@ def ruby2_keywords class << Hash unless method_defined?(:ruby2_keywords_hash?) + # call-seq: + # Hash.ruby2_keywords_hash?(hash) -> false + # + # Returns false. def ruby2_keywords_hash?(hash) false end end unless method_defined?(:ruby2_keywords_hash) + # call-seq: + # Hash.ruby2_keywords_hash(hash) -> new_hash + # + # Duplicates a given hash and returns the new hash. def ruby2_keywords_hash(hash) hash.dup end From 9603fec096b257d382776c09ab1f5fe88d289307 Mon Sep 17 00:00:00 2001 From: Nobuyoshi Nakada Date: Tue, 19 Jan 2021 20:19:09 +0900 Subject: [PATCH 12/14] Make README.md the main page --- ruby2_keywords.gemspec | 1 + 1 file changed, 1 insertion(+) diff --git a/ruby2_keywords.gemspec b/ruby2_keywords.gemspec index 47a7583..56d4df9 100644 --- a/ruby2_keywords.gemspec +++ b/ruby2_keywords.gemspec @@ -9,6 +9,7 @@ Gem::Specification.new do |s| s.licenses = ["Ruby", "BSD-2-Clause"] s.authors = ["Nobuyoshi Nakada"] s.require_paths = ["lib"] + s.rdoc_options = ["--main", "README.md"] s.files = [ "LICENSE", "README.md", From aa06490df9efa905ef17c143e96edee547c4ffad Mon Sep 17 00:00:00 2001 From: Nobuyoshi Nakada Date: Tue, 19 Jan 2021 20:20:31 +0900 Subject: [PATCH 13/14] Fixed RDoc location --- lib/ruby2_keywords.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/ruby2_keywords.rb b/lib/ruby2_keywords.rb index 9e29bec..97cd081 100644 --- a/lib/ruby2_keywords.rb +++ b/lib/ruby2_keywords.rb @@ -1,10 +1,10 @@ class Module unless private_method_defined?(:ruby2_keywords, true) + private # call-seq: # ruby2_keywords(method_name, ...) # # Does nothing. - private def ruby2_keywords(name, *) # nil end From 396cc7991604632bc686e3c363504db42337cca3 Mon Sep 17 00:00:00 2001 From: Nobuyoshi Nakada Date: Tue, 19 Jan 2021 20:57:52 +0900 Subject: [PATCH 14/14] Added tests --- .github/workflows/test.yml | 20 +++++++++++++++++++ Rakefile | 8 ++++++++ ruby2_keywords.gemspec | 2 +- test/test_keyword.rb | 41 ++++++++++++++++++++++++++++++++++++++ 4 files changed, 70 insertions(+), 1 deletion(-) create mode 100644 .github/workflows/test.yml create mode 100644 Rakefile create mode 100644 test/test_keyword.rb diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml new file mode 100644 index 0000000..b79ea7d --- /dev/null +++ b/.github/workflows/test.yml @@ -0,0 +1,20 @@ +name: ubuntu + +on: [push, pull_request] + +jobs: + build: + name: build (${{ matrix.ruby }} / ${{ matrix.os }}) + strategy: + matrix: + ruby: [ '3.0', 2.7, 2.6, head ] + os: [ ubuntu-latest, macos-latest, windows-latest ] + runs-on: ${{ matrix.os }} + steps: + - uses: actions/checkout@master + - name: Set up Ruby + uses: ruby/setup-ruby@v1 + with: + ruby-version: ${{ matrix.ruby }} + - name: Run test + run: rake test diff --git a/Rakefile b/Rakefile new file mode 100644 index 0000000..8830e05 --- /dev/null +++ b/Rakefile @@ -0,0 +1,8 @@ +require "bundler/gem_tasks" +require "rake/testtask" + +Rake::TestTask.new(:test) do |t| + t.test_files = FileList["test/**/test_*.rb"] +end + +task :default => :test diff --git a/ruby2_keywords.gemspec b/ruby2_keywords.gemspec index 56d4df9..5eac57b 100644 --- a/ruby2_keywords.gemspec +++ b/ruby2_keywords.gemspec @@ -1,4 +1,4 @@ -version = IO.popen(%W[git -C #{__dir__} describe --tags --match v[0-9]*], &:read)[/\Av?(\d+(?:\.\d+)*)/, 1] +version = "0.0.3" abort "Version must not reach 1" if version[/\d+/].to_i >= 1 Gem::Specification.new do |s| diff --git a/test/test_keyword.rb b/test/test_keyword.rb new file mode 100644 index 0000000..5fcecb5 --- /dev/null +++ b/test/test_keyword.rb @@ -0,0 +1,41 @@ +require 'test/unit' +LOADING_RUBY2_KEYWORDS = (RUBY_VERSION.scan(/\d+/).map(&:to_i) <=> [2, 7]) < 0 +if LOADING_RUBY2_KEYWORDS + require 'ruby2_keywords' +end + +class TestKeywordArguments < Test::Unit::TestCase + def test_loaded_features + list = $LOADED_FEATURES.grep(%r[/ruby2_keywords\.rb\z]) + if LOADING_RUBY2_KEYWORDS + assert_not_empty(list) + assert_not_include($LOADED_FEATURES, "ruby2_keywords.rb") + else + assert_empty(list) + assert_include($LOADED_FEATURES, "ruby2_keywords.rb") + end + end + + def test_module_ruby2_keywords + assert_send([Module, :private_method_defined?, :ruby2_keywords]) + assert_operator(Module.instance_method(:ruby2_keywords).arity, :<, 0) + end + + def test_toplevel_ruby2_keywords + main = TOPLEVEL_BINDING.receiver + assert_send([main, :respond_to?, :ruby2_keywords, true]) + assert_operator(main.method(:ruby2_keywords).arity, :<, 0) + end + + def test_proc_ruby2_keywords + assert_respond_to(Proc.new {}, :ruby2_keywords) + end + + def test_hash_ruby2_keywords_hash? + assert_false(Hash.ruby2_keywords_hash?({})) + end + + def test_hash_ruby2_keywords_hash + assert_equal({}, Hash.ruby2_keywords_hash({}.freeze)) + end +end