Ruby standard type

Source: Internet
Author: User
Tags character classes delete key

Ruby standard type

 

I. Numbers
  1. Ruby supports integers and floating-point numbers. integers can be of any length.
  2. Integers within a certain range are stored in binary. They belong to the fixnum type. If they exceed this range, they are automatically converted to the bignum type.
  3. Expression: the symbol + a string of characters. The underlines in the digit string are ignored. (prefix: 0 indicates octal, 0x indicates hexadecimal, and 0b indicates binary.) 123_456_789_123_345_789 # bignum
    0 xaabb # hexadecimal
  4. You can also add a question mark to get the integer corresponding to the ASCII character and the value of the escape sequence.
    ? A # common characters
    ? /N # linefeed (0x0a)
    ? /C-A # Ctrl + a (0x01)
    ? /M-A # Alt +
    ? /M-/C-A # CTRL + ALT +
    ? /C -? # Delete key
  5. A numeric literal with a decimal point is converted to a float object.
  6. All numbers are objects and there is no corresponding function but a method.
    Exp:
    The absolute value of a number is anumber. Abs instead of ABS (anumber)
  7. Integer useful iterator
    3. times {print "X" }=> x 1. upto (5) {| I | print I, "" }=> 1 2 3 4 5 99. downto (95) {| I | print I, ""} => 99 98 97 96 95 50. STEP (80, 5) {| I | print I, "" }=> 50 55 60 65 70 75 80
