Ruby 语法通过 send 接受动态生成的方法上的参数

发布于 2024-12-06 04:56:03 字数 281 浏览 3 评论 0原文

如何通过 send 在动态生成的方法上传递参数?

下面我动态生成几个方法,我希望它们接受“行”参数。

下面的代码生成方法,但我不知道如何让这些方法接受“行”参数。

@sql_columns.each do |attr|
  (class << self; self end).send :define_method, attr do
    key = @column_mapping[attr].to_s
    row[key]
  end
end

How I can pass arguments on dynamically generated methods via send ?

Below I generate on the fly several methods, I want they accept the "row" argument.

The code below generates the methods, but I don't know how let the methods accept the "row" argument.

@sql_columns.each do |attr|
  (class << self; self end).send :define_method, attr do
    key = @column_mapping[attr].to_s
    row[key]
  end
end

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

就像说晚安 2024-12-13 04:56:03
(class << self; self end).send :define_method, attr do |row|
(class << self; self end).send :define_method, attr do |row|
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文