The ruby annotation starts with # And ends until the end of the line. Ruby does not have/*... */C style annotations.
If # appears in the string or regular expression literal, it is a part of a string or regular expression, rather than a comment.
Ruby supports embedded annotations, starting with = begin and ending with = end. = There must be at least one space between begin and the content to be annotated as the separator. This is also true for = end, and the line where = end is located is commented out. = It must appear as the first character of the row. Leading spaces or tabs cannot exist.
Docized comments
The literal is the value that appears directly in the ruby source code, including numbers, strings, regular expressions, arrays, and hash values.
The identifier is the name. Ruby uses identifiers to name variables, classes, and methods.
1) it may consist of letters, numbers, and underscores, but cannot begin with a number.
2) an identifier starting with an uppercase letter is a constant. If a similar identifier is modified, a warning is issued, but not an error.
3) the class and module name must start with an uppercase letter. Otherwise, an error is returned.
4) Multi-byte identifiers. The following lines are separated, rather than the common "Hump naming" in Java ".
Ruby is case sensitive.
In the ASCII character set, Ruby specifies which characters are valid and which are invalid. All characters other than the ASCII character set are valid characters of the Ruby identifier. For example, the multiplication number "X" in the UTF-8 ".
Punctuation in the identifier
1) The variable starting with $ is a global variable;
2) variables starting with @ are instance variables;
3) variables starting with @ are class variables;
4 )? The name of the method that returns a Boolean value usually ends with a question mark;
5 )! The method name at the end, indicating that this method will change the object itself that calls it;
6) = if the method name ends with "=", this number can be omitted when this method is called. This method is usually placed on the left side of the value assignment operator.
Reserved Words
_ Line __ |
Case |
Ensure |
Not |
Then |
_ Encoding __ |
Class |
False |
Or |
True |
_ File __ |
Def |
For |
Redo |
UNDEF |
Begin |
Defined? |
If |
Rescue |
Unless |
End |
Do |
In |
Retry |
Until |
Alias |
Else |
Module |
Return |
When |
And |
Elseif |
Next |
Self |
While |
Begin |
End |
Nil |
Super |
Yield |
Break |
|
|
|
|
= Begin |
= End |
_ End __ |
|
|
If _ end _ appears on a single row (no leading or trailing blank), the end of the program is indicated, that is, the beginning of the Data zone.
You can add @, @, and $ before reserved words as instance variables, class variables, and global variables. In addition, you can use keywords as method names, but such method names cannot appear in programs separately and must be called by displaying an object.
Many features of Ruby are actually implemented using methods of the kernel, module, class, and object classes. We recommend that you use the following identifier as a reserved word.
1) these methods are usually used for declaration or keywords.
At_exit |
Catch |
Private |
Require |
ATTR |
Include |
Proc |
Throw |
Attr_accessor |
Lambda |
Protected |
|
Attr_reader |
Load |
Public |
|
Attr_writer |
Loop |
Raise |
|
2) These methods are usually used for global methods.
Array |
Chomp! |
Gsub! |
Select |
Float |
Chop |
Iterator? |
Sleep |
Integer |
Chop! |
Load |
Split |
String |
Eval |
Open |
Sprintf |
Uri |
Exec |
P |
Srand |
Abort |
Exit |
Print |
Sub |
Autoload |
Exit! |
Printf |
Sub! |
Autoload! |
Fail |
Putc |
Syscall |
Binding |
Fork |
Puts |
System |
Block_given? |
Format |
Rand |
Test |
Callcc |
GETC |
Readline |
Trap |
Caller |
Gets |
Readlines |
Warn |
Chomp |
Gsub |
Scan |
|
3) these methods are usually used for object methods.
Allocate |
Freeze |
Kind_of? |
Superclass |
Clone |
Frozen? |
Method |
Taint |
Display |
Hash |
Methods |
Tainted? |
DUP |
ID |
New |
To_a |
Enum_for |
Inherited |
Nil? |
To_enum |
Eql? |
Inspect |
Object_id |
To_s |
Equal? |
Instance_of? |
Respond_to? |
Untaint |
Extend |
Is_a? |
Send |
|
Blank Space Character
1) You can use semicolons in Ruby to conclude the sentence, but it is usually omitted. If omitted, the linefeed is regarded as the end of the statement.
2) If the statement is distributed across multiple rows, pay attention to the location of the line break.
total = x + y total = x+ y
3) do not worry about the Ambiguity Caused by line breaks in the following situations. After the operator, after the period of the method call, after the comma of the elements separated by the number of word faces in the array or hash table.
4) You can add "\" to escape the line break to avoid ending the statement.
5) in ruby1.9, if the first non-blank character of a line of code is ".", this row is considered to be a continuation of the previous line. This style is very consistent with the chain call.
6) Ruby allows omitting brackets in method calls, but note the spaces between method names and parameters.
f(3+2)+1# equals to f(5) + 1f (3+2)+1# equals to f(6)
Therefore, do not leave it blank between the method name and left brace. If the parameter starts with parentheses, parentheses are always used for the entire method call. It is best to always use the-W option of the Ruby interpreter.
A block is a piece of code related to the iterator, separated by curly brackets, do, and end. (When the Block Code contains more than one line, do and end are used to separate them)
3.times {print "Ruby! "} 3.times do |x| print xend
Body is a list of statements, such as the class definition body and the method body loop body. Objects are separated by curly brackets instead of keywords.
File structure
1) if the comment contains "shebang", the comment must appear in the first line of the file;
2) If the comment contains "coding", it should appear in the line below "shebang"; if there is no "shebang", it should appear in the first line;
3) if the file contains a line of code, and the line of code only contains the _ end _ mark, and there are no blank characters before and after the mark, the Ruby interpreter stops processing the file. The rest of the file can contain any data, and the program can read it through the IO Stream object data.
Ruby supports Unicode and other character set encoding. In 1.8, the-ku option is required. In 1.9, you only need to add the following annotations at the beginning of the file (This annotation must be written in ASCII characters ):
# coding: utf-8
1) After coding, a colon or equal sign must be followed, followed by a character encoding name. The name cannot contain spaces or underlines or punctuation marks other than hyphens;
2) There can be spaces on both sides of the colon and equal sign, and any prefix can be included before the string coding;
3) The entire comment is case insensitive.
If you are using a UTF-8, it is best to choose the non-bom mode when saving in windows.
In Windows's command line, to correctly display the UTF-8 output, you should execute the command "chcp 65001" and change the font to "lucida console ".
Jruby 1.6.x is compatible with ruby1.8 by default. To be compatible with 1.9, you need to set the system variable "jruby_opts = -- 1.9 ".
Normally (unless exit is called! Method), Ruby will execute all the end statements before exiting, and any "Close hook" code registered with the at_exit function.