Ii. String
  1. The ruby string is a simple 8-byte sequence, and the string is a string object.
  2. Note the conversion mechanism (note the difference between single quotes and double quotes), for example:
    The two backslashes connected to each other in single quotes are replaced with a backslashes. A backslashes followed by a single quotation mark are replaced with a single quotation mark.
    'Escape using "//" '> escape to "/"' That/'s right '> that's right
  3. Double quotation marks support multi-meaning escape
    "/N"
    # {Expr} sequence to replace the values of any Ruby expressions. (braces can be omitted for global variables, class variables, or instance variables)
    "Seconds/day: # {24*60*60}"> seconds/day: 86400 "# {'ho! '* 3} Merry Christmas "> Ho! Ho! Ho! Merry Christmas "this is line # $."> This is line 3
  4. Here document to create a string, end_of_string is the end symbol
    Astring = <end_of_string the body of the string is the input lines up to one ending with the same text that followed the '<'end_of_string
  5. % Q and % Q separate the strings into single quotes and double quotation marks respectively (I .e., the symbols after % Q and % Q have the ', "function)
    % Q/general single-quoted string/> general single-quoted string
  6. Common functions of string
    String # split: splits rows into fields.
    String # chomp: Remove line breaks
    String # squeeze: removes the characters that have been repeatedly entered.
    String # scan: Specify the pattern for block matching
    Exp:
    /Jazz/j00132.mp3 | Fats Waller | ain't misbehavin'
    /Jazz/j00319133 | Louis Armstrong | wonderful world
    # The file format is as follows:
    Songs = songlist. New
    Songfile. Each do | Line |
    File, length, name, title = line. Chomp. Split (// s */|/S */) # first chomp, then decomposed, // s * indicates any character
    Name. Squeeze! ("") # Replace Spaces
    Mins, secs = length. Scan (// D +/) # scan matching mode is used here
    Songs. append song. New (title, name, Mins. to_ I * 60 + secs. to_ I)
    End
III, Interval
  1. The interval exists anywhere, for example, from 1 to December. Ruby uses intervals to implement three different features: sequence, condition, and interval.
  2. "...": Create a closed interval for the two dots, "...": create a right-open interval for the three dots (that is, the right boundary is not valid)
    Exp: 0... anarray. Length-1 is equivalent to 0... anarray. Length
  3. To_a converts an interval to a list.
    Exp: ('bar'... 'bat '). to_a> ["bar", "Bas", "Bat"]
  4. Common interval usage
    Digits = 0 .. 9
    Digits. Include? (5)> true
    Digits. min> 0
    Digits. max> 9
    Digits. Reject {| I <5}> [5, 6, 7, 8, 9]
    Digits. Each do | digit |
    Dial (DIGIT)
    End
  5. Ruby can return the interval of an object defined by itself. This object must be able to respond to the succ method to return the next object in the sequence, and this object must be able to be compared using the <=> operator, that is, regular comparison operators,
  6. Interval Test
    Puts (1 .. 10). Include? (3.14) => ture
    Puts (1 .. 10) === 3.14 => ture
Iv. Regular Expressions
  1. The regular expression is a Regexp type object. You can use the constructor to explicitly create a regular expression, or use the literal value form/pattern/and % R/pattern/to create a regular expression.
  2. Regxp # match (astring) format or matching operator = ~ (Positive match) and !~ (Negative match) to match the string. The matching operator is defined in both string and Regexp. If both operands are strings, the one on the right will be converted to a regular expression.
    Exp:
    A = "Fats Waller"
    A = ~ /A/> 1
    A = ~ /Z/> Nil
    A = ~ "Ll"> 7
  3. The above returns the position of the matching character.
    $ & Accept the string part matched by the pattern
    $ 'Accept the string part before matching
    $ 'Accept the string.
    Exp: The following methods will be used later.
    Def showre (A, RE)
    If a = ~ Re
    "#{$ '}<#{$ & }>#{$'}" # Returns the front, center, and back
    Else
    "No match"
    End
    End
  4. Mode. Any expression contains a pattern used to match a regular expression with any string.
    In the mode, except for., |, (,), [, {, +,/, ^, $, *, and? All other words match themselves.
    If you want to match these special characters, you need to add the backslash as the prefix to analyze the above example.
    // S */|/S */. prefix/is added before/s and |.
    Showre ('hangaroo ',/Angar/)> K <Angar> oo
    Showre ('! @ % &-_ = + ',/% &/)>! @ <% &>-_ = +
    Showre ('yes | no', // |/)> Yes <|> NO
  5. /Is followed by a letter or number to indicate a specific structure, such as/s to indicate characters.
  6. A regular expression of the anchor always returns the first matching of the pattern. How can this change?
    Modes ^ and $ are used to match the beginning and end of a row.
    Sequence/A matches the start position of the string, And/Z matches the end position of the string.
    /B and/B match the word boundary and non-word boundary respectively.
    Showre ("This Is/nthe time",/^ The/)> This is/n <The> time
    Showre ("This Is/nthe time",/is $/)> This <is>/nthe time
    Showre ("This Is/nthe time", // athis/) >>< <This> is/nthe time

V. character classes
  1. The character class here is not an object-oriented class. It only indicates that these characters belong to a special type.
  2. A character class is a set of characters expanded with square brackets: [characters] matches all single characters in square brackets. [Aeiou] matches the vowel, [,.: '!?] Match punctuation marks, etc.
    Showre ('It costs $12. ',/[aeiou]/)> it C <o> STS $12.
  3. The sequence c1-c2 in square brackets represents all characters between the c1-c2 that also contain C1 and C2
    A = 'gamma [design patterns-page 123]'
    Showre (A,/[]/)> gamma [design patterns-page 123 <]>
    Showre (A,/[B-F]/)> gamma [<D> esign patterns-page 123]
    Showre (A,/[-]/)> gamma [design patterns <-> page 123]
    Showre (A,/[0-9]/)> gamma [design patterns-page <1> 23]
  4. The character ^ is followed by the opening brackets ([), which indicates the negation of this character class: [^ A-Z] matches any character that is not a lowercase letter.
  5. Abbreviations
    The sequence is shown in [...] Meaning
    /D [0-9] digit character
    /D [^ 0-9] nondigit
    /S [/S/T/R/n/f] whitespace character matches a single blank character
    /S [^/S/T/R/n/f] nonwhitespace character
    /W [A-Za-z0-9 _] word character
    /W [^ A-Za-z0-9 _] nonword character
  6. Repeated
    R * matches 0 or multiple R records.
    The appearance of R + matching one or more R
    R? Occurrence of matching 0 or 1 R
    The appearance of r {m, n} matching at least m up to n r
    The appearance of r {M,} matching at least M r

    The repeat structure has a high priority: that is, they are only bundled with the forward Regular Expression in the pattern.
    /AB +/match a "A" followed by a "B", not a "AB" sequence.
    /A */will match any string: 0 or multiple "A" strings.
    Exp:
    A = "the moon is made of cheese"
    Showre (A, // W +/) >>< <The> moon is made of cheese
    Showre (A, // S. */S/)> the <moon is made of> cheese
    Showre (A, // S .*? /S/)> the <moon> is made of cheese
    Showre (A,/[aeiou] {2, 99}/)> the m <oo> N is made of cheese
    Showre (A,/Mo? O/)> the <Moo> N is made of cheese

  7. Replace
    "|" Matches the regular expression or
    A = "red ball blue sky"
    Showre (A,/d | E/)> r <E> D ball blue sky
    Showre (A,/Al | lu/)> Red B <Al> L blue sky
    Showre (A,/red ball | angry sky/) >>< red ball> blue sky
  8. Group
    Parentheses group regular expressions. The content in the group is treated as a separate regular expression.
    Showre ('banana ',/(an) +/)> B <Anan>
    # Matching duplicate letters
    Showre ('he said "hello" ',/(/W)/1/)> he said "He <ll> O"
    # Matching repeated substrings
    Showre ('Mississippi ',/(/W +)/1/)> m <ississ> IPPI
  9. Mode-based replacement
    Have you ever thought about replacing the case with the upper or lower case.
    Fang
    Both string # sub and string # gsub search for the part matching the first parameter in the string, and then replace them with the second parameter. String # Replace sub only once,
    String # gsub replaces all matched items. Returns a copy of the new string that contains the replacement. The evolutionary version is string # sub! And string # gsub!
    A = "The quick brown fox"
    A. Sub (/[aeiou]/, '*')> "th * quick brown fox"
    A. gsub (/[aeiou]/, '*')> "th * q ** CK Br * wn f * X"
    A. Sub (// S/S +/, '')>" the brown fox"
    A. gsub (// S/S +/, '')>""
    The second parameter can be a code block.
    A = "The quick brown fox"
    A. Sub (/^./) {$ &. upcase}> "The quick brown fox"
    A. gsub (/[aeiou]/) {$ &. upcase}> "The quick brown fox"
  10. The backslash sequence is used in replacement.
    /& Match
    /+ Matching Group
    /'Match the previous string
    /'Match the following string
    // The Literal Value of the backslash.
  11. Object-oriented Regular Expressions
    Create Regexp class for the literal value of a regular expression
    Re =/CAT/
    Re. type> Regexp
    Method Regexp # match matches a regular expression from the string. If it fails, the method returns nil. If it succeeds, an instance of the matchdata class is returned.
    Exp:
    E =/(/d +) :(/d +)/# match a time hh: mm
    MD = Re. Match ("Time: 12: 34 AM ")
    Md. type> matchdata
    MD [0] #== &>"
    MD [1] #==$ 1> "12"
    MD [2] #==$ 2> "34"
    Md. pre_match #== '> "time :"
    Md. post_match #== '> "am"

Original electronic documents: http://www.moer.net

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.