ruby - Create arrays via code -
I was wondering if it is possible to create dynamic ARA, i.e., arrays with code based on user input If the user enters 3
, the code creates three arrays or if the user enters 5
, the code creates five arrays on how I can do it Any ideas?
print 'how many arrays? '# = & Gt; It looks like 5 has entered arrays = Array.new (gets.to_i) {[]} # = & gt; [[], [], [], [], [], []]
It will create an array holding 5 different arrays. If you want to store each one in a different variable, then you can use the fact that Ruby allows you to dynamically create example variables:
print 'how many arrays ? 'Number = is found. TOII number. Times.each | I #If the number is 5, then I have 0,1,2,3,4 examples_with_set (: "@ array_ # {i}", array.New) end p @ array_0, @ array_1, @ array_2, @ array_3, @ array_4 Assume that we have entered 3 here, the first 3 example variables ( array_0
via array_3
) will print []
, while Previous 2 prints zero
(because they lack a value).
Comments
Post a Comment