Iterating over multi-dimensional arrays - What went wrong?


#1



https://www.codecademy.com/en/courses/ruby-beginner-en-F3loB/2/3?curriculum_id=5059f8619189a5000201fbcb#


undefined local variable or method `sub_array' for #


s = [["ham", "swiss"], ["turkey", "cheddar"], ["roast beef", "gruyere"]]

s.each do |x|
    x = sub_array
end
sub_array.each do |x|
    puts x
end

I don't get it , why i cant define sub_array through my block of codes? I do understand the solution but i don't get why my solution is wrong. It seems to me that either you can't define variable to elements or you can't define variable under each iterator.


#2

This line could just be,

puts x

and the second loop can be removed. It looks like Ruby flattens the array in the background.

To write a more explicit loop,

s.each do |sub_array|
  sub_array.each do |x|
    puts x
  end
end

but the output will be the same.


#3

Hi, I understand what you mean. However i don't understand why i cant define x = sub_array. It keep sayings "undefined local variable or method `sub_array' for #". Hope you can clarify this . Thanks in advance


#4

In your s.each instance you define x as the block parameter, but then immediately assign it an undefined variable. That is what the error message is telling us. What happens if we switch them around?

sub_array = x
sub_array.each ...

#5

Okay I'm getting puzzled. Firstly what's a block parameter ? (I might have miss that one out). Secondly, what is wrong with assigning x = sub_array? Isn't what I am doing now defining x, the elements of s to sub_array(the new variable i want to define)? Also what is the difference between sub_array = x and x = sub_array? Aren't they the same? I tried putting sub_array = x, it does not result in any error. I am a complete newbie, i hope my question isn't confusing because im quite confused.


#6

Now you have us at a disadvantage. We don't have any way of knowing how much you already know since your question suggests it is minimal at best. Should we be pulling out the primer for this language? Or programming in general? Where should we start so we get you going on the forward path?

A Ruby block looks like so,

{ |params| procedure }

The block parameters are what their name suggests, the parameters for the block procedure. Namely, the variables defined as local to the function that the block performs.

Assignment statements are not comparisons of equality or identity. They are unidirectional in nature with the value or expression on the right side being assigned to the variable on the left side of the equal sign.

a <= b  # symbolic

Whatever b is gets copied into a. Consequently we could not assign sub_array to x. We only wipe out the parameter value. But assigning x to sub_array makes perfect sense.

One other consideration is static versus transient. Generally, the right side is transient in a dynamic situation. The left side is, too, but less so. It will remain static for the duration of this cycle. It's ballet, really.


#7

s = [["ham", "swiss"], ["turkey", "cheddar"], ["roast beef", "gruyere"]]

s.each do |x|
sub_array = x
end

puts sub_array

One more to clarify, the variable sub_array is only defined between the do and end right? That means the sub_array outside do/end, is a undefined variable right?


#8

That will take some testing.x is defined in the block parameters, so is local to the block. sub_array is defined in the block, but not in the parameters. That makes it also a local variable.

Let's run a test...

s = [["ham", "swiss"], ["turkey", "cheddar"], ["roast beef", "gruyere"]]

s.each do |x|
    sub_array = x
end

puts sub_array

(eval):12: undefined local variable or method `sub_array' for main:Object (NameError)


#9

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.