转换”对象。属性“在Ruby中”对象[属性]。

时间:2021-11-22 21:44:17

Say I've got strings like "object.property.property" and I'd like to be able to use those strings to dynamically create form fields for those properties; I'd need to convert them to "object[property][property]".

比如我有像object。property这样的字符串。属性,我希望能够使用这些字符串动态地为这些属性创建表单字段;我需要将它们转换为“object[property][property]”。

I was trying something along the lines of: "object.property.property".split('.')

我尝试了一些类似于:object。property。property。property。split('.)

Which of course leaves me with an array: ["object", "property", "property"]

当然,我还有一个数组:["object", "property", "property"]

But I'm unsure how to rejoin them so the properties are surrounded by brackets ("object[property][property]"). I tried using join, but as far as I can tell that only lets you specify the seperator between each element in the array.

但我不确定如何重新联接它们,使属性被括号括起来(“object[property][property] [property]])。我尝试过使用join,但是就我所知,只能在数组中的每个元素之间指定分配器。

Any thoughts appreciated.

任何想法赞赏。

4 个解决方案

#1


3  

This is an option

这是一个选择

result, *sub_str = "object.property.property".split('.')
result += sub_str.map{|property| "[#{property}]"}.join
#=> "object[property][property]"

#2


2  

"object.property.property".gsub('.', '][').sub('][', '[') << ']'

or, less productive, but more readable:

或者,效率降低,但可读性更强:

arr = "object.property.property".split('.')
[arr.shift, *arr.map { |e| "[#{e}]" }].join

#3


2  

Ah, the beauty of ruby:

啊,红宝石的美丽:

obj, *props = "object.property.property".split('.')
obj   #=> "object"
props #=> ["property", "property"]

"#{obj}[#{props.join('][')}]"
#=> "object[property][property]"

Update: to also cover objects without properties:

更新:也包括没有属性的对象:

def form_name(property)
  obj, *props = property.split('.')
  "#{obj}[#{props.join('][')}]"
  props.any? ? "#{obj}[#{props.join('][')}]" : obj
end

form_name "object.property.property"
#=> "object[property][property]"
form_name "object"
#=> "object"

#4


0  

I would suggest this:

我建议:

"object.property.property".gsub(/\.([\w\d_]+[^\.])/, "[#{$1}]")

Though this is only readable to someone who understands Regular Expressions well.

虽然这只对理解正则表达式的人来说是可读的。

#1


3  

This is an option

这是一个选择

result, *sub_str = "object.property.property".split('.')
result += sub_str.map{|property| "[#{property}]"}.join
#=> "object[property][property]"

#2


2  

"object.property.property".gsub('.', '][').sub('][', '[') << ']'

or, less productive, but more readable:

或者,效率降低,但可读性更强:

arr = "object.property.property".split('.')
[arr.shift, *arr.map { |e| "[#{e}]" }].join

#3


2  

Ah, the beauty of ruby:

啊,红宝石的美丽:

obj, *props = "object.property.property".split('.')
obj   #=> "object"
props #=> ["property", "property"]

"#{obj}[#{props.join('][')}]"
#=> "object[property][property]"

Update: to also cover objects without properties:

更新:也包括没有属性的对象:

def form_name(property)
  obj, *props = property.split('.')
  "#{obj}[#{props.join('][')}]"
  props.any? ? "#{obj}[#{props.join('][')}]" : obj
end

form_name "object.property.property"
#=> "object[property][property]"
form_name "object"
#=> "object"

#4


0  

I would suggest this:

我建议:

"object.property.property".gsub(/\.([\w\d_]+[^\.])/, "[#{$1}]")

Though this is only readable to someone who understands Regular Expressions well.

虽然这只对理解正则表达式的人来说是可读的。