Ruby: Control Structure

Conditions

  • if-elsif-else(basic):
    • basic condition control structure, will do anything you want.
    • trick1: when you assign a value to a variable behind if
      • that variable can only be used in the multiline scheme.
      • that variable cannot be used in the single line scheme.
    • trick2: when you define a variable inside the conditional block.
      • you can use it outside the conditional block
      • when the condition is false, outside variable value is nil.
      • both single line and multiline schemes can use this feature.
  • case-when-else(switch):
    • has clear structure than the basic if-else structure.
    • internally use the ruby triple equal operator === to compare.
    • a === b means "if 'a' describe a set, would 'b' be a member of that set?"
      • when range and element: (1..5) === 3
      • when class and class: Object === String
      • when class and object: Object === 'string'
      • when regex and string: /foo/ === 'foobar'
      • you can also rewrite object === method
  • ternary-operator(terse):
    • only use one line code, look so pretty :P_
    • suit with simple statement, shouldn't be too complex.
    • also use for assignment with condition: a = condition ? b : c
  • && (and) vs || (or):
    • a && b can convert to: b if a
    • a || b can convert to: b if not a
    • assign with default value: x = a || b
    • assign if not assigned before: x ||= c

Loops

  • iterators:
    • (basic) each, select, map, reduce...
    • (numbers) times, upto, downto, step...
  • traditional-loop:
    • loop: infinite loop, use break to stop it.
      • use case: generate unique random keys.
  • control-keywords:
    • break: says quit immediately.
    • next: says loop immediately.
    • redo: says redoes that same loop over again.
    • retry: says starts the whole loop over from the beginning.
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末兵迅,一起剝皮案震驚了整個濱河市抢韭,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌恍箭,老刑警劉巖刻恭,帶你破解...
    沈念sama閱讀 216,496評論 6 501
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場離奇詭異扯夭,居然都是意外死亡鳍贾,警方通過查閱死者的電腦和手機(jī),發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,407評論 3 392
  • 文/潘曉璐 我一進(jìn)店門交洗,熙熙樓的掌柜王于貴愁眉苦臉地迎上來骑科,“玉大人构拳,你說我怎么就攤上這事≈蒙” “怎么了?”我有些...
    開封第一講書人閱讀 162,632評論 0 353
  • 文/不壞的土叔 我叫張陵凫海,是天一觀的道長。 經(jīng)常有香客問我盐碱,道長,這世上最難降的妖魔是什么瓮顽? 我笑而不...
    開封第一講書人閱讀 58,180評論 1 292
  • 正文 為了忘掉前任,我火速辦了婚禮缕贡,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘拣播。我一直安慰自己,他們只是感情好贮配,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,198評論 6 388
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著昼蛀,像睡著了一般宴猾。 火紅的嫁衣襯著肌膚如雪叼旋。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 51,165評論 1 299
  • 那天讹剔,我揣著相機(jī)與錄音,去河邊找鬼详民。 笑死,一個胖子當(dāng)著我的面吹牛沈跨,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播谒出,決...
    沈念sama閱讀 40,052評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼笤喳,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了杀狡?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 38,910評論 0 274
  • 序言:老撾萬榮一對情侶失蹤呜象,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后恭陡,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 45,324評論 1 310
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡休玩,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,542評論 2 332
  • 正文 我和宋清朗相戀三年劫狠,在試婚紗的時候發(fā)現(xiàn)自己被綠了。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片独泞。...
    茶點(diǎn)故事閱讀 39,711評論 1 348
  • 序言:一個原本活蹦亂跳的男人離奇死亡,死狀恐怖懦砂,靈堂內(nèi)的尸體忽然破棺而出组橄,到底是詐尸還是另有隱情,我是刑警寧澤晨炕,帶...
    沈念sama閱讀 35,424評論 5 343
  • 正文 年R本政府宣布毫炉,位于F島的核電站,受9級特大地震影響瞄勾,放射性物質(zhì)發(fā)生泄漏费奸。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,017評論 3 326
  • 文/蒙蒙 一进陡、第九天 我趴在偏房一處隱蔽的房頂上張望愿阐。 院中可真熱鬧,春花似錦趾疚、人聲如沸缨历。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,668評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽辛孵。三九已至,卻和暖如春赡磅,著一層夾襖步出監(jiān)牢的瞬間魄缚,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 32,823評論 1 269
  • 我被黑心中介騙來泰國打工焚廊, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留冶匹,地道東北人咆瘟。 一個月前我還...
    沈念sama閱讀 47,722評論 2 368
  • 正文 我出身青樓,卻偏偏與公主長得像嗓蘑,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子桩皿,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 44,611評論 2 353

推薦閱讀更多精彩內(nèi)容