我从外部api端点获取以下数组。
输入:-
1. [["date", "country_name", "month"], ["2019-02-21", "US", "Jan"]]
2. ["name", "homeAddress", "zipcode"]
预期输出:-
1. [["Date", "Country Name", "Month"], ["2019-02-21", "US", "Jan"]]
2. ["Name", "Home Address", "Zipcode"]
如何在Ruby on Rails中高效地更改每个数组?
更新: 某些名称与预期的有所不同
输入:
["column1", "column2", "date"]
预期输出:
["column3", "column4", "Date"]
如何获得以上输出?
答案:-
输入:-
a=['1', '2', '3', '4']
b= {"1"=>"10", "2"=>"20", "3"=>"30"}
执行:
c=a.map{|i| b[i].nil?? i : b[i] }
输出:-
["10", "20", "30", "4"]
答案 0 :(得分:1)
您想用空格替换'_'或在字符串中遇到大写字母时带空格
尝试按照以下Rails方法进行操作
"now_isTheTime".titleize.camelize
=> "Now Is The Time"
答案 1 :(得分:0)
ar1 = [["date", "country_name", "month"], ["2019-02-21", "US", "Jan"]]
ar2 = ["name", "homeAddress", "zipcode"]
def formatter(string)
return string if string.length < 3 || string.count("0-9").positive?
string.titleize.camelize
end
ar1.map{ |sub_arr| sub_arr.map(&method(:formatter)) }
ar2.map(&method(:formatter))