This question already has answers here:
How to declare an empty 2-dimensional array in Ruby?
(5 answers)
Closed 6 years ago.
I have create empty array of array like below , Is there any other best way to initialize it ?.
arr = [[],[],[],[],[],[],[],[],[]]
I think the best way to achieve it by using Array class.
ex:
Array.new(width){Array.new(height)}
you can also provide width & height value like width = 2 & height = 4
Have you tried something like
arr = Array.new(9) { Array.new }
?
Related
This question already has answers here:
How to concatenate (join) items in a list to a single string
(11 answers)
Closed 3 years ago.
f = open("dict.txt","a")
f.write("Customer_Name: "+Name+"\nMovies: "+Movies_Name+"\nQuantity: "+str(quantity)+"\n")
f.close()
Movies_Name is a list which might contain more than one movies. Now I want to write the names of the movies individually instead of writing them as a list.
For me it has nothing to do with discord itself. It is just Python question.
So answer can be found here
By using ''.join
list1 = ['1', '2', '3']
str1 = ''.join(list1)
In your case:
f.write("Customer_Name: "+Name+"\nMovies: "+''.join(Movies_Name)+"\nQuantity: "+str(quantity)+"\n")
This question already has answers here:
What does the (unary) * operator do in this Ruby code?
(3 answers)
Closed 5 years ago.
I've ecountered this today and I have no idea what it means. I tried to google it but I had no luck. Can someone explain this to me?
combinations.each do |combination|
messages = EventNotification.where('user_id = ? AND message_template = ?', *combination)
...
end
It's called the splat operator, and it unpacks an array into single method arguments. In this case, because the function presumably expects two more arguments after the format string, it's equivalent to:
messages = EventNotification.where('user_id = ? AND message_template = ?',
combination[0], combination[1])
In other languages, this feature is often called "varargs".
This question already has answers here:
How to dynamically create a local variable?
(4 answers)
Closed 7 years ago.
I am trying to perform the following operation.
#i, price_0, price_1, price_2 = 0, 0, 0, 0
until #i > 2 do
if trade_history[#i]["type"] == 2
price_"#{#i}" = (trade_history[#i]["xbt_gbp"]).to_f ##NOT WORKING
end
#i += 1;
end
I cannot find anywhere online where it says that you can dynamically name a variable in Ruby. What I want to be able to do is to extract the prices of the trade_history object whenever they have a type 2. I need to be able to use the prices variables (price_0..2) to make calculations at the end of the loop. Please help! :-)
Just store the values in an array:
prices = []
3.times do |i|
history = trade_history[i]
prices << history["xbt_gbp"].to_f if history["type"] == 2
end
After this loop the prices array would hold the results and look like this:
prices
#=> e.q. [0.2, 0.4, 0.5]
Calculations can be easily done with reduce or inject:
prices.inject(:+)
#=> 1.1
I do not think this is the best way to do it, you should rather use a Hash containing the prices, but if you really want to dynamically assign the local variables you created, use binding.local_variable_set.
binding.local_variable_set("price_#{#i}", "your value")
Note that this is only available from Ruby 2.1. See How to dynamically create a local variable? for more info.
If you prefer an instance variable, you can use.
instance_variable_set("#price_#{#i}", 1)
This question already has answers here:
How to use d3.min and d3.max within a d3.json command
(3 answers)
Closed 8 years ago.
If I want the maximum value of an array, I can do this:
var data = [1,2,3,4,5];
console.log(d3.max(data)); //output 5
But if I try with an associative array, I get 'undefined':
var data = {'foo' : 4, 'baz' : 8};
console.log(d3.max(data)); //output undefined
How do I use d3.max() with an associative array?
Based on help from the comments to the question, this was the solution:
d3.max(d3.values(data));
This question already has answers here:
How can I find out the position of an item contained by an array?
(3 answers)
Closed 9 years ago.
Let's say I have the following array:
array = ["a","a","a","a","a","a","b","b","b","b","b","b"]
I want to find the index of the first "b" in the array. What is the best way of doing it?
Use Array#index
for first occurrence and Array#rindex for last occurrence of an element.
array = ["a","a","a","a","a","a","b","b","b","b","b","b"]
array.index("b") # => 6
array.rindex("b") # => 11