Ruby/Array/empty

Материал из Wiki.crossplatform.ru

Перейти к: навигация, поиск

Check for an Empty Array

x = []
puts "x is empty" if x.empty?



Loop through an array until empty

def print_each(array)
  array.each { |x| puts x.inspect }
end
hash = { "A" => "a",
         "B" => "b" }
array = hash.to_a
until array.empty?
  item, quantity = array.pop
  puts "#{quantity} #{item}"
end