-1

How do I format str:

str = "2013-12-03 10:19:06 +0100: Success: 34 => ["reset/htx", "auth/mx", "auth/htx", "product", "product/id", "product/id/review", "search/offer", "search/ac", "location", "location/zone", "shop", "shop/id", "shop/id/category", "shop/id/contact", "shop/id/terms", "shop/id/return", "shop/id/oid/fragment", "retailer", "retailer/id", "user/id/account", "user/id/review", "user/id/review/id", "user/id/product", "user/id/product/id", "user/id/tag", "category", "category/urlName", "about", "about/terms", "about/privacy", "about/order", "about/order/state", "about/tp", "about/tp/abbrev"]"

To get this output:

2013-12-03 10:19:06 +0100: Success: 25 => ["reset/htx", "auth/mx", "auth/htx", "product", "product/id", "product/id/review", 
                                           "search/offer", "search/ac", "location", "location/zone", "shop", "shop/id", 
                                           "shop/id/category", "shop/id/contact", "shop/id/terms", "shop/id/return", 
                                           "shop/id/oid/fragment", "retailer", "retailer/id", "user/id/account", 
                                           "user/id/review", "user/id/review/id", "user/id/product", "user/id/product/id"]

At first I just tried to add break lines but with

x.scan(/.{100}/).join("\n")

break lines are added between words. I also tried using

str.split("").each_slice

but it also didn't work.

1
  • Why does "Success: 34" change to "Success: 25"? Is it important to the question that you provide such a long sample input string? Couldn't you reduce it to something much shorter and still ask the same question? "How to Ask". Nov 9, 2015 at 17:11

1 Answer 1

1

If I understand your problem correctly, there's probably not a simple solution for this. Here's some code you could work with.

str = %q{2013-12-03 10:19:06 +0100: Success: 34 => ["reset/htx", "auth/mx", "auth/htx", "product", "product/id", "product/id/review", "search/offer", "search/ac", "location", "location/zone", "shop", "shop/id", "shop/id/category", "shop/id/contact", "shop/id/terms", "shop/id/return", "shop/id/oid/fragment", "retailer", "retailer/id", "user/id/account", "user/id/review", "user/id/review/id", "user/id/product", "user/id/product/id", "user/id/tag", "category", "category/urlName", "about", "about/terms", "about/privacy", "about/order", "about/order/state", "about/tp", "about/tp/abbrev"]}

max_length = 100
timestamp, list = str.split(' => [')
result = ''
line = timestamp += ' => ['
list.split(' ').each do |item|
  if line.length + item.length > max_length
    result += line + "\n"
    line = ' ' * timestamp.length
  end
  line += item
end
result += line

puts result

Which returns:

2013-12-03 10:19:06 +0100: Success: 34 => ["reset/htx","auth/mx","auth/htx","product","product/id",
                                           "product/id/review","search/offer","search/ac",
                                           "location","location/zone","shop","shop/id",
                                           "shop/id/category","shop/id/contact","shop/id/terms",
                                           "shop/id/return","shop/id/oid/fragment","retailer",
                                           "retailer/id","user/id/account","user/id/review",
                                           "user/id/review/id","user/id/product",
                                           "user/id/product/id","user/id/tag","category",
                                           "category/urlName","about","about/terms","about/privacy",
                                           "about/order","about/order/state","about/tp",
                                           "about/tp/abbrev"]

Please note, however, that you would run into trouble if any single list item were too long for max_length. In the above case, the indent is 43 characters and the max_length 100. A single list item longer than 57 charactes would though have to be broken and would cause the above code to produce overflow lines.

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.