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 7847383
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T17:52:40+00:00 2026-06-02T17:52:40+00:00

I don’t know how exactly to word this, but I am trying to define

  • 0

I don’t know how exactly to word this, but I am trying to define many variables and then redefine them without rewriting each of the many variables and creating redundant code in each new block I write. The variables are defining array elements from multiple databases. Here is a downsized sample what I am working with:

def lots_of_vars(array)
  name = array[1]
  membership = array[2]
  spouse = array[3]
  ....
  lap12 = array[36]
end

def second_block
  #database1 => [ "Randy", true, "Nancy", 2, 17, false...
  lots_of_vars(database1)
  return unless membership
  puts "Lap progress for #{name} and #{spouse}: #{lap1}, #{lap2}... #{lap12}..."
end

def third_block
  #database2 => [ "Steven", true, nil, 0, 5, false...
  lots_of_vars(database2)
  return unless spouse.empty? or spouse.nil?
  puts "Weekly progress for #{name}: #{lap1}, #{lap5}, #{lap6}, #{lap10}..."
end

The second and third block need all the variables defined from the first block/method. But how do I pass all these variables? One example I read suggested I pass them all as parameters like:

def second_block(name, membership, spouse...)

but this would make just as much of a mess as defining each variable twice in both blocks. What is the simple, dry approach to such a situation?
Please let me know if I need to clarify anything in my question, Thanks.

  • 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-06-02T17:52:43+00:00Added an answer on June 2, 2026 at 5:52 pm

    What you want is to create a Struct, which is a simple class to represent a datastructure. A struct will take its arguments in by position, which is exactly what you want, since you can splat the array into the method call (turn an array into an argument list)

    So

    Thing = Struct.new(:name, :membership, :spouse, :lap12)
    
    array = ['Larry', 'gold', 'Sue', 2.2]
    thing = Thing.new(*array)
    
    #note that the splat (*) is equivalent to saying
    # Thing.new(array[0], array[1], array[2], array[3])
    
    thing.name # => "Larry"
    thing.lap12 # => 2.2
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Don't know if I'm over-thinking this or not.. but I'm trying to be able
don't know if the title describes anything about what I'm trying to say but
I don't know why, but this code worked for me a month ago... maybe
Don't know whats exactly going on, but it's definitely killing my time for nothing.
Don't know if this is the right place to ask this, but I will
Don't know why this is happening, but after submitting a form via JS (using
(Don't know if this is strictly on-topic, but I don't see any better Stack
Don't know how to explain it better but i'm trying to get a response
Don't know if this is an eclipse specific problem but whenever I declare a
Don't know why but I can't find a solution to this. I have 3

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.