Ruby Hash of arrays iteration

ruby

#1

I'm new to Ruby and i have a problem.

I make a RestClient.get on an url and i make a JSON parse on it.

response= RestClient.get 'www.example.com'
response=JSON.parse(response)

response looks like this:
{"tag"=>["one", "two", "three"], "name"=>"example"}

and i would like to have an array like this::
[example-one,example-two,...]

I can print all tags or names but not together, if someone can help me :slight_smile:


#2

So, assuming you have your response in variable response, you can do this:

response = {"tag"=>["one", "two", "three"], "name"=>"example"}
 
def get_formatted_response(response)
    name = response["name"]
    tags = response["tag"]
    new_list = []
 
    tags.each do |tag|
        new_list << name + "-" + tag
    end
 
    return new_list
end
 
print get_formatted_response(response)

Run it here

Hope it helps! :smiley: