Ruby (2): basic syntax

Source: Internet
Author: User
Tags uppercase letter

Expressions and variables:
There is no difference between these two points and the other mainstream programming languages, which are skipped directly here. It is important to note that x=x+1 in Ruby can be written x+=1 but does not support x + +, x--and other unary operators

comparison operators and expressions:
In Ruby, you can use the comparison operator directly in an expression to implement the appropriate expression action if a certain logic is satisfied:

1 x =  2"x is ten"if x = = Ten   #  x is ten
    
Expression

In addition to the IF keyword you can use unless, which is the opposite of if:

1 x =2"you aren'ta teenager" unless age > B && Age < 20
   # You aren't a teenager
unless

Indicates that a number can use between between two numbers, at which point the two boundaries of between include:

1 x =  2"xis in the"if x.between? ( 6)   #  x is in the area
between

Special comparison symbol to note: x<=>y, which returns 0 if x==y, or 1 if x > y, or 1 if x<y.
 
loop:
See that you can use the Times block of numbers to implement loops, which can be in the following two ways:

1 # use do end to wrap logical blocks of code 2 " Test " end     #  testtesttesttesttest34#  use {} To wrap logic code fast  5"test"}               #  testtesttesttesttest
Loop

In general, if the logical code of the loop is a single line and is shorter, you can use the form of curly braces, and the Do end form is recommended when multiple lines are used.


In addition, when you need to know the current number of loops in the loop, you can use the following three types of iterations:

1 1.upto (Ten) {|number| puts number}  2#  or   3 do |number|  Puts number end  45# The following can also be two ways of 6 10.downto (5) {| number| puts number} 7
Loop

constants:
In Ruby, you typically name the constant as an uppercase letter, and once you capitalize the variable name, you can continue to change its value, but Ruby gives a warning. In fact, the class name is also capitalized, in a way, the class can also be regarded as a constant


string expression:

1Puts"ABC"* 5#ABCABCABCABCABC2Puts'x'>'y'      #false3Puts'x'<'y'      #true4 #If you want a character ASCII code value, use the?:5Puts? A-and 65#(as if the character is still output in ruby2.3.0)6 If you want to know the character that corresponds to an ASCII code, you can use the number. Chr method to convert it to a character:7Puts 65.CHR--A
stringexpression


Use string interpolation:

1Puts"This was a #{' bad ' * * word" #This was a bad bad bad word2 #It is important to note that this sentence must be enclosed in double quotes, while the bad in the inside can be in single quotes and double quotes3 4 #You can also use variables in interpolated values:5x = 106y = 207 #If you want to output ten +8 #direct output, you must call the number of the. To_s method, or you will get an error:9Puts x.to_s +" + "+ y.to_s +" = "+ (x +y). to_sTen #If you use interpolation, the input is: OnePuts"#{x} + #{y} = #{x + y}" 
interpolation

regular expressions and string manipulation:
Replace:
The simplest string is replaced by:

1 " Foobarbar ". Sub ("bar""foo")  #  
Sub

The method only replaces the first encounter, to replace all using Gsub.
Of course, the sub and Gsub methods support regular expressions, for example, to replace the first two characters in a string with Hello, you can use the

1 " Foobarbar " 2 " Hello ")  # Helloobarbar
regsub

iterate with a regular expression:
To iterate through a string, the scan function is the best:

1
Scan

Where the incoming parameter of scan is also a regular expression, the function iterates through the string, finds the matching character, and handles it accordingly.
Among the common regular expression special symbols are: \w (word) \d (digit) \s (space) ^ (table start) $ (table end). (any character) \a (anchor of the beginning of the character) \z (anchor with the end of the character) (where the uppercase form of the w/d/s indicates its lowercase form is reversed)

1 # For example, find the numbers in the  following string: 2 " The car cost $1000 and so car cost $ ". Scan (/\d/) do |letter| puts letter End 3 # will find this time is a digital output, it is necessary to use the + sign to indicate one or more.  4"Thecar cost $1000and so car cost $ $"
Find


Matching query:
If you just want to see if the string has a matching pattern, you can use the following method:

1  # The string has no digits 2 " The string has no digits " " Hello, this is a test " =~/[0-9]/  
Match

Of course, you can use the more powerful string match function, which does not require//To specify a regular expression, will default to the middle string as a regular expression, and has a return value, similar to the group on Java. can be accessed in a list-like manner:

1Puts"The string has no digits"Unless"Hello, this is a test". Match ('[0-9]')2x ="This is a test". Match (/(\w+) (\w+)/)3X[0]# This is4X[1]# This5X[2]# is6X[3]#Nil
Match

array:
The initialization time does not need to specify the length, can direct a = []. An array of Redis's out-of-bounds access elements will not error, but will return nil.
Add value to array: A << word or a.push (123)---both methods put elements at the end of the array
Bounce the value out: A.pop (This is the last element that pops up)
Arrays are first-in-first-out containers, but this pop is obviously LIFO. Get the array length using the. Length property. (In fact, the length of the array or string in Redis can be obtained by both lengths and the size method)
You can use the Join method to combine elements from an array into a long string, either as an attribute without parameters, or with a parameter that represents a group of characters to be split after the new string is synthesized.

1 IRB (main):013:0> a = [1, 2,3]2 = [1, 2, 3]3 irb (main):014:0> A.join 4 " 123 " 5 IRB (main):015:0> a.join ('| ' )6"1|2|3"
Join



Ruby (2): basic syntax

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.