简体   繁体   English

Ruby阵列打印

[英]Ruby array printing

I have an array of objects ( nokogiri xml nodes ) and each node is having name and value . 我有一个对象数组( nokogiri xml节点 ),每个节点都有namevalue I want to print them in the format name=value without iterating in for loop. 我想以name=value的格式打印它们,而不是迭代for循环。

if I use arr * "," I'm able to see just the values as below 如果我使用arr *“,”我只能看到如下的值

4900.00,5.00,0.00,-100.00,100.00
6085.00,5.00,1285.00,1185.00,100.00
6015.00,5.00,30.00,-70.00,100.00
5915.00,5.00,0.00,-100.00,100.00
5815.00,5.00,0.00,-100.00,100.00

Is there something that can be done here. 有没有可以在这里完成的事情。

Something like this should work: 这样的事情应该有效:

array.each do |n|
  puts "#{n.name}=#{n.value}"
end

Assuming that your Nokogiri nodes have name and value attributes (not child elements), here's one way: 假设您的Nokogiri节点具有namevalue属性(不是子元素),这里有一种方法:

# Convert the array into a new array of strings
namevals = my_array.map{ |node| "#{node['name']}=#{node['value']}" }

# See it with space delimiters
puts namevals.join(' ')

Possibly useful alternative: 可能有用的替代方案:

# Create a hash mapping unique names to values
namevals = Hash[ my_array.map{ |node| [ node['name'], node['value'] } ]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM