本人是 ruby 新手 最近在学习实践 ruby 的优雅性;下面的代码是实现了我的功能,但是我在想是否有其他方式呢? 代码如下
unless city['MonPointList'].nil?
city['MonPointList'].each_with_index do |monitor,index|
a =[]
a << monitor['MonPointName']
a << monitor['Real_CO']
a << monitor['Real_NO2']
a << monitor['Real_O3']
a << monitor['Real_PM10']
a << monitor['Real_SO2']
a << monitor['Real_PM2_5']
a << monitor['Real_AQI']
a.each_with_index do |single,index|
a[index] = "-" if single.empty?
end
puts index.to_s + "|" + a.join("|")
end
end
问题 1 如何改写上面的代码,我的要求是,当 monitor['Real_O3']="" 时 让 monitor['Real_O3']=“-”
事实上 monitor['Real_O3'] 就是“”所以 a << monitor['Real_O3'] || "-"
肯定是不行了,so how?
问题 2 假如上面的方式是优雅的,那么 如何更优雅的处理 array 的 element ;
a.each_with_index do |single,index|
a[index] = "-" if single.empty?
end