Prev - DUP - Next - TOC

Variables


Ruby has three sorts of variables, one sort of constant and two sorts of pseudo-variables. The variables and the constants have no type. There are also demerits due to non-typed variable, however, it seems to fit to `easy programming' as ruby aiming.

One can obtain the sort of variable and constant by its name. It is one of the features of ruby, the name tells that it is a variable or a constant, so we don't need declarations and one can tell the sort apart with a glance. This feature is convenient for programming, but it may cause another problem to make finding typo difficult.

The sort is told by the first character of the name. The following give the correspondings.

	$		global variable
	@		instance variable
	[a-z]		local variable
	[A-Z]		constant

The exception of the above rule is in the case of pseudo-variable. Nonetheless the pseudo-variable looks as same as the local variable, it is the constant in the fact. There are only two sorts of the pseudo-variables, so I guess you will never confuse them.

	self            the object executing currently
	nil             `meaningless' value (for false)

Let's see few example.

 ruby> self
 main
 ruby> nil
 nil

`main' is the object of the top-level. The value of `self' inside each method may be different. The pseudo-variables are eventually the constants, so substitutions into `self' or `nil' are forbidden.


Prev - DUP - Next - TOC