如何在 ruby on rails 中将字符串对象转换为数组?
how to transform a string object into an array in ruby on rails?
如何将字符串对象转换为数组?
在我的代码中,我可以接收一个数组。
示例:
Eq = ["5f17283521ac387fc8eeca6a", "5f17283c21ac387fc8eecb78", "5f17285621ac387fc8eed109", "5f17286321ac387fc8eed3ca", "5f17286621ac387fc8eed433", "5f17286d21ac387fc8eed5c8", "5f17287221ac387fc8eed6c7", "5f17288621ac387fc8eeda69", "5f17288921ac387fc8eedb1d", "5f17289421ac387fc8eedd39", "5f17289521ac387fc8eedd93", "5f17289821ac387fc8eeddfc", "5f17296e21ac387fc8ef0a57"]
而且我也可以简单地收到一个标识字符串。
示例:
Eq = "5f17296e21ac387fc8ef0a57"
如何将这个字符串转换成数组?
consulta_OS.each do |os|
if os.equipment.map(&:to_s) != []
eq_id = os.equipment_ids.map(&:to_s) #equipment_ids receive in array else
eq_id = os.equipment_id.to_s #equipment_id in string
end
end
你可以这样做:
EQ = ["5f17296e21ac387fc8ef0a57"]
或者您的情况:
EQ = [os.equipment_id.to_s]
如何将字符串对象转换为数组?
在我的代码中,我可以接收一个数组。
示例:
Eq = ["5f17283521ac387fc8eeca6a", "5f17283c21ac387fc8eecb78", "5f17285621ac387fc8eed109", "5f17286321ac387fc8eed3ca", "5f17286621ac387fc8eed433", "5f17286d21ac387fc8eed5c8", "5f17287221ac387fc8eed6c7", "5f17288621ac387fc8eeda69", "5f17288921ac387fc8eedb1d", "5f17289421ac387fc8eedd39", "5f17289521ac387fc8eedd93", "5f17289821ac387fc8eeddfc", "5f17296e21ac387fc8ef0a57"]
而且我也可以简单地收到一个标识字符串。
示例:
Eq = "5f17296e21ac387fc8ef0a57"
如何将这个字符串转换成数组?
consulta_OS.each do |os|
if os.equipment.map(&:to_s) != []
eq_id = os.equipment_ids.map(&:to_s) #equipment_ids receive in array else
eq_id = os.equipment_id.to_s #equipment_id in string
end
end
你可以这样做:
EQ = ["5f17296e21ac387fc8ef0a57"]
或者您的情况:
EQ = [os.equipment_id.to_s]