Zeeshan Shafqat
Zeeshan Shafqat

Reputation: 55

How to convert an array into a string with two different separators at two different places in ruby?

I want to convert an array in to a string with two different separators at two different places. meaning:

array = [1,2,3,4]
after converting: separator 1: (":") separator 2: ("and")
string = "1:2:3: and 4"
OR
string = "1:2 and 3:4"

How can I build dynamic and short code that lets me convert an array (of any length) into a string which allows me to insert more than one separator and at different positions.

My current solution is messy and hideous: I have used #join with a single argument.

def oxford_comma(array)
 if array.length == 1
  result_at_1 = array.join
  return result_at_1
 elsif array.length == 2
  result_at_2 = array.join(" and ")
  return result_at_2
 elsif array.length == 3
  last = array.pop
  result = array.join(", ")
  last = ", and " + last
  result = result + last
 elsif array.length > 3
  last = array.pop
  result = array.join(", ")
  last = ", and " + last
  result = result + last
  return result
 end
end

Can someone please help me establish a better, shorter and more abstract method of doing this?

Upvotes: 3

Views: 493

Answers (4)

cavin kwon
cavin kwon

Reputation: 501

You can use Enumerable#slice_after.

array.slice_after(1).map { |e| e.join ":" }.join(" and ") #=> "1 and 2:3:4"
array.slice_after(2).map { |e| e.join ":" }.join(" and ") #=> "1:2 and 3:4"
array.slice_after(3).map { |e| e.join ":" }.join(" and ") #=> "1:2:3 and 4"

Upvotes: 6

Cary Swoveland
Cary Swoveland

Reputation: 110755

Code

def convert(arr, special_separators, default_separator, anchors={ :start=>'', :end=>'' })
  seps = (0..arr.size-2).map { |i| special_separators[i] || default_separator }
  [anchors.fetch(:start, ""), *[arr.first, *seps.zip(arr.drop(1)).map(&:join)],
    anchors.fetch(:end, "")].join
end

Examples

arr = [1,2,3,4,5,6,7,8]
default_separator  = ':'

#1

special_separators = { 1=>" and ", 3=>" or " }
convert(arr, special_separators, default_separator)
  #=> "1:2 and 3:4 or 5:6:7:8"

where

seps #=> [":", " and ", ":", " or ", ":", ":", ":"]

#2

special_separators = { 1=>" and ", 3=>") or (", 5=>" and " }    
convert(arr, special_separators, default_separator, { start: "(", end: ")" })
  #=> "(1:2 and 3:4) or (5:6 and 7:8)"

where

seps #=> [":", " and ", ":", ") or (", ":", " and ", ":"]

Upvotes: 0

Sergio Tulentsev
Sergio Tulentsev

Reputation: 230531

If you're using rails/activesupport, then it's built-in:

[1,2,3,4].to_sentence # => "1, 2, 3, and 4"
[1,2].to_sentence # => "1 and 2"
[1,2,3,4].to_sentence(last_word_connector: ' and also ') # => "1, 2, 3 and also 4"

If you don't, go copy the activesupport's implementation, for example :)

Note: this does not allow you to place your "and" in the middle of the sequence. Perfect for oxford commas, though.

Upvotes: 4

Aleksei Matiushkin
Aleksei Matiushkin

Reputation: 121010

pos = 2
[array[0...pos], array[pos..-1]].
  map { |e| e.join ':' }.
  join(' and ')
#⇒ "1:2 and 3:4"

Upvotes: 3

Related Questions