Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ruby add values to string separated by comma except the first element

    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.

like image 446
underScorePassionFruit Avatar asked Dec 02 '25 21:12

underScorePassionFruit


1 Answers

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(":")
like image 95
Fer Avatar answered Dec 04 '25 12:12

Fer