From: matz@... (Yukihiro Matsumoto) Date: 2000-03-17T02:28:20+09:00 Subject: [ruby-talk:01908] variable prefixes (Re: Re: Matrix Multiplication in Ruby) Hi, In message "[ruby-talk:01853] Re: Matrix Multiplication in Ruby" on 00/03/15, "Dat Nguyen" writes: |But I still see weird prefixes like $ for global variables and @ for local |ones. |Are they the only prefixes tolerated in Ruby? OK, here's the list of variable prefixes. $ global variables @ instance variables Uppercase constant Lowercase and _ local variables and @@ experimental class variabls in 1.5. |Why are they introduced? (Python doesn't have any, maybe it uses 'self' |instead). Prefixing variables has several effect: * it eliminates variable declaration totally; no `global' etc. * as a consequent, you can tell scope of a variable at a glance; no search for declaration needed. * because too much `$'s are ugly, it descourages use of global variables. * by using `@' for instance variable prefix, you can a) reduce attribute lookup to 1, instead of Python's 2 (one for self, another of real attribute access). b) allow instance variables to be private by default. c) type less since `@' is much shorter than `self.'; appeals lazy people like me. matz.