how does this work?
in irb:
>> class A
>> b = [1, 2,3]
>> end
=> [1, 2, 3]
Is b an instance variable? class variable? how would I access b from
outside the class? Is it used for meta-programming?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
No, it’s a local variable inside the
class ... endscope.You wouldn’t. It goes out of scope (and is thus inaccessible) once it reaches the
end.It can be. Example:
I’ve now defined the methods foo1, foo2 and foo3.
Of course this wouldn’t behave any differently if I didn’t create the variable b and just did
[1,2,3].eachdirectly. So creating a local variable by itself does nothing, it allows you to write cleaner code (the same as using local variables in a method).