I’ve just started to learn Ruby after years of programming in PHP. I wanted to know how array in Ruby is different from PHP array and also what are its similarities/differences with the hash in Ruby.
Share
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
An array, also called list in other languages, is an ordered collection of things. In PHP that’s this:
In Ruby, that’s:
You access them using numeric indexes:
$arr[0]/arr[0].Hashes are called associative arrays in PHP. They consist of keys and values:
Actually, both types are associative arrays in PHP, simply with numerical or string keys respectively. In PHP they use the same language construct and type, in other languages including Ruby they’re different types. PHP mixes hashes and lists into one
Arraytype, Ruby doesn’t.Contrary to languages like Haskell, where lists have to be homogenous, hashes/arrays can contain any kind of mixed values in both PHP and Ruby:
How they differ under the hood is hardly answerable, since the languages are very different, starting that arrays/hashes are objects in Ruby but not in PHP. If you have a specific targeted under-the-hood implementation detail question, please ask one.