1

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. 
6
  • What's your expected output? Have you checked if a Hash is a better data structure for the type of data staff is currently holding? Commented Jun 14, 2020 at 11:42
  • 1
    Can you post valid ruby code (code which does not raise an error when run) along with the expected result? Commented Jun 14, 2020 at 15:51
  • Hi Sebastian, im fairly new to this as i understand hashing is for key value pairs ie 1 value to 1 key... so as i have multiple values per key i overlooked it. Hi Viktor, i will update as soon as i can, thanks Commented Jun 14, 2020 at 16:23
  • 1
    puts {#name_array} + name_array.each do |interest_element|[-1]; end does not do what you expect. Firstly, #name_array would 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 becomes puts name_array + name_array. Commented Jun 14, 2020 at 18:03
  • Welcome to SO! Please see "How to Ask", "Stack Overflow question checklist" and all their linked pages. Grammar is important on SO, so please put in the effort to write as well as possible. I'd recommend using a spelling and grammar checker. SO isn't a message board or forum, it's more like an online reference book where grammar matters. Commented Jun 14, 2020 at 20:46

2 Answers 2

1

Ruby has nested arrays. You reference index positions with the [] method starting at 0, no matter the nesting level.

array = [ [ ['0.0.0', '0.0.1', '0.0.2'], ['0.1.0', '0.1.1', '0.1.2'], ['0.2.0', '0.2.1', '0.2.2'], ], [ ['1.0.0', '1.0.1', '1.0.2'], ['1.1.0', '1.1.1', '1.1.2'], ['1.2.0', '1.2.1', '1.2.2'], ], [ ['2.0.0', '2.0.1', '2.0.2'], ['2.1.0', '2.1.1', '2.1.2'], ['2.2.0', '2.2.1', '2.2.2'], ], ] array[0][0][0] # => '0.0.0' array[0][1][2] # => '0.1.2' array[2][2][2] # => '2.2.2' 

There are also some specific methods like first and last.

array.first.first.first # => '0.0.0' array.last.last.last # => '2.2.2' 
Sign up to request clarification or add additional context in comments.

6 Comments

Or array.dig(0, 1, 2)
Be careful with dig, @steenslag. array[1][99][0] #=> NoMethodError (undefined method '[]' for nil:NilClass), which is probably what is wanted, whereas array.dig(1, 99, 0) happily returns nil, possibly masking the error for some time.
sounds good thanks! just had a nose round on the docs as im looking for simple way of accessing the last element of each array .i may give the dig approach a try.. as it sounds straight forward.. potentially! thanks chaps
Hi Cary, apologies just saw your comment, tbh i dont fully understand
To put @CarySwoveland's comment in human terms: if your supervisor tells you "Go to Parkland Drive 7628, and ask the man of the house for the name of his eldest child",but it turns out that Parkland Drive only has house numbers up to 127, the first one would be equivalent to coming back to your supervisor with an error "there's no such address"; the second would be telling him "the man has no children". Defaults are dangerous when there can be multiple explanations for the result. (No such address, or no "man of the house" - a single woman, a lesbian couple... - or legitimately no children).
|
1

my first aim is to print out each name and the last area of interest.

staff_array.each do |name, interests| puts "#{name.sub(/./) { |m| m.upcase }}: #{interests.last.capitalize}" end 

Output:

PaulBarry: Frameworks ChrisMuedec: Programming languages Nigelwhite: Trees AustinKinsella: Digital photography GerryMoloney: Webdevelopment 

Perhaps from this first step you can infer how to generalize to answering your other questions.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.