LoginSignup
0
0

More than 5 years have passed since last update.

RubyのHashをindex=1からeachしながらkey/valueに簡単にアクセスしたい

Last updated at Posted at 2018-11-12

Request

HashをLoopさせるためにeachを使いたい
数のカウントをしたいので,1からIndexをカウントしたい
key, valueに簡単にアクセスしたい

Solution

irb
hash = {
    "hoge" => "HOGE",
    "fuga" => "FUGA",
    "piyo" => "piyo",
}

hash.each.with_index(1) { |(key, value), index|
  puts "## key=#{key}, value=#{value}, index=#{index}"
}
output
## key=hoge, value=HOGE, index=1
## key=fuga, value=FUGA, index=2
## key=piyo, value=piyo, index=3

---///

0
0
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0