I've been looking at learning a new dynamic scripting language for web development and after agonising over Python and Ruby, as I really liked both, I've decided to pick Ruby (It pretty much came down to a coin toss and the fact there are more RoR jobs in the UK than Python/Django). My question is about scope in Ruby. Do I have to declare a class attribute inside a method to be able to access it from other methods?
For example, I cannot do
class Notes
@notes = ["Pick up some milk"]
def print_notes
puts @notes
end
end
It seems I have to declare attributes I want to use in the constructor? This example works:
class Notes
def initialize
@notes = ["Pick up some milk"]
end
def print_notes
puts @notes
end
end
Is this right? I've noticed prefixing example one with @@ instead of @ works but to my understanding if the class has a subclass (say, Memo) then any changes to attributes prefixed with @@ in Notes would change the value in Memo?
Sorry if this is a repeat question, just a lost noobie :)