Ruby变量
i0 = 1 loop { i1 = 2 puts defined?(i0) # true; "i0" was initialized in the ascendant block puts defined?(i1) # true; "i1" was initialized in this block break } puts defined?(i0) # true; "i0 was initialized in this block puts defined?(i1) # false; "i1" was initialized in the loop
#!/usr/bin/ruby class Customer def initialize(id, name, addr) @cust_id=id @cust_name=name @cust_addr=addr end def display_details() puts "Customer id #@cust_id" puts "Customer name #@cust_name" puts "Customer address #@cust_addr" end end # Create Objects cust1=Customer.new("1", "John", "Wisdom Apartments, Ludhiya") cust2=Customer.new("2", "Poul", "New Empire road, Khandala") # Call Methods cust1.display_details() cust2.display_details() |
#!/usr/bin/ruby class Customer @@no_of_customers=0 def initialize(id, name, addr) @cust_id=id @cust_name=name @cust_addr=addr end def display_details() puts "Customer id #@cust_id" puts "Customer name #@cust_name" puts "Customer address #@cust_addr" end def total_no_of_customers() @@no_of_customers += 1 puts "Total number of customers: #@@no_of_customers" end end # Create Objects cust1=Customer.new("1", "John", "Wisdom Apartments, Ludhiya") cust2=Customer.new("2", "Poul", "New Empire road, Khandala") # Call Methods cust1.total_no_of_customers() cust2.total_no_of_customers() |
Here @@no_of_customers is a class variable. This will produce following result:
Total number of customers: 1 Total number of customers: 2 |
self
- Execution context of the current method.
nil
-
The sole-instance of the
NilClass
class. Expresses nothing.
true
-
The sole-instance of the
TrueClass
class. Expresses true.
false
-
The sole-instance of the
FalseClass
class. Expresses false. ( nil also is considered to be false, and every other value is considered to be true in Ruby.)
The value of a pseudo variable cannot be changed. Substitution to a pseudo variable causes an exception to be raised.
$! 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.
This infestation of cryptic two-character $? expressions is a thing that people will frequently complain about, dismissing Ruby as just another perl-ish line-noise language. Keep this chart handy. Note, a lot of these are useful when working with regexp code.
Note that there are some pre-defined constants at parse time, as well, namely
__FILE__ (current file)
and
__LINE__ (current line)
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:
#!/usr/bin/ruby ary = [ "fred", 10, 3.14, "This is a string", "last element", ] ary.each do |i| puts i end |
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:
#!/usr/bin/ruby hsh = colors = { "red" => 0xf00, "green" => 0x0f0, "blue" => 0x00f } hsh.each do |key, value| print key, " is ", value, "/n" end |