Keywords
Def undef method definition
Module Definition
Class Definition
Defined? Check type
Conditional statement if, then, else, elsif, case, when, unless
Loop statements for, in, while, until, next, break, do, redo, retry, yield
Logical judgment not, and, or
Logical or null values: true, false, nil
Exception Handling rescue, ensure
Reference super, self
Embedded Module BEGIN END
Start/end of a block
FILE-related _ FILE _ LINE _
Return Method
Alias
Symbol
; Used to separate multiple statements
() Include the parameter list when defining a method
, Separate multiple parameters
. Separate the object from its method.
: The symbol used by the domain to separate the (module) class from its constant.
Operator
[] [] = Array subscript, array assignment
** Chengming
!~ +-Non, non-bit, one dollar plus negative number
*/% Multiplication and division
+-Subtraction
>>< Shift right to left
& Bit and
^ | Bitwise XOR or
<=<>>= Less than or equal to less than or greater than and
<==>======== ~ ! = !~ Equality judgment
& Short circuit and
| Short circuit or
The start and end of the... area, and the second does not include the end.
? : Ternary conditional Operator
Global variables start with $. They can be accessed anywhere in the program. Before initialization, the global variables have a special value nil.
Special variables starting with $ and with a single character are listed here. for example, $ contains the process id of the Ruby interpreter, which is read-only. here are the main system variables and their meanings (details can be found in the Ruby reference manual ):
$! Last error message
$ @ Error location
$ _ Gets recently read string
$. Number of lines recently read by the interpreter (line number)
$ & The latest string matching the Regular Expression
$ ~ Last match as a child expression group
$ N the nth subexpression (and $ ~ [N] Same)
$ = Case-insensitive flag
$/Input record Separator
$ \ Output record Separator
$0 Ruby script file name
$ * Command line parameters
$ Interpreter process ID
$? The last sub-process exited
= Begin
Example: win32api operations
= End
Require 'dl'
User32 = DL. dlopen ("user32 ")
MB_OKCANCEL = 1
Message_box = User32 ['messageboxa ', 'ilssi']
R, rs = message_box.call (0, 'OK? ', 'Please confirm', MB_OKCANCEL)
Case r
When 1
Print ("OK! \ N ")
When 2
Print ("Cancel! \ N ")
End
System ($ * [0]) # command line receiving parameter $ * [0] global variable
File. open ('C: \ boot. ini '). ecah do | line |; puts line; end # Read all content
Arr = File. readlines ('C: \ boot. ini '); puts arr [0]) # Read-Only first line
File. open ('C: \ boot. ini ') do | f1 | # Another method, read-only the first line
While line = f1.gets
Puts line
Break
End
End
# Writing files
File. new ('C: \ 1.txt ', "w"). puts 'message'