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

  • Home
  • SEARCH
  • 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 6246767
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T12:45:04+00:00 2026-05-24T12:45:04+00:00

A recent good article on ruby destructuring defines destructuring as ability to bind a

  • 0

A recent good article on ruby destructuring defines destructuring as ability to bind a set of variables to a corresponding set of values anywhere that you can normally bind a value to a single variable, and gives an example of block destructuring

triples = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]

triples.each { |(first, second, third)| puts second } =>#[2, 5, 8]

In this case we have an idea of the number of elements in the main array and therefore when we provide the arguments first,second,third we can get the corresponding result. So what about if we had an array of arrays whose size is determined at run-time?

triples = [[1, 2, 3], [4, 5, 6], [7, 8, 9],...,[]]

and we would want to get the elements of the first entry for each subarray?

triples.each { |(first, second, third,...,n)| puts first }

what is the best way to create the local variables (first, second, third,...,n) dynamically?

  • 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-24T12:45:05+00:00Added an answer on May 24, 2026 at 12:45 pm

    In your specific case, you’d use a splat to collect up everything but the first value:

    triples.each { |first, *rest| puts first }
    #-----------------------^splat
    

    The *rest notation just collects everything that’s left into an Array called rest.

    In general, there’s not much point to creating an arbitrary number of local variables (second, third, ..., nth) because you wouldn’t be able to do anything with them; you could probably whip up a sick mess of evals but we already have a fine and functional Array class so why bother?

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Does anyone have any suggestions for a good tool, xslt or other that can
I'm testing a recent WCF Data Service that I've set. Most of things are
I read that oracle's CBO(on recent versions) is so good that even if worst
A recent article on Ars Technica discusses a recent study performed by the Psychology
A recent question about string literals in .NET caught my eye. I know that
A recent post by John Gruber notes that the following legalese: 3.3.1 — Applications
In a recent CODE Magazine article, John Petersen shows how to use bitwise operators
In a recent question of mine I learned that if there are more than
In couple of recent projects that I took part in I was almost addicted
In a recent conversation, I mentioned that I was using JavaScript for a web

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.