Tbpgr Blog

Employee Experience Engineer tbpgr(てぃーびー) のブログ

Ruby | Proc | arity

概要

Proc#arity

詳細

Proc オブジェクトが受け付ける引数の数を返却。
可変長の場合は必要とされる引数の数 + 1 を負の値にして返却。

サンプルコード
require 'tbpgr_utils'

bulk_puts_eval binding, <<-EOS
Proc.new { || }.arity
Proc.new { |x|x }.arity
Proc.new { |*x|x }.arity
Proc.new { |x, y|[x, y] }.arity
Proc.new { |x, *y|[x, y] }.arity
EOS

__END__
下記はTbpgrUtils gemの機能
bulk_puts_eval

https://rubygems.org/gems/tbpgr_utils
https://github.com/tbpgr/tbpgr_utils

出力

Proc.new { || }.arity            # => 0
Proc.new { |x|x }.arity          # => 1
Proc.new { |*x|x }.arity         # => -1
Proc.new { |x, y|[x, y] }.arity  # => 2
Proc.new { |x, *y|[x, y] }.arity # => -2