开发者社区> 问答> 正文

如何将多个参数作为数组传递给ruby方法?

我在Rails助手文件中有这样的方法

def table_for(collection, *args) options = args.extract_options! ... end 我希望能够像这样调用此方法

args = [:name, :description, :start_date, :end_date] table_for(@things, args) 这样我就可以基于表单提交动态传递参数。我无法重写该方法,因为我在很多地方都使用过该方法,我还能怎么做? 问题来源于stack overflow

展开
收起
保持可爱mmm 2020-02-09 12:13:05 651 0
1 条回答
写回答
取消 提交回答
  • Ruby很好地处理了多个参数。

    这是一个很好的例子。

    def table_for(collection, *args) p collection: collection, args: args end

    table_for("one") #=> {:collection=>"one", :args=>[]}

    table_for("one", "two") #=> {:collection=>"one", :args=>["two"]}

    table_for "one", "two", "three" #=> {:collection=>"one", :args=>["two", "three"]}

    table_for("one", "two", "three") #=> {:collection=>"one", :args=>["two", "three"]}

    table_for("one", ["two", "three"]) #=> {:collection=>"one", :args=>[["two", "three"]]}

    2020-02-09 12:13:17
    赞同 展开评论 打赏
问答分类:
问答地址:
问答排行榜
最热
最新

相关电子书

更多
低代码开发师(初级)实战教程 立即下载
冬季实战营第三期:MySQL数据库进阶实战 立即下载
阿里巴巴DevOps 最佳实践手册 立即下载