Sign up ×
Stack Overflow is a community of 4.7 million programmers, just like you, helping each other. Join them; it only takes a minute:
    device_target = ["3001", "3002", "3003"]
    devices = ",kv1101="
    device_target.map {|d|
      case d
        when "30000" #desktop
          devices << ":9"
        when "30001" # smartphone
          devices << ":3:4:6:8"        
        when "30002" #tablet
          devices << ":2:5:7"
        when "30003" #feature_phone
          devices << ":1"
      end

My target is to get devices = "kv1101=3:4:6:8:2:5:7:1". But, how can I avoid this colon : from the first entry? The order doesn't matter.

share|improve this question
3  
Please provide valid input and output. The code can't produce "9:3:4:6:8:2". – Stefan Oct 6 '14 at 13:00
3  
I am sure it is just a typo but your "targets" are 3001,3002,3003 so 2 things one there is no 3000 and 2 your case statement will never work since d will never be in 30000,30001,30002,30003 – engineersmnky Oct 6 '14 at 13:09
    
If any of the answers were helpful to you, consider selecting the one you valued most. – Cary Swoveland Nov 7 '14 at 22:00

3 Answers 3

Store the values in an array and then use the join method:

devices = ",kv1101="
my_devices = []
device_target.map {|d|
  case d
    when "30000" #desktop
      my_devices << "9"
    when "30001" # smartphone
      my_devices += ["3","4","6","8"]
    when "30002" #tablet
      my_devices += ["2","5","7"]
    when "30003" #feature_phone
      my_devices << "1"
  end}
devices << my_devices.join(":")
share|improve this answer
1  
s/map/each/ :) – mudasobwa Oct 6 '14 at 13:03

Similar to Fer's answer, but using flat_map and its return value:

device_target = ['30001', '30002', '30003']
devices = ',kv1101='
devices << device_target.flat_map { |device|
             case device
             when '30000' then 9            # desktop
             when '30001' then [3, 4, 6, 8] # smartphone
             when '30002' then [2, 5, 7]    # tablet
             when '30003' then 1            # feature_phone
             end
           }.join(':')

devices #=> ",kv1101=3:4:6:8:2:5:7:1"

Or using a lookup table as suggested by tadman:

device_target = ['30001', '30002', '30003']
device_map = {
  '30000' => 9,            # desktop
  '30001' => [3, 4, 6, 8], # smartphone
  '30002' => [2, 5, 7],    # tablet
  '30003' => 1             # feature_phone
}

devices = ',kv1101='
devices << device_target.flat_map { |d| device_map[d] }.join(':')
share|improve this answer
1  
flat_map does a great job in this case. Could be even better if instead of a rigid case statement it was a lookup table using a Hash. – tadman Oct 8 '14 at 21:23
    
@tadman much better, indeed. – Stefan Oct 9 '14 at 7:14
    
A variant of #2: ",kv1101="+device_map.values_at(*device_target).flatten.join(?:). – Cary Swoveland Dec 20 '14 at 7:03

try this out:

"kv1101=:3:4:6:8:2:5:7:1".sub(/:/, "")
=> "kv1101=3:4:6:8:2:5:7:1"
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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