Ruby programming language _ chapter01 _ note

Source: Internet
Author: User
Tags array sort case statement
Chapter 1 Introduction

  1. Ruby is fully object-oriented.

  2. Ruby annotations start.

  3. When Ruby calls a method without parameters, brackets are usually omitted, and Ruby can only access the internal state of the object through methods.

  4. Code block and iterator
    1) times and upto are implementation methods of integer objects. They are called "iterator" and are similar to loops. The Code contained in "{}" becomes a block ).

    3.times {print "Ruby! "}1.upto(9) {|n| print n}

    2) An Array (and similar "enumerable" objects) has an iterator named each, which calls an associated code block for each element at a time; each call to a code block uses this element as a parameter.

    a = [1, 2, 3]a.each do |n|    print n ** 2end

    3) Other iterators

    • MAP is used to traverse elements in the array, perform operations in the code block, and place the return value of the code block in the new array returned by map.
    • Select is used to traverse the elements in the array and perform operations in the code block. If true is returned, the element is placed in the new array returned by select.
    a = [1, 2, 3, 4]b = a.map {|n| n ** 2}# b = [1, 4, 9, 16]c = a.map {|n| print n}# c = [nil, nil, nil, nil]d = a.select {|n| n % 2 == 0}# d = [2, 4]e = a.select {"k"}# e = [1, 2, 3, 4]f = a.select {|n| print n}# f = []

    Both inject and inject

    4) hash is also the basic data structure of Ruby. Similar to arrays, "[]" is used to access element values, but hash is used as an index for key objects. The hash class also defines the each iteration method. This method calls a code block for each key-Value Pair and passes the key and value as parameters to the code block.
    Hash can use any object as the key, but the symbol object is the most commonly used.

    k = "key 01"    h = {    :one => 1,    :two => 2,    k => "Value 01"}        h[:one] = "ONE"p "h[k]=> '#{h[k]}'"        h.each do |k, v|    p "#{k}: #{v}" end

  5. String interpolation: Double quotation marks can contain any Ruby expressions, but these expressions must be included in the delimiters # {And. The expression value is converted to a string by using the to_s method instead of the original expression.

    P "# {true}" # print "true" p "# {false}" # print "false" p "# {nil}" # print ""

  6. Expressions and operators
    1) Ruby syntax is expression-oriented, and the control structure of the IF class is actually an expression and has a value. It is very similar to the three-object operator.

    m = if 2 > 1 then "y" else "n" end

    2) Although all Ruby statements are expressions, not all statements return meaningful values. For example, while loop and method definition usually only return nil.

    3) Many Ruby operators are implemented as methods, and classes can be defined from them. However, new operators cannot be defined, because the identifiable operators are a fixed set.

    "Ruby! " * 3# "Ruby! Ruby! Ruby! ""Hello, %s! -- %d" % ["Ruby", 2011]# "Hello, Ruby! -- 2011"[1,2,3] << 5# [1, 2, 3, 5]"Hello, " << "Wolrd!"# "Hello, Wolrd!"

    4) [] is one of the most powerful operators that can be rewritten. The [] operator of the array class can accept two parameters to obtain the sub-array or fragment of the array.

  7. Method
    1) def definition, returns the value of the last expression

    def cube(x)    x ** 3end

    2) If it is defined outside the class or module, it is a global function rather than a method. Global functions can be considered as private methods of object classes.

    3) You can define a method for a class or object by prefix the class name or object name.

    a = "Solana"def a.say()    p "I'm Solana!"enda.say

  8. Assignment
    1) Parallel assignment

    X, Y, Z = 1, 3, 5 # x = 1; y = 3; Z = 5x, y = Y, X # exchange value a, B, c = [2, 4, 6] # A = 2; B = 4; C = 6

    2) The method can have multiple return values, and it is convenient to assign values in parallel.

    def calc(x)    [x**2, x**3]enda, b = calc(7)p a, b

    3) For Methods ending with equal signs, Ruby allows them to be called using the syntax of the value assignment operation.

    o.x=(1)o.x = 1

  9. Prefix and suffix punctuation marks
    1) Suffix
    Suffix? Return a Boolean value. For example, the empty of array? Method.
    No! Returns a modified copy of the object that calls this method; suffix! Is a variable method, you can modify the original object. For example, array sort and sort! Method.

    a = []b = [4,7,2,3,9,1]c = bp a.empty?b.sortp bb.sort!p c

    2) prefix $ global variable
    @ Instance variable
    @ Class variable

  10. Both Regexp and range have literal values. They both define the = Operator for Equality testing, and both define the = Operator for matching and property testing.

    x = 3p case x    when 0...3: "0 <= x < 3"    when 3...6: "3 <= x < 6"    when 6..9: "6 <= x <= 9"    else "others"endusrname = "Gengv2007"p case usrname    when /^[a-z]/        "Started with lowercase letter."    when /^[A-Z]/        "Started with uppercase letter."    when /\d$/        "Started with number."end

    The case statement of ruby looks "not straight down.

  11. A class is a collection of related methods. These methods operate on the state of an object, and the state of an object is saved in its instance variable (a variable starting ).

  12. The ruby string is variable. The [] = operator can change the characters in the string, or insert, delete, or replace the substring. <operator can append content after the string.

  13. The string is variable, so the recycle contains a string literal, and a new object is created for it in each loop. By calling the freeze method on a string (or any other object), you can avoid any changes to this object.

  14. The judgment expressions of conditions and loops (such as if and while) do not have to be true or false; NIL is treated as false, and any other values (such as 0 or "") are treated as true.

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.