HEMANT SONKER'S BLOG

Sunday, December 2, 2007

Names in Ruby......

1. Names - Ruby names are used to refer to constants, variables, methods, classes, and modules. The first character of a name helps Ruby to distinguish its intended use. Certain names, are reserved words and should not be used as variable, method, class, or module name. Lowercase letter means the characters ''a'' though ''z'', as well as ''_'', the underscore. Uppercase letter means ''A'' though ''Z,'' and digit means ''0'' through ''9.'' A name is an uppercase letter, lowercase letter, or an underscore, followed by Name characters: This is any combination of upper- and lowercase letters, underscore and digits.

2. Variables - Variables in Ruby can contain data of any type. You can use variables in your Ruby programs without any declarations. Variable name itself denotes its scope (local, global, instance, etc.).

2.1. A local variable name consists of a lowercase letter followed by name characters (sunil, _z, hit_and_run).

2.2. An instance variable name starts with an ''at'' sign (''@'') followed by an upper- or lowercase letter, optionally followed by name characters (@sign, @_, @Counter).

2.3. A class variable name starts with two ''at'' signs (''@@'') followed by an upper- or lowercase letter, optionally followed by name characters (@@sign, @@_, @@Counter).

2.4. A constant name starts with an uppercase letter followed by name characters. Class names and module names are constants, and follow the constant naming conventions. By convention, constant variables are normally spelled using uppercase letters and underscores throughout (module MyMath, PI=3.1416, class MyPune).

2.5. Global variables start with a dollar sign (''$'') followed by name characters. A global variable name can be formed using ''$-'' followed by any single character ($counter, $COUNTER, $-x).


3. Method names should begin with a lowercase letter. ''?'' and ''!'' are the only weird characters allowed as method name suffixes

0 Comments:

Post a Comment

Subscribe to Post Comments [Atom]

<< Home