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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T16:37:15+00:00 2026-06-15T16:37:15+00:00

@data , @x , @y , and @z are all arrays of the same

  • 0

@data, @x, @y, and @z are all arrays of the same type. I use @data in my view to iterate through it and display all the values.

  @data = if params[:a] == "1"
    @x
  elsif params[:b] == "1"
    @y 
  elsif params[:c] == "1"
    @z

What I’d like to do is also intersect the arrays if multiple parameters are applied. So, for example, if ?a=1&b=2 is appended to the URL, then I’d like @data to equal @x & @y

I’m not sure how to write this code – what’s the easiest way to figure out which params == 1 and then intersect the corresponding arrays and store that in @data?

  • 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-15T16:37:16+00:00Added an answer on June 15, 2026 at 4:37 pm

    Fortunately, Ruby makes this easy!

    @data = [params[:a] && @x, params[:b] && @y, params[:c] && @z].compact.inject(:&)
    

    This constructs an array of either arrays (if the parameter is present; you could check == '1' if you wanted to be more specific) or nils (if it is not present). compact then strips the nils out of the array, and we can then intersect the remaining values by calling inject(:&).

    The inject call iterates over your array of values, takes the first value, and then applies the & method with the next value as a parameter. It then takes the result and applies & with the third parameter, and so on, resulting in a final value which is the intersection of all of the arrays in the passed array.

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

Sidebar

Related Questions

I often have the need to view data in arrays and use the html
I have an algorithm where I create two bi-dimensional arrays like this: TYPE TPtrMatrixLine
I need to dump all data from the object into two-dimetional array this is
If I create a cell array using: clear all data = {rand(1,5),rand(1,4),rand(1,4),rand(1,6)}; a =
I'm trying to save some data into an array but unfortunately all the data
List all node as element of an array? my xml data: <videos> <video> <id>1</id>
My application should be syncronized with a ServerDB to collect some Data all users
Long introduction: Normally all data necessary for my web application are stored in session
I'm fetching data from all 3 tables at once to avoid network latency. Fetching
I have some data structures: all_unordered_m is a big vector containing all the strings

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.