Ruby on Rails(RoR) 3 Variables
Variable of Ruby on Rails is as follows
Instance Variables
Class Variables
Global Variables
Constants
Pseudo Variables
Pre-defined Variables
default the values of the pre-defined variables will be nil
Pre-defined constants
- local variables
- constants
- instance variable(object variables) .
- class variables
- global variables
Local Variables
- A variable whose name begins with a lowercase letter (a-z) or underscore ( _ ) .
Example
distance
Instance Variables
- A variable whose name begins with '@' is an instance variable of self.
- Uninitialized instance variables have a value of nil
Example
@distance
Class Variables
- A variable whose name begins with '@@'
- A class variable is shared by all instances of a class
- class variable is shared by all the descendants of the class
Example
@@distance
Global Variables
- A variable whose name begins with '$'
- can be accessed from anywhere within the program during runtime
Example
$distance
Constants
- A variable whose name begins with an uppercase letter (A-Z)
- A constant can be reassigned a value after its initialization, but doing so will generate a warning
- Every class is a constant
- Trying to access an uninitialized constant raises the NameError exception.
Example
DISTANCE
Pseudo Variables
- self
- nil
- true
- false
Pre-defined Variables
| $! | The exception information message set by the last 'raise' (last exception thrown). |
| $@ | Array of the backtrace of the last exception thrown. |
| $& | The string matched by the last successful pattern match in this scope. |
| $` | The string to the left of the last successful match. |
| $' | The string to the right of the last successful match. |
| $+ | The last bracket matched by the last successful match. |
| $1 to $9 | The Nth group of the last successful regexp match. |
| $~ | The information about the last match in the current scope. |
| $= | The flag for case insensitive, nil by default (deprecated). |
| $/ | The input record separator, newline by default. |
| $\ | The output record separator for the print and IO#write. Default is nil. |
| $, | The output field separator for the print and Array#join. |
| $; | The default separator for String#split. |
| $. | The current input line number of the last file that was read. |
| $< | The virtual concatenation file of the files given on command line. |
| $> | The default output for print, printf. $stdout by default. |
| $_ | The last input line of string by gets or readline. |
| $0 | Contains the name of the script being executed. May be assignable. |
| $* | Command line arguments given for the script. Same as ARGV. |
| $$ | The process number of the Ruby running this script. Same as Process.pid. |
| $? | The status of the last executed child process. |
| $: | Load path for scripts and binary modules by load or require. |
| $" | The array contains the module names loaded by require. |
| $LOADED_FEATURES | An english friendlier alias to $" |
| $DEBUG | The status of the -d switch. Assignable. |
| $FILENAME | Current input file from $<. Same as $<.filename. |
| $KCODE | Character encoding of the source code. |
| $LOAD_PATH | An alias to $: |
| $stderr | The current standard error output. |
| $stdin | The current standard input. |
| $stdout | The current standard output. |
| $VERBOSE | The verbose flag, which is set by the -v switch. |
| $-0 | The alias to $/ |
| $-a | True if option -a ("autosplit" mode) is set. Read-only variable. |
| $-d | The alias to $DEBUG. |
| $-F | The alias to $; |
| $-i | If in-place-edit mode is set, this variable holds the extension, otherwise nil. |
| $-I | The alias to $: |
| $-K | The alias to $KCODE. |
| $-l | True if option -l is set ("line-ending processing" is on). Read-only variable. |
| $-p | True if option -p is set ("loop" mode is on). Read-only variable. |
| $-v | The alias to $VERBOSE. |
| $-w | True if option -w is set. |
default the values of the pre-defined variables will be nil
Pre-defined constants
__FILE__ (current file) __LINE__ (current line)
Comments
Post a Comment