Creating a nested array with unspecified number of columns - ruby

I'm trying to create a nested array that takes the user's input and stores it in the sub arrays. The number of subarrays is given by the user. I need the columns to be unspecified, and any number of elements can go in.
Is there a way to achieve this?
I tried:
puts "How many groups do you want to create?"
number_of_teams = gets.chomp
team_array = [Array.new(number_of_teams.to_i){Array.new()}]

team_array = Array.new(number_of_teams.to_i) { [] }
So if input is 3 then team_array will have [ [], [], [] ]

This is true for any Array. Ruby doesn't have the concept of an Array of a fixed number of elements. You don't even need to ask the user for the number of groups.

Related

How to create a specific number of arrays with a single command?

I want a way to make multiple arrays at the same time, given a specific number or command. I'm specifically trying to make a pyramid with a number of arrays based on base.length, and I feel like there should be a way to make base.length number of arrays. I don't want to write multiple lines of code, I specifically want to write a method equivalent to English sentence: "make X number of empty arrays."
This can be done with
Array.new(x) { [] }
for example x = 4:
Array.new(4) { [] }
#=> [[], [], [], []]

use ruby to identify if array of terms contains one and ONLY one repeated term

In categorical logic, categorical syllogism requires four terms, but can only have three unique terms; that is one and ONLY one term can and must be repeated.
I trying to write a Ruby test for this but having a little bit of trouble.
Consider the following three arrays of terms, the first of which is a valid list, while the other two are invalid (termarray2 contains 4 unique terms and termarray3 contains only 2 unique terms).
termarray1 = ["Dogs", "Mortal Things", "Mortal Things", "Living Things"]
termarray2 = ["Dogs", "Mortal Things", "Cats", "Living Things"]
termarray3 = ["Dogs", "Mortal Things", "Mortal Things", "Dogs"]
I want to write a test called three_terms?
It should return true for termarray1 and false for termarray2 and termarray3
Any ideas how I could to this?
The uniq methods returns the unique elements in an array.
This should work:
array.uniq.count == 3
But the test you mention also checks that the original array has four elements. Thus the entire check should be:
(array.count == 4) && (array.uniq.count == 3)
Check to see if the size of unique elements is 3 (using Array#uniq):
array.uniq.size == 3
You could also monkey-patch Array with three_terms?:
class Array
def three_terms?
uniq.size == 3
end
end

Why am I geting wrong results in my array with I iterate?

I am iterating on a array within a Object van. I am trying to pop the elements of the array into another object array. See below.
#van.bikes.each { #garage<<( #van.removebike )}
def removebike
#bikes.pop
end
When I do this the resulting array in the garage has missing elements and/or duplicate elements.
The reason for this is that when ruby iterates on the array it sets number of iterations based on the original array size. When you pop an element from that array the size changes so the iteration can not work properly.
You can use instead,
#van.bikes.count.times { #garage<<( #van.removebike )}
You can try this too..
#garage = []
#van.bikes.each{|bike| #garage << bike}

Can you add to an array in Ruby whose name is dependent upon a variable?

Let's suppose a store owner wants to know how well his products are selling around the world, and which are selling best where.
He has the following data: |ID,Currency,Quantity,Location|
Rather than iterate through the data for each currency (data set > 10,000), is there a way to put the data into arrays specific to the currency without explicit designation...i.e., is there a way to avoid
if curr == "USD"; USDid << ID; USDquan << Quantity
elsif...
...and so on? For the purposes of this question, assume the *id and *quan arrays exist for the currencies under observation.
Is there some sort of regex trick that can look at the currency and put the data in the appropriate arrays?
Yes. Use a hash of arrays instead of multiple arrays:
sale_data = {}
sale_data.default = {"ID" => [], "Quantity" => [], "Location" => []}
# Later...
sale_data[curr]["ID"] << ID; sale_data[curr]["Quantity"] << quan; #Etc..
The default= call makes it so you can just assign as many currencies as you want without every predefining them. So, anywhere in your code, if there are not prior entries for, say, USD, when you add data, one is created.

matching array items in rails

I have two arrays and I want to see the total number of matches, between the arrays individual items that their are.
For example arrays with:
1 -- House, Dog, Cat, Car
2 -- Cat, Book, Box, Car
Would return 2.
Any ideas? Thanks!
EDIT/
Basically I have two forms (for two different types of users) that uses nested attributes to store the number of skills they have. I can print out the skills via
current_user.skills.each do |skill| skill.name
other_user.skills.each do |skill| skill.name
When I print out the array, I get: #<Skill:0x1037e4948>#<Skill:0x1037e2800>#<Skill:0x1037e21e8>#<Skill:0x1037e1090>#<Skill:0x1037e0848>
So, yes, I want to compare the two users skills and return the number that match. Thanks for your help.
This works:
a = %w{house dog cat car}
b = %w{cat book box car}
(a & b).size
Documentation: http://www.ruby-doc.org/core/classes/Array.html#M000274
To convert classes to an array using the name, try something like:
class X
def name
"name"
end
end
a = [X.new]
b = [X.new]
(a.map{|x| x.name} & b.map{|x| x.name}).size
In your example, a is current_user.skills and b is other_users.skills. x is simply a reference to the current index of the array as the map action loops through the array. The action is documented in the link I provided.

Resources