(Ruby)Select an element in the array randomly [duplicate] - arrays

This question already has answers here:
How do I pick randomly from an array?
(7 answers)
Closed 7 years ago.
Select a element in the array randomly WITHOUT using the Random class.
id={1,2,3,4,5}
Is there any method which belongs to the Array class that is used to randomly select an element?

You can use the sample method
[:foo, :bar].sample # => :foo, or :bar :-)
Credit this SO question/answer

Related

How can I convert an array of elements to the individual elements for variable element method in Scala? [duplicate]

This question already has answers here:
What does `:_*` (colon underscore star) do in Scala?
(4 answers)
Closed 3 years ago.
I'm wondering if it's possible to convert an array of elements to the elements themselves. For my example, I'm trying to pass individual strings into a function whose arguments are a variable number of strings. I currently have an array of strings and am trying to pass them into .toDF (which takes a variable number of strings). This is what I have:
.toDF((df.columns)
How can I convert this array into the individual elements?
You can do this with scala's splat operator (more information here):
.toDF(df.columns: _*)

Find the only difference between two arrays in Ruby [duplicate]

This question already has answers here:
Find number deleted from array
(2 answers)
Closed 5 years ago.
I have two arrays a and b. a always has one element more than b, and the arrays are mixed.
a = [1,5,7,2,4]
b = [2,4,7,1]
I want to find the extra element in a. The output should be 5.
What is the best approach in ruby? Is it good idea to use loop?
I think this is the best built-in function to get what you expect
(a - b).pop

How to check if one array has exactly the same elements as another (with duplicates) and in any order [duplicate]

This question already has answers here:
Check if elements in two arrays are same irrespective of index
(2 answers)
How do I check in Swift if two arrays contain the same elements regardless of the order in which those elements appear in?
(10 answers)
Closed 5 years ago.
I tried using sets but obviously that doesn't work. I need a function to be able to work like this:
let Array1 = [2,2,1,1]
let Array2 = [2,1,2,1]
let Array3 = [2,2,2,1]
I need my function to be able to recognize that Array1 and Array2 are equal without thinking that Array3 is the same. Any thoughts?

Combine values of two arrays to form key and values to hash in ruby [duplicate]

This question already has answers here:
Combine two Arrays into Hash
(8 answers)
Closed 6 years ago.
If i have two arrays containing about 20 separate values is it possible to combine the two into a hash? With the contents of one array acting as the keys and the other as the values?
Sure, very simple
Hash[[1,2,3,4].zip([5,6,7,8])]
=> {1=>5, 2=>6, 3=>7, 4=>8}
But it might be a problem if the arrays have not the same size.

Check if element in array is Null / Excel VBA [duplicate]

This question already has answers here:
How to check for Is Not Null in VBA?
(2 answers)
Closed 7 years ago.
While retrieving data from a database into an array one of my array elements is (or appears to be) 'Null', as the watch window shows.
However, pCurveDefinitions(2,0)=Null return FALSE.
How can I make the check in such a way it returns TRUE?
This is just a different test, like IsError, you should use IsNull(pCurveDefinitions(2,0))!

Resources