im to Ruby; I see theres been various questions on this topic..but cant find one like this bu I gather multidimensional arrays don't exist in ruby as such. So im working with a nested array for a fictitious data set. is it possible to reference an index position for a child element in a nested array? I've figured iterating through and creating a block for each array is the best way forward (and confirmed this on SO). i see in another article its recommended to use the Narray library...does this work with strings? my first aim is to print out each name and the last area of interest. ive put an example of what im roughly trying to achieve in the example.. many thanks
staff_array = [ ["paulBarry", ["programming", "networking", "security", "open source" ,"frameworks"]], ["chrisMuedec", ["testing", "safety systems", "formal systems", "programming languages"]], ["nigelwhite", ["graphics", "imaging", "programming","sign languages","trees"]], ["austinKinsella", ["networks", "wans", "programming", "macintosh", "digital photography"]], ["gerryMoloney", ["placement", "employment", "emerging systems", "webdevelopment"] ] ] staff.each do |name_array| # Iterate through the parent array, returning each element sequentially name_array.each do |interest_element| # Iterate through each element of the child array returned by the above parent iteration Example:
puts {#name_array}+ name_array.each do |interest_element|[-1] end end Expected output would be:
Paulbarry: Frameworks ChrisMuedec: Programming languages Nigelwhite: Trees AustinKinsella: Digital photography GerryMoloney: Webdevelopment.
puts {#name_array} + name_array.each do |interest_element|[-1]; enddoes not do what you expect. Firstly,#name_arraywould raise an exception. If you write,puts name_array + name_array.each do |interest_element|[-1]; end, Ruby first executes,name_array.each do |interest_element|[-1]; end. You will see that the method Array#each returns it's receiver,name_array, so the expression becomesputs name_array + name_array.