如何在Ruby中的数组中分离两个数组?

时间:2016-01-21 22:15:40

标签: arrays ruby

我想从regplates中将两个数组distancespeeders分开并将它们彼此相邻输出:

require 'date'
# Variables and Constants.
speeders = Array.new
DISTANCE = 10
LIMIT = 70
MINTIME = DISTANCE/LIMIT

# Recieving input of regplate + Checking if they are speeding.
def calculations(speeders)
  puts "Please input your registration plate:"
  puts "Type 'stop' to stop."
  loop do
    regplate = gets.chomp
    break if regplate == 'stop'
    now = Time.now
    time1 = DateTime.strptime("13:22:45","%H:%M:%S")
    time2 = time1 + (rand(10) / ( 24.0 * 60))
    elapsed_time = time2 - time1 
    if elapsed_time >= MINTIME 
      speeders << [regplate, DISTANCE/elapsed_time.to_f]   
    end
  end
return speeders
end

# Outputting the speeders with their mph.
def speeder_output(speeders) 
  speeders.each do |regplate|
    printf("%s %i",regplate, DISTANCE)
  end
end  

speeders = calculations(speeders)
speeder_output(speeders)

1 个答案:

答案 0 :(得分:2)

如您所知,数组是一个数组数组。您只需输出子数组的每个元素。

# Outputting the speeders with their mph.
def speeder_output(speeders) 
  speeders.each do |reg, speed|
    printf("%s %i",reg, speed)
  end
end