I am very familiar with Java but have been venturing out into new languages. In Java, we can declare instance variables outside methods in the body of the class like so:
public class Test {
public String str = "Hello!";
public int count = 0;
public void printString() {
System.out.println(str);
}
public static void main(String[] args) {
Test t = new Test();
t.printString();
}
}
But in Ruby, when I do this, it doesn't print anything:
class Test
@str = "Hello"
def print_string
puts @str
end
end
t = Test.new
t.print_string
Why can't instance variables be declared in the class body. Isn't this setting attributes for the object? Or does everything have to be set in the initialize method?