---
title: "带序号循环Hash"
author: "夜鸣猪"
date: "2011-12-02T23:17:36Z"
tags: []
categories: ["编程语言"]
slug: "hash"
draft: false
lastmod: "2011-12-02T23:17:36Z"
id: 1290330
url: "https://hlee.iteye.com/blog/1290330"
---

```ruby

hash.keys.each_with_index do |key, index|
   value = hash[key]
   print "key: #{key}, value: #{value}, index: #{index}\n"
   # use key, value and index as desired
end

```



```ruby

hash.each_with_index do |(key, value), index|
   print "key: #{key}, value: #{value}, index: #{index}\n"
   # use key, value and index as desired
end

```