0

I am getting the following arrays from external api endpoint.

Input:-

1. [["date", "country_name", "month"], ["2019-02-21", "US", "Jan"]]
2. ["name", "homeAddress", "zipcode"]

Expected Output:-

1. [["Date", "Country Name", "Month"], ["2019-02-21", "US", "Jan"]]
2. ["Name", "Home Address", "Zipcode"]

How can I change the each array in an efficient way in Ruby on Rails?

Update: Some of the name are different in expected as follows

Input:

["column1", "column2", "date"]

Expected output:

["column3", "column4", "Date"]

How can I get the above output?

Answer:-

Inputs:-

a=['1', '2', '3', '4']

b= {"1"=>"10", "2"=>"20", "3"=>"30"}

Execute:

c=a.map{|i| b[i].nil?? i : b[i] }

Output:-

 ["10", "20", "30", "4"] 

2 Answers 2

1

You want to replace '_' with space or bring space when capital letter is encountered within string,

Try following rails methods to do so,

"now_isTheTime".titleize.camelize
 => "Now Is The Time" 
Sign up to request clarification or add additional context in comments.

Comments

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))

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.