diff options
author | Kyrylo Silin <silin@kyrylo.org> | 2019-04-06 14:01:03 +0300 |
---|---|---|
committer | Kyrylo Silin <silin@kyrylo.org> | 2019-04-08 00:49:39 +0300 |
commit | 26f665eaf294f1c96db336749cca362562ef88a6 (patch) | |
tree | d4eb18d8e0aa1275514d569eb91b71ca503a26c7 | |
parent | a4c3161f498b01b231283e1c78600561921e5892 (diff) | |
download | pry-26f665eaf294f1c96db336749cca362562ef88a6.tar.gz |
command: rename #correct_arg_arity to #normalize_method_args
We also refactor it to use `case` instead of `if`.
-rw-r--r-- | lib/pry/block_command.rb | 2 | ||||
-rw-r--r-- | lib/pry/class_command.rb | 2 | ||||
-rw-r--r-- | lib/pry/command.rb | 20 |
3 files changed, 13 insertions, 11 deletions
diff --git a/lib/pry/block_command.rb b/lib/pry/block_command.rb index 756984e8..947f8e7d 100644 --- a/lib/pry/block_command.rb +++ b/lib/pry/block_command.rb @@ -10,7 +10,7 @@ class Pry # @param [Array<String>] args The arguments passed # @return [Object] The return value of the block def call(*args) - instance_exec(*correct_arg_arity(block.arity, args), &block) + instance_exec(*normalize_method_args(block, args), &block) end def help diff --git a/lib/pry/class_command.rb b/lib/pry/class_command.rb index 62f562b5..dde4781a 100644 --- a/lib/pry/class_command.rb +++ b/lib/pry/class_command.rb @@ -77,7 +77,7 @@ class Pry output.puts slop.help void else - process(*correct_arg_arity(method(:process).arity, args)) + process(*normalize_method_args(method(:process), args)) end end diff --git a/lib/pry/command.rb b/lib/pry/command.rb index 8cf83ac4..78206889 100644 --- a/lib/pry/command.rb +++ b/lib/pry/command.rb @@ -500,17 +500,19 @@ class Pry ret end - # Fix the number of arguments we pass to a block to avoid arity warnings. - # @param [Fixnum] arity The arity of the block - # @param [Array] args The arguments to pass - # @return [Array] A (possibly shorter) array of the arguments to pass - def correct_arg_arity(arity, args) - if arity < 0 + # Normalize method arguments according to its arity. + # + # @param [Integer] method + # @param [Array] args + # @return [Array] a (possibly shorter) array of the arguments to pass + def normalize_method_args(method, args) + case method.arity + when -1 args - elsif arity == 0 + when 0 [] - elsif arity > 0 - args.values_at(*(0..(arity - 1)).to_a) + else + args.values_at(*(0..(method.arity - 1)).to_a) end end end |