Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
Here is my problem:
Given a multi dimensional array: [['monday', 'saturday'], ['beginner'], ['kid', 'adult']]
I want:
['monday', 'monday-beginner', 'monday-beginner-kid', 'monday-beginner-adult', 'monday-kid', 'monday-adult',
'saturday', 'saturday-beginner', 'saturday-beginner-kid', 'saturday-beginner-adult', 'saturday-kid', 'saturday-adult',
'beginner', 'beginner-kid', 'beginner-adult',
'kid',
'adult']
Here are questions I saw that could help:
Creating permutations from a multi-dimensional array in Ruby
How to combination/permutation in ruby?
first, *rest = [['monday', 'saturday'], ['beginner'], ['kid', 'adult']]
.map{|a| [nil, *a]}
first.product(*rest).map{|a| a.compact.join("-")} - [""]
I've a solution in 2 steps :
- Find all the possible combinaisons, 4 in your examples:
['monday', 'beginner', 'kid']
['monday', 'beginner', 'adult']
['saturday', 'beginner', 'kid']
['saturday', 'beginner', 'adult']
Then use combinaison method on it 3 times using n from 1 to 3 and then, merge the 3 returns. You'll finally just have to call .join("-") on each permutation.
Related
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I am trying to count the arrays themselves not the elements. so say i had the following
arrays = [["1 2"],["3 4"],["5 6"],["6 7"]]
i am then trying to find a way to return only one of these arrays as a set of instructions if asked for?
Your example showed a multi-dimensional array, which is an array of arrays.
To count the number of arrays in this 2 dimensional array, in other words the number of elements, you could use the following methods.
arrays = [["1 2"],["3 4"],["5 6"],["6 7"]]
arrays.length
arrays.size
arrays.count
If you would like to return one of them you simply reference the element
arrays[0]
=> Returns ["1 2"]
If you want to return a random element,
arrays.sample
=> Returns a random array
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I use the excellent faker gem to generate random words for my models. Eg. product.name = Faker::Lorem.word
Sometimes I need to generate a sentence, and I want the length of the sentence to
vary each time.
How to achieve this with ruby?
How about:
result = rand(max_size).times.map { produce_word }
Since you have not provided enough information, this is my approach, [*1..100].sample will return a random number between 1 and 100, so looping that times the string which is returned bya method named get_word will get stored in the array word_array
word_array = []
[*1..100].sample.times do
word_array << get_word
end
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
How to convert a fixnum to an array in ruby?
For Example:
s = SituationType.all(:conditions => {:name => 'did not match retrieved design - text misspelled'}).collect(&:id)
result: [10034, 10055]
sf = situation_type_id = SituationType.find_by_name('did not match retrieved design - text misspelled').id
result: 10034
s -sf says,
TypeError: can't convert Fixnum into Array
Do the following
s - [sf]
Since sf is an integer, It cannot do the subtraction unless you convert it into an array
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
Need to add two arrays before that need to add some value at starting of the first array. Look at the following:
#conunty_format = [ "country", "imps", "revenue","network_revenue"]
final_ca = [2000,55.62,88.69]
I need to add "Canada" to final_ca and generate hash with corresponding county_format.
Hash[#conunty_format.zip(final_ca.unshift('canada'))]
=> {"country"=>"canada", "imps"=>2000, "revenue"=>55.62, "network_revenue"=>88.69}
You can use Array Zip and some properties of Array to achieve it in a single line. see the below code.
resulted_hash = #country_format.zip(final_ca.unshift("Canada")).inject({}) do |r, s| r.merge!({s[0] => s[1]}) end
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
I have this:
array = ["a","b","c"]
How do I get this:
"a","b","c"
I need to get the items out of the array, each double-quoted, separated by a comma.
array.collect { |a| "\"#{a}\"" }.join(",")
I'm just started to learn ruby, I guess:
return ["a","b","c"].map{|i| '"' + i + '"'}.join(",")
Might you want to get this:
irb(main):009:0> [1, 2, 3].map(&:to_s).join('","')
=> "1\",\"2\",\"3"
"a","b","c" this is not abject (these are 3 objects). But in ruby any code returns object value. So you should know what you want to get: 1 object (I returning string in this example) or various. If you want to get 3 objects you should extract array like this:
a, b, c = [1,2,3].map(&:to_s)