Undefined local variable or method `aa' for #<Context:0x10999c0>


#1



https://www.codecademy.com/courses/ruby-beginner-en-Qn7Qw/2/4?curriculum_id=5059f8619189a5000201fbcb


I definded the array "aa" and this say I didnt.

The error: "undefined local variable or method `aa' for #"


r = 0
aa = ["a","b","c"]
print "letter:"
def s    
    text = gets.chomp
    aa.each do |a|
        if text==a
            r+=1
        end
    end
    if r==1
        puts "11111"
    else
        puts "somthing else:"
    end
end
s


#2

Ruby does not see global variables inside a method. We have to define them as global.

$some_global_variable = ["a","b","c"]

def some_method
    $some_global_variable

You could always pass the variable into the method:

def my_method(my_array)
    # code

Best practices dictate that we avoid using global variables for a number of reasons. I'll leave you to look this up.


#4

Thank you soooo much!!!!


#5

You're welcome! I see you withdrew your question about recursion...

def factorial(n)
    return 1 if n == 1
    n * factorial(n - 1)
end