PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Tuesday, September 20, 2022

[FIXED] Why is the method #each_index non existing for hashes in Ruby?

 September 20, 2022     arrays, hashmap, methods, ruby     No comments   

Issue

Why can I use #each and #each_with_index on both arrays and hashes, but the #each_index method can be used only on an array and not on a hash?


Solution

each_index

Iterates over array indexes.

Hashes don't have indexes. Hashes have keys. Use each_key.

Why do Hashes have each_with_index? Because that's part of the Enumerable module which both Hashes and Arrays include. Enumerable methods use shared terminology. Hashes and Arrays predate the Enumerable module, Array#each_index (sometime before v1.0r2) came before Enumerable#each_with_index (Feb 5th, 1998), so there is some overlap between Array, Hash, and Enumerable for backwards compatibility.



Answered By - Schwern
Answer Checked By - Senaida (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing