Ruby中如何获取构造函数的参数名称?

更新于
2026-10-09 21:04:27
1阅读来源:SEO资源
  • 内容介绍
  • 文章标签
  • 相关推荐

本文共计184个文字,预计阅读时间需要1分钟。

Ruby中如何获取构造函数的参数名称?

我想要检索构造函数中的参数名称。以下是对应的代码:

rubyclass Something def self.do_something(a, b) end

def initialize(a, b) endend

Ruby中如何获取构造函数的参数名称?

Something.method(:do_something).parameters

我想检索构造函数中的参数名称.给出以下代码:

class Something def self.do_something(a, b); end def initialize(a, b); end end

我明白了:

Something.method(:do_something).parameters # => [[:req, :a], [:req, :b]] Something.method(:new).parameters # => [[:rest]]

为什么Method#参数在new的情况下返回[[:rest]],如何获取构造函数参数?

如果您询问Something :: new的参数,您将获得Something :: new的参数.如果你想知道Something #initialize的参数,你需要要求Something #initialize:

Something.instance_method(:initialize).parameters # => [[:req, :a], [:req, :b]]

本文共计184个文字,预计阅读时间需要1分钟。

Ruby中如何获取构造函数的参数名称?

我想要检索构造函数中的参数名称。以下是对应的代码:

rubyclass Something def self.do_something(a, b) end

def initialize(a, b) endend

Ruby中如何获取构造函数的参数名称?

Something.method(:do_something).parameters

我想检索构造函数中的参数名称.给出以下代码:

class Something def self.do_something(a, b); end def initialize(a, b); end end

我明白了:

Something.method(:do_something).parameters # => [[:req, :a], [:req, :b]] Something.method(:new).parameters # => [[:rest]]

为什么Method#参数在new的情况下返回[[:rest]],如何获取构造函数参数?

如果您询问Something :: new的参数,您将获得Something :: new的参数.如果你想知道Something #initialize的参数,你需要要求Something #initialize:

Something.instance_method(:initialize).parameters # => [[:req, :a], [:req, :b]]