Supersonic
Supersonic

Reputation: 430

Ruby: Array to string with conditions

I need to generate an array checking some conditions. Here is the scenario.

I have 3 variables containing strings like this:

client1 = "Google"
client2 = "Apple"
client3 = "Microsoft"

category1 = "sales"
category2 = "software"
category3 = "hardware"

The output i need to generate is an array which should have all the clients and the categories appended together by an underscore "_".

Desired Output: array = ["Google_sales", "Apple_software", "Microsoft_hardware"]

What I have tried so far:

array = [client1+"_"+category1, client2+"_"+category2, client3+"_"+category3]

Now, this works fine and I get what I want. But the complexity starts when a variable is empty. Consider there is another variable called client4="" and category4="". Now these are empty and when i try to complete my array i get messy array values.

Ex: array = [client1+"_"+category4, client4+"_"+category2]

This would give me an output like this: array = ["Google_", "_software"]

Question:

The user fills in the clients and the category of the clients. There might be a chance where the user failed to enter client or a category. Currently I have client1,client2,client3,client4 and cat1,cat2,cat3,cat4. Client1 is associated with cat1 and so on. Now I need to get an array only with the valid entries and if one of them is empty then we skip to the next one.

So we insert "_" in between client1 and cat1 only if both are present. Else we move to client2 and cat2 and so on.

Upvotes: 0

Views: 212

Answers (3)

Todd A. Jacobs
Todd A. Jacobs

Reputation: 84353

Use Array#zip and Array#join

The easiest way to do this would be to use Array#zip and Array#join. For example:

companies  = %w[Google Apple Microsoft]
categories = %w[sales software hardware]
companies.zip(categories).map { |array_pair| array_pair.join '_' }
# => ["Google_sales", "Apple_software", "Microsoft_hardware"]

You can make this more complicated if you want, but as long as you have the same number of items in each array, this is about as simple as it gets.

Upvotes: 2

sawa
sawa

Reputation: 168101

array = [
  [client1, category1], [client2, category2], [client3, category3],
  [client1, category4], [client4, category2],
]
.map{|a| a.join("_") unless a.any?(&:empty)}.compact

Upvotes: 1

Abraham P
Abraham P

Reputation: 15471

I'm going to make a few assumptions here:

1) Your method takes as input two arrays: an array of cliens and an array of categories .

2) If the user neglects to enter a value, an empty string is saved in its place.

3) As outlined in your comment, your desired final output is a commaseparated string.

def collect_clients_and_customers(clients, categories)
   returnable = ""
   if clients.size != categories.size
      puts "O MY GOD! SOMETHING HAS GONE TERRIBLY WRONG"
      nil
   else
      clients.each_with_index do |client, index|
          unless client[index].empty? or categories[index].empty?
               returnable = returnable+client[index]+"_"+categories[index]+","
          end
          if index == clients.size-1
               returnable = returnable[0..index-1]
          end
      end
   end
   returnable
 end

Upvotes: 1

Related Questions