[#1816] Ruby 1.5.3 under Tru64 (Alpha)? — Clemens Hintze <clemens.hintze@...>

Hi all,

17 messages 2000/03/14

[#1989] English Ruby/Gtk Tutorial? — schneik@...

18 messages 2000/03/17

[#2241] setter() for local variables — ts <decoux@...>

18 messages 2000/03/29

[ruby-talk:01908] variable prefixes (Re: Re: Matrix Multiplication in Ruby)

From: matz@... (Yukihiro Matsumoto)
Date: 2000-03-16 17:28:20 UTC
List: ruby-talk #1908
Hi,

In message "[ruby-talk:01853] Re: Matrix Multiplication in Ruby"
    on 00/03/15, "Dat Nguyen" <thucdat@hotmail.com> 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.

In This Thread

Prev Next