我所不知道的Ruby文法【糖?】

來源:互聯網
上載者:User

最近在讀【IronRuby Unleashed Feb.2010】的基本文法部分,發現書中有很多地方都出現了“USE A DIFFERENT ...”的表述。

的確,Ruby裡的很多基本操作都提供了很多種方式/別名,很多獵奇的文法(糖)。這樣可以讓語言的實現更加多樣化(多範式?),更加友好。不過很多時候也可以寫出更加詭異的代碼……

note:wiki了一下,Ruby應該是最像python, 都是【命令式|物件導向|反射|函數】語言。看來得加緊看看python了……

以下的基本例子選自書中,note中的例子都經過了測試。

1. 數字常量+底線大數字常量可以用底線分割開,便於閱讀。像這個樣子……
num1 = 1000000000 # A billionnum2 = 1_000_000_000 # A billion. Equivalent to the previous line
note: 其實不一定要三位分割。你還可以
p 1_0000_0000p 1_12_123_1234_12345

不過數字之間只能加一個底線。

2.字串字面量這個很常用了。不過既然沒用過,還是記一下吧。關於單雙引號字串的區別。雙引號字串會處理逸出字元和#{expr}的運算式(稱為expression interpolation的)。而單引號字串只處理\'和\\
my_string = “Hello there”# 多行字串my_string = “Hellothere!This is a three line string.”# 單引號字串:see belowmy_string = ‘A \’single-quoted string\’’ # = A ‘single-quoted string’# %q和%Q:%q產生單引號字串;%Q產生雙引號字串my_string = %q^I’ve seen enough of the chef’s food!^# Heredoc(這個叫什嗎?)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. 內建鬼畜的printf-多次使用同一參數C的printf似乎不能夠多次使用同一參數。不過Unix98的文檔 Open-group
printf 這裡的規則是有%n$這樣的文法的。Boost.format庫就是支援這樣的文法

# 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. sprintf和python-style的字串賦值
str = sprintf(“%-10s”, “IronRuby”)str = “%-10s” % “IronRuby”str = “%s %s !!!” % [“IronRuby”, “Unleashed”]# str now equals “IronRuby Unleashed !!!”

5. 數組賦值

%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”]

多個值變成一個值= =

my_array=[1,2,3,4,5]my_array[1, 2]="haha"# 此時my_array==[1, "haha", 4, 5]my_array[1..2]=[]# 此時my_array==[1, 5]

6. 詭異的rangerange可以用a..b和a...b來表示(也就是中間2個點/3個點)但是……

1..5  = [1, 5]閉區間1...5 = [1, 5) 不包括5“AAA”..”BBB” # all possible combinations between “AAA” and “BBB”# “AAA”, “AAB”, ..., “ABA”, “ABB”, ..., “ART”, “ARU”, ..., “BAZ”, “BBA”, “BBB”
7. 比較相關: equal? eql? ==, ===<=>:3出口的比較。返回-1,0或者1關係運算優先順序:&& > || > and = or
以下ref:http://www.iteye.com/topic/107040==: 判斷值相等。可以重載。例如: 17==17.0  ---> trueeql?: 判斷(== 且 類型相同),可以重載。例如:17.eql?(17.0) --> falseequal? 判斷(是同一個對象,object_id相同),不應該重載(其實還是可以重載)。
===:在Object的定義中,類似於instanceof。IR Unleashed中的描述是
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.

例如:

(1...10) === 5 returns true。這裡實際上是(1...10.member?(5))

暈倒。。。。。。

……
99. misc取字元的ascii:前面加'?'

num = ?A
Regex:string =~ regex 匹配。結果的MatchData對象存放在$~變數中另外還有$&是匹配的字串,$`是前面的部分,$'是後面的部分……@pochioly
irb(main):015:0> "Hello 123 world" =~ /\d(\d*)/=> 6irb(main):016:0> $1=> "23"
命名分組
irb(main):019:0> "Hello 123 world" =~ /\d(?<abc>\d*)/=> 6irb(main):020:0> $~["abc"]=> "23"

別點!http://www.lab2.kuis.kyoto-u.ac.jp/~yyoshida/software/rrencode.rb

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.