|  | | |  |  |
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.
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.). - A local variable (declared within an object) name consists of a lowercase letter followed by name characters (sunil, _z, hit_and_run).
- An instance variable name starts with an ''at'' sign (''@'') followed by an upper- or lowercase letter, optionally followed by name characters (@sign, @_, @Counter).
- A class variable (declared within a class) name starts with two ''at'' signs (''@@'') followed by an upper- or lowercase letter, optionally followed by name characters (@@sign, @@_, @@Counter).
- 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).
Constants 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).
Method Names Method names should begin with a lowercase letter. ''?'' and ''!'' are the only weird characters allowed as method name suffixes (! or bang labels a method as dangerous-specifically, as the dangerous equivalent of a method with the same name but without the bang).
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. | An example to show Ruby is dynamically typed - p007dt.rb
| # Ruby is dynamic x = 7 # integer x = "house" # string x = 7.5 # real | The basic types in Ruby are Numeric (subtypes include Fixnum, Integer, and Float), String, Array, Hash, Object, Symbol, Range, and RegEx. Ruby doesn't require you to use primitives (data types) when manipulating data of these types-if it looks like an integer, it's probably an integer; if it looks like a string, it's probably a string. class returns the class of an object, for example:
| s = 'hello' s.class # String | In Ruby, everything you manipulate is an object, and the results of those manipulations are themselves objects. There are no primitives or data-types.
| 5.times { puts "Mice!\n" } # more on blocks later "Elephants Like Peanuts".length |
| |  | | | |