Ruby programming language _ chapter02 _ notes

Source: Internet
Author: User
Tags autoload chop shebang
Chapter 2 Structure and running of Ruby programs

  1. The ruby annotation starts with # And ends until the end of the line. Ruby does not have/*... */C style annotations.

  2. If # appears in the string or regular expression literal, it is a part of a string or regular expression, rather than a comment.

  3. 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.

  4. Docized comments

  5. The literal is the value that appears directly in the ruby source code, including numbers, strings, regular expressions, arrays, and hash values.

  6. 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 ".

  7. Ruby is case sensitive.

  8. 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 ".

  9. 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.

  10. 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 __    

  11. 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.

  12. 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.

  13. 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  
  14. 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.

  15. 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

  16. 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.

  17. 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.

  18. 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.

  19. If you are using a UTF-8, it is best to choose the non-bom mode when saving in windows.

  20. 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 ".

  21. 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 ".

  22. 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.

     

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.