I recently read the basic syntax section of [ironruby unleashed feb.2010] and found that "use a different..." appears in many places in the book.
Indeed, many basic operations in Ruby provide many methods/aliases, and many odd syntax (sugar ). In this way, the language implementation can be more diversified (Multi-paradigm ?), More friendly. However, it is often possible to write more strange code ......
Note: After wiki, Ruby should be the most similar to Python. It is a [imperative | object-oriented | reflection | function] language. It seems that you have to step up watching python ......
The following basic examples are selected from the book, and the examples in note are all tested.
1. Numeric constants + underline large numeric constants can be separated by underscores for easy reading. Like this ......
num1 = 1000000000 # A billionnum2 = 1_000_000_000 # A billion. Equivalent to the previous line
Note: in fact, it is not necessary to separate three digits. You can
p 1_0000_0000p 1_12_123_1234_12345
However, only one underline can be added between numbers.
2. String Literal is very common. But since it has never been used, remember it. The difference between Single and Double quotation marks. Double quotation marks are used to process escape characters and expressions of # {expr} (called expression interpolation ). The single quotation mark string only processes \ 'and \\
My_string = "Hello there" # multi-line string my_string = "hellothere! This is a three line string. "# single quotes string: see belowmy_string = 'a \ 'single-quoted string \'' # = A' single-quoted string '# % Q and % Q: % Q generates a single quotation mark string; % Q generates a double quotation mark string my_string = % Q ^ I 've seen enough of the chef's food! ^ # Heredoc (what is this ?) My_string = <dochello there! # This is part of the text... can't put comments inside heredocsthis is the second line. docmy_string = <'end of my string 'I m writing here whatever I want. yes, everything goes! Hallelujah! End of my string
3. the printf with the same parameter C appears to be unable to use the same parameter multiple times. However, the unix98 document open-group
Printf rules include the syntax % N $. The boost. Format Library supports this syntax.
# Use the same value multiple timesprintf(“%1$s %2$s, %2$s %1$s, %2$s %2$s %2$s”, “hello”, “Ruby!”)# = “hello Ruby!, Ruby! hello, Ruby! Ruby! Ruby!”
4. assign values to sprintf and Python-style strings
str = sprintf(“%-10s”, “IronRuby”)str = “%-10s” % “IronRuby”str = “%s %s !!!” % [“IronRuby”, “Unleashed”]# str now equals “IronRuby Unleashed !!!”
5. array assignment
String Array defined by % W/% W
my_array = %w-A B C 5-# Same as [‘A’, ‘B’, ‘C’, ‘5’]my_array = %W~”Iron Ruby” V1~# Same as [“\”Iron”, “Ruby\””, “V1”]
Convert multiple values to one value =
My_array = [, 5] my_array [1, 2] = "Haha" # my_array = [1, "Haha", 4, 5] my_array [1 .. 2] = [] # my_array = [1, 5]
6. The strange rangerange can be expressed by A. B and A. B (that is, two or three vertices in the middle), ......
1 .. 5 = [1, 5] closed range 1... 5 = [1, 5) does not include 5 "AAA ".. "BBB" # all possible combinations between "AAA" and "BBB" # "AAA", "AAB ",..., "ABA", "ABB ",..., "art", "aru ",..., "BAZ", "BBA", "BBB"
7. Comparison: equal? Eql? =, = <=>: Comparison of 3 exits. Return-1, 0, or 1 relational operation Priority: & gt; |> and = or
The following Ref: http://www.iteye.com/topic/1070401_values: The value is equal. Can be reloaded. Example: 17 = 17.0 ---> trueeql? : Determines (= and has the same type) and can be overloaded. Example: 17.eql? (17.0) --> falseequal? Judge (it is the same object with the same object_id) and should not be overloaded (in fact, it can still be reloaded ).
===: In the object definition, it is similar to instanceof. The description in IR unleashed is
Case equality. Mostly used implicitly in case statements. It has different implementations. For example, for ranges it checks for membership, and for regular expressionsit tries to pattern match.
For example:
(1... 10) = 5 returns true. Here is actually (1... 10. member? (5 ))
Fainting ......
......
99. Misc takes the ASCII character: Add '? '
num = ?A
Regular Expression: String = ~ RegEx match. The matchdata object of the result is stored in $ ~ $ & Is a matched string in the variable, $ 'is the previous part, and $' is the later part ...... @ Pochioly
irb(main):015:0> "Hello 123 world" =~ /\d(\d*)/=> 6irb(main):016:0> $1=> "23"
Naming Group
irb(main):019:0> "Hello 123 world" =~ /\d(?<abc>\d*)/=> 6irb(main):020:0> $~["abc"]=> "23"
Don't click! Http://www.lab2.kuis.kyoto-u.ac.jp /~ Yyoshida/software/rrencode. Rb