ruby-on-rails - 将数组元素减少为嵌套类实例化
问题描述
鉴于我有以下数组:
operations = [
[
:do_this,
["a"]
],
[
:do_that,
["b", "c"]
],
[
:then_this,
["b"]
]
]
我如何在上面进行转换,使其看起来像:
DoThisOperation.new(DoThatOperation.new(ThenThisOperation.new('b'), 'b' , 'c'), 'a')
这是据我所知:
require 'active_support/inflector'
class DoThisOperation
def initialize(successor = nil, a)
end
end
class DoThatOperation
def initialize(successor = nil, b, c)
end
end
class ThenThisOperation
def initialize(successor = nil, b)
end
end
operations = [
[
:do_this,
["a"]
],x
[
:do_that,
["b", "c"]
],
[
:then_this,
["b"]
]
]
operations.reverse.reduce do |result, element|
klass_name = element[0].to_s.camelize
args = element[1]
"#{klass_name}Operation".constantize.new(result, *args)
end
减少/注入是解决这个问题的正确方法吗?如果是这样,我应该在上面做什么?
解决方案
减少/注入是解决这个问题的正确方法吗?
是的,但您需要将初始值传递给reduce
,例如nil
。否则,第一个元素(在您的情况下为最后一个元素)将用作初始值而不进行转换。
这会起作用:
operations.reverse.reduce(nil) do |result, element|
klass_name = element[0].to_s.camelize
args = element[1]
"#{klass_name}Operation".constantize.new(result, *args)
end
您可以使用数组分解进一步简化它:
operations.reverse.reduce(nil) do |result, (name, args)|
klass_name = name.to_s.camelize
"#{klass_name}Operation".constantize.new(result, *args)
end
甚至:
operations.reverse.reduce(nil) do |result, (name, args)|
"#{name}_operation".camelize.constantize.new(result, *args)
end
推荐阅读
- google-apps-script - 谷歌表格的谷歌应用脚本中的总和
- r - 每日时间序列的功率谱
- windows - 从 psql.exe 执行 SQL 文件后列中的字符错误
- kotlin - Gradle 从 4 升级到 7,模块之间共享测试
- python - 如何从 numpy 数组中删除字符串 dtype?
- php - 使用 use() 的数组过滤器回调引用
- r - survfit 和 ggsurvplot 中的审查生存
- reactjs - 好的还是坏的做法?取自事件对象的表单值
- python - 将 Flask webapp 中的数据保存到数据库中
- c# - Xunit 集成测试访问 localdb 导致间歇性问题