Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

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.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 390353
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T15:57:22+00:00 2026-05-12T15:57:22+00:00

fruit = [apple,red,banana,yellow] => [apple, red, banana, yellow] Hash[*fruit] => {apple=>red, banana=>yellow} Why does

  • 0
fruit = ["apple","red","banana","yellow"]
=> ["apple", "red", "banana", "yellow"]

Hash[*fruit]    
=> {"apple"=>"red", "banana"=>"yellow"}

Why does the splat cause the array to be so neatly parsed into the Hash?

Or, more precisely, how does the Hash “know” that “apple” is the key and “red” is its corresponding value?

Is it simply because they are at consecutive positions in the fruit array?

Does it matter that the splat is used here? Can a Hash not define itself from an arry so directly otherwise?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-12T15:57:23+00:00Added an answer on May 12, 2026 at 3:57 pm

    As the documentation states:

    Hash["a", 100, "b", 200]       #=> {"a"=>100, "b"=>200}
    Hash["a" => 100, "b" => 200]   #=> {"a"=>100, "b"=>200}
    { "a" => 100, "b" => 200 }     #=> {"a"=>100, "b"=>200}
    

    You can’t pass an array to the Hash[] method according documentation, thus the splat is just a way to explode the fruit array and pass its elements as normal arguments to the Hash[] method. Indeed this is a very common use of the splat operator.

    The cool thing is that if you try to pass an odd number of arguments to Hash you will get an ArgumentError exception:

    fruit = ["apple","red","banana","yellow","orange"]
    #=> ["apple", "red", "banana", "yellow", "orange"]
    Hash[*fruit] #=> ArgumentError: odd number of arguments for Hash
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

var fruit = [apple,pear,pear,pear,banana]; How do I remove all pear fruit from this array?
my %fruit_colors = (apple, red, banana, yellow); my @fruits = keys %fruit_colors; my @colors
I have the following lists in Prolog: fruit(banana, yellow, curvy, 4). fruit(apple, green, round,
member purchase fruit 1 1 apple 1 2 apple 1 3 banana 1 4
Say I have three tables: Fruit (Table 1) ------ Apple Orange Pear Banana Produce
I have an array $data fruit => apple, seat => sofa, etc. I want
I have a simple simulated array with two elements: bowl["fruit"] = "apple"; bowl["nuts"] =
So consider an array with my 3 favorite fruits: $array1 = array(Apple, Banana,Raspberry) I
I've encountered this situation so many times... enum Fruit { Apple, Banana, Pear, Tomato
SELECT person.id, person.name, COUNT(DISTINCT fruit.apple) AS Red Apple, fruit.* FROM (SELECT * FROM tree

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.