Tuesday, June 27, 2006

Names in Ruby

Now, let us look at Names in Ruby.
  1. Names - Ruby names are used to refer to constants, variables, methods, classes, and modules (more of this later). 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 names. 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: 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.).
    1. A local variable name consists of a lowercase letter followed by name characters (sunil, myCount, _z, hit_and_run).
    2. An instance variable name starts with an ''at'' sign (''@'') followed by an upper- or lowercase letter, optionally followed by name characters (@sign, @_, @Counter).
    3. A class variable name starts with two ''at'' signs (''@@'') followed by an upper- or lowercase letter. optionally followed by name characters (@@sign, @@_, @@Counter).
    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).
    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 (More on this later).


The Ruby convention is to use underscores to separate words in a multiword method or variable name. For Class names, module names and constants the convention is to use capitalization, rather than underscores, to distinguish the start of words within the name.

It's to be noted that any given variable can at different times hold references to objects of many different types. A Ruby constant is also a reference to an object. Constants are created when they are first assigned to (normally in a class or module definition; they should not be defined in a method - more of this later). Ruby lets you alter the value of a constant, although this will generate a warning message. Also, variables in Ruby act as "references" to objects which undergo automatic garbage collection.

Questions asked by the participants:
1. Shantanu - If I declare a variable 'x', and later on I want to make it global, can I do that?

2. Jatinder Singh - What is the scope of a global variable? Can I access them between two different files? A lot of relevant questions are coming into my mind like, what if a global variable already declared somewhere else is declared again with the same name (shadowing variables?) Also, what is the difference between Constants and Global Variables?


First Post | Previous | Next


Technorati Tags:
Blogs linking to this article

6 Comments:

Blogger Anish Betawadkar said...

1. Name characters means any combination of upper- and lowercase letters and digits. Should it say " and an underscore at the end"?

2. It's to be noted that any given variable can at different times hold references to objects of many different types. - Does that mean a variable of type x can hold reference to the variable of type B without direct relation (inheritance) between them?

2:14 PM  
Blogger SatishTalim said...

Anish you are right for your comment 1. I have corrected the same on this blog post. Will answer point 2. soon.

5:04 AM  
Blogger Jatinder Singh said...

what is the scope of global variable?
Can I access them between 2 different files?
a lot of relevant questions are coming in to my mind. like what if a global variable already declared somewhere else is declared again with the same name(shadowing variables?)

12:55 PM  
Blogger Jatinder Singh said...

There is one more in question in my head..

What is the difference between Constants and Global Variables?
I remember in C we use to have global variables to be used as constants.

12:58 PM  
Blogger शंतनु said...

Difference between constant and variable:

Start shell with 'irb --simple-prompt'

>> a=10
=> 10
>> a=a+1
=> 11
>> A=20
=> 20
>> A=A+1
(irb):4: warning: already initialized constant A
=> 21
>> a
=> 11
>> A
=> 21
>>

While trying this, I tried with $A. But it was global variable. Is there global constant?

1:25 PM  
Blogger SatishTalim said...

Anish, I have answered your question 2. in my blog post. As regards scope of variables/constants, we shall discuss this later.

2:11 PM  

Post a Comment

Links to this post:

Create a Link

<< Home