如何在Ruby

时间:2017-03-06 00:31:25

标签: ruby-on-rails ruby

在这个特殊的代码中,当输入[0] =“状态”时,我希望输出删除符号“”[]和,但我似乎无法找到解决方案,我尝试过使用每个但是我认为我所做的是错的,我是Ruby的新手,请帮助。谢谢!

class Main
  puts "Welcome to the automated Parking Lot"
  start = true
  parking_lot = []
  allocated_slot = 0
  slot_number = 0
  plate_number = ""
  color = ""
  x = ""
  while (start == true) do
    parking = Array.new
    input = gets.split(' ')
    case input[0]
    when input[0] = "create_parking_lot"
      parking_lot.clear
      allocated_slot = input[1]
      puts "Created a parking lot with #{allocated_slot} slots."
    when input[0] = "park"
      plate_number = input[1]
      color = input[2]
      if parking_lot.length < allocated_slot.to_i
        slot_number = slot_number + 1
          parking = [slot_number, input[1], input[2]]
          parking_lot.push(parking)
          parking_lot.sort!
          puts "Allocated slot number: #{slot_number}"
      else
        puts "Sorry, parking lot is full"
      end
    when input[0] = "leave"
        slot_number = input[1].to_i
        puts "Slot number #{slot_number} is now free."
        slot_number = slot_number - 1
        parking_lot.delete_at(slot_number)
        parking_lot[slot_number]
        #puts parking_lot
    when input[0] = "quit"
      break
    when input[0] = "status"
      if parking_lot.count == 0
        puts "No cars are parked."
      else
      puts "Slot No. Registration No. Color"
      parking.sort!
      i = 0
      y = ""
      while i < allocated_slot.to_i do
          x = parking_lot[i].to_s
          y = x.split(' ')
          puts "#{y[0]} #{y[1]} #{y[2]}" #
          #y.each { |a| print a, " " }
          i = i+1
        end
     end
    when input[0] = "registration_numbers_for_cars_with_color"
      puts "registration number for cars"
    when input[0] = "slot_numbers_for_cars_with_color"
      puts "slot number for cars with color"
    when input[0] = "slot_number_for_registration_number"
      puts "slot number for registration number"
    when input[0] = "check"
      puts parking_lot
    else
    puts "Command Error"
    end
  end

输出如下: [1,“HA-312424242”,“绿色”]

1 个答案:

答案 0 :(得分:0)

如果您输出的数组如下所示:

[1, "HA-312424242", "Green"]

然后你可以像这样调用join方法:

 [1, "HA-312424242", "Green"].join(' ')

它将产生:

"1 HA-312424242 Green"