为什么 Ruby 在 String 对象上调用参数方法时会输出 [[:rest]] ?

vis*_*nth 1 ruby parameters jruby irb

我在 jruby irb 上玩,遇到了这种现象,其中参数方法[[:rest]]在字符串方法上调用时返回。这不仅是字符串的情况,而且我将举一个关于字符串的例子。

irb(main):042:0> String.new.methods-Object.methods
[:valid_encoding?, :casecmp, :to_java_bytes, :squeeze!,
 :is_utf8?, :slice, :hex, :[]=, :initialize_copy, :empty?,
 :oct, :rindex, :unseeded_hash, :%, :rjust, :chop, :index,
 :gsub!, :chomp!, :*, :+, :concat, :capitalize, :singularize,
 :titlecase, :each_line, :size, :deconstantize, :downcase!,
 :capitalize!, :to_sym, :humanize, :setbyte, :force_encoding,
 :sub, :reverse!, :swapcase, :scan, :gsub, :sum, :partition,
 :to_str, :codepoints, :swapcase!, :byteslice, :end_with?,
 :upto, :tr!, :[], :intern, :parameterize, :tableize, :chomp,
 :pluralize, :reverse, :mb_chars, :succ, :underscore, :titleize,
 :start_with?, :ljust, :tr, :chars, :chop!, :encode, :<<,
 :lstrip!, :dasherize, :prepend, :replace, :strip, :split,
 :to_i, :succ!, :to_f, :to_r, :rstrip!, :count, :to_c, :chr,
 :encoding, :camelcase, :slice!, :next!, :upcase, :bytesize,
 :demodulize, :delete!, :each_byte, :next, :classify, :squeeze,
 :downcase, :tr_s, :constantize, :crypt, :each_codepoint,
 :foreign_key, :insert, :delete, :camelize, :upcase!, :getbyte,
 :rpartition, :sub!, :unpack, :dump, :lines, :strip!, :ord,
 :safe_constantize, :center, :each_char, :match, :clear,
 :length, :to_java_string, :rstrip, :bytes, :ascii_only?,
 :tr_s!, :encode!, :lstrip, :to_json_raw, :to_json_raw_object,
 :included, :between?]
Run Code Online (Sandbox Code Playgroud)

如您所见,上面的列表显示了 String 类中的方法,当在上述任何方法上调用参数方法时,它会给出输出 # => [[:rest]]

irb(main):043:0> String.new.method(:each_line).parameters
[[:rest]]
irb(main):044:0> String.new.method(:slice).parameters
[[:rest]]
Run Code Online (Sandbox Code Playgroud)

另一个名为 .gem 的示例Sidekiq。在这种情况下,它显示方法类名称Statsrest.

irb(main):049:0> Sidekiq::Stats.new.methods-Object.methods
[:scheduled_size, :enqueued, :failed, :processed, :default_queue_latency, :retry_size, :queues, :processes_size, :reset, :workers_size, :dead_size, :fetch_stats!]
irb(main):050:0> Sidekiq::Stats.new.method(:reset).parameters
[[:rest, :stats]]
Run Code Online (Sandbox Code Playgroud)

有人可以解释一下rest在这种情况下指的是什么吗?

Ama*_*dan 5

parameters将为您提供一组对,其中第一个元素是一种参数类型,第二个元素是将接受它的变量的名称。大多数参数都有 type :req,一个必需的变量。可选参数,具有默认值的参数,类型为:opt。用 splat 定义的参数具有类型:rest:这些参数将包含超出必需和可选参数的任何其他参数。还有 new :keyrest,它是 double-splat 参数。

def foo(a, b=3, *c, **d); end
method(:foo).parameters
# => [[:req, :a], [:opt, :b], [:rest, :c], [:keyrest, :d]] 
Run Code Online (Sandbox Code Playgroud)

如果 rest 参数未命名,您只需获取:rest值而不是一对:

def bar(a, b=3, *); end
method(:foo).parameters
# => [[:req, :a], [:opt, :b], [:rest]]
Run Code Online (Sandbox Code Playgroud)

许多具有本机实现的 Ruby 方法没有定义精确的签名;底层 C 代码正在检查参数列表以确定调用了方法的哪个用例。因此,而在 Ruby 中String#each_line通常是这样制作的:

class String
  def each_line(separator=$/)
    #...
  end
end
Run Code Online (Sandbox Code Playgroud)

这会给它签名[[:opt, :separator]],它实际上被定义为好像它是这样写的

class String
  def each_line(*)
    #...
  end
end
Run Code Online (Sandbox Code Playgroud)