Check Order status | Verified Sales | Escrow Service | Advertise
westernunion carding gold carding
revolut carding electronics carding
paypal carding payoneer carding
advertise on cracking forums

Ruby Variables, Constants and Literals

Prince

[ Verified Seller ]
Staff member
Trusted Seller
Joined
10 yrs. 6 mth. 11 days
Messages
5,386
Reaction score
18,373
Age
44
Wallet
11,590$
Ruby Variables

Variables are the memory locations which holds any data to be used by any program.
There are five types of variables supported by Ruby. You already have gone through a small description of these variables in previous chapter as well. These five types of variables are explained in this chapter.
Ruby Global Variables:

Global variables begin with $. Uninitialized global variables have the value nil and produce warnings with the -w option.
Assignment to global variables alters global status. It is not recommended to use global variables. They make programs cryptic.
Here is an example showing usage of global variable.

Code:
Please, Log in or Register to view codes content!
Here $global_variable is a global variable. This will produce following result:
NOTE: In Ruby you CAN access value of any variable or constant by putting a hash (#) character just before that variable or constant.
Code:
Please, Log in or Register to view codes content!
Ruby Instance Variables:

Instance variables begin with @. Uninitialized instance variables have the value nil and produce warnings with the -w option.
Here is an example showing usage of Instance Variables.

Code:
Please, Log in or Register to view codes content!
Here @cust_id, @cust_name and @cust_addr are instance variables. This will produce following result:
Code:
Please, Log in or Register to view codes content!

Ruby Class Variables:

Class variables begin with @@ and must be initialized before they can be used in method definitions.
Referencing an uninitialized class variable produces an error. Class variables are shared among descendants of the class or module in which the class variables are defined.
Overriding class variables produce warnings with the -w option.
Here is an example showing usage of class variable:

Code:
Please, Log in or Register to view codes content!
Here @@no_of_customers is a class variable. This will produce following result:
Code:
Please, Log in or Register to view codes content!
Ruby Local Variables:

Local variables begin with a lowercase letter or _. The scope of a local variable ranges from class, module, def, or do to the corresponding end or from a block's opening brace to its close brace {}.
When an uninitialized local variable is referenced, it is interpreted as a call to a method that has no arguments.
Assignment to uninitialized local variables also serves as variable declaration. The variables start to exist until the end of the current scope is reached. The lifetime of local variables is determined when Ruby parses the program.
In the above example local variables are id, name and addr.

Ruby Constants

Constants begin with an uppercase letter. Constants defined within a class or module can be accessed from within that class or module, and those defined outside a class or module can be accessed globally.
Constants may not be defined within methods. Referencing an uninitialized constant produces an error. Making an assignment to a constant that is already initialized produces a warning.

Code:
Please, Log in or Register to view codes content!
Here VAR1 and VAR2 are constant. This will produce following result:

Code:
Please, Log in or Register to view codes content!
Ruby Pseudo-Variables:

They are special variables that have the appearance of local variables but behave like constants. You can not assign any value to these variables.
self: The receiver object of the current method.

true: Value representing true.

false: Value representing false.

nil: Value representing undefined.

__FILE__: The name of the current source file.

__LINE__: The current line number in the source file.

Ruby Basic Literals

The rules Ruby uses for literals are simple and intuitive. This section explains all basic Ruby Literals.

Integer Numbers:

Ruby supports integer numbers. An integer number can range from -230 to 230-1 or -262 to 262-1. Integers with-in this range are objects of class Fixnum and integers outside this range are stored in objects of class Bignum.
You write integers using an optional leading sign, an optional base indicator (0 for octal, 0x for hex, or 0b for binary), followed by a string of digits in the appropriate base. Underscore characters are ignored in the digit string.
You can also get the integer value corresponding to an ASCII character or escape sequence by preceding it with a question mark.
Example:

Code:
Please, Log in or Register to view codes content!
NOTE: Class and Objects are explained in a separate chapter of this tutorial.

Floating Numbers:

Ruby supports integer numbers. They are also numbers but with decimals. Floating-point numbers are objects of class Float and can be any of the following:
Example:
Code:
Please, Log in or Register to view codes content!

String Literals:

Ruby strings are simply sequences of 8-bit bytes and they are objects of class String. Double-quoted strings allow substitution and backslash notation but single-quoted strings don't allow substitution and allow backslash notation only for \\ and \'
Example:
Code:
Please, Log in or Register to view codes content!
This will produce following result:
Code:
Please, Log in or Register to view codes content!
That's right
You can substitute the value of any Ruby expression into a string using the sequence #{ expr }. Here expr could be any ruby expression.
Code:
Please, Log in or Register to view codes content!

Backslash Notations:

Following is the list of Backslash notations supported by Ruby: Notation Character represented \n Newline (0x0a) \r Carriage return (0x0d) \f Formfeed (0x0c) \b Backspace (0x08) \a Bell (0x07) \e Escape (0x1b) \s Space (0x20) \nnn Octal notation (n being 0-7) \xnn Hexadecimal notation (n being 0-9, a-f, or A-F) \cx, \C-x Control-x \M-x Meta-x (c | 0x80) \M-\C-x Meta-Control-x \x Character x
Ruby Arrays:

Literals of Ruby Array are created by placing a comma-separated series of object references between square brackets. A trailing comma is ignored.
Example:

Code:
Please, Log in or Register to view codes content!
Ruby Hashes:

A literal Ruby Hash is created by placing a list of key/value pairs between braces, with either a comma or the sequence => between the key and the value. A trailing comma is ignored.
Example:

Code:
Please, Log in or Register to view codes content!
 
Top Bottom