自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

菜鳥Ruby技巧集合:序列,Array及散列

開發(fā) 開發(fā)工具
本文介紹幾個比較初級的Ruby技巧,對于理解一些Ruby的基礎(chǔ)用法比較有幫助。

原文是說幾個蠢笨的ruby技巧。

51CTO編輯推薦:Ruby入門教程與技巧大全

原文地址:http://robots.thoughtbot.com/

Ruby技巧之代碼塊的序列調(diào)用

  1. def touch_down    
  2.   yield [3, 7]    
  3.   puts "touchdown!"    
  4. end    
  5.     
  6. touch_down do |(first_down, second_down)|    
  7.   puts "#{first_down} yards on the run"    
  8.   puts "#{second_down} yards passed"    
  9. end    
  10.     
  11. => "3 yards on the run"    
  12. => "7 yards passed"    
  13. => "touchdown!"   

主要是說array在block中的使用

Ruby技巧之從array中取出元素

  1. >> args = [1, 2, 3]    
  2. >> first, rest = args    
  3.     
  4. >> first    
  5. => 1    
  6.     
  7. >> rest    
  8. => [2, 3]   

之前只是清楚split序列的用法,沒有注意到實際上,我們可以方便的得到剩余的序列。

  1. Hash#fetch   
  2.  
  3. >> items = { :apples => 2, :oranges => 3 }    
  4. => items = {:apples=>2, :oranges=>3}    
  5.     
  6. >> items.fetch(:apples)    
  7. => 2    
  8.     
  9. >> items.fetch(:bananas) { |key| "We don't carry #{key}!"}    
  10. => We don't carry bananas!    
  11.  

在散列的使用的時候,fetch可能會比檢查是否存在值要方便一些。

Ruby技巧之創(chuàng)建代碼段的散列

  1. >> smash = Hash.new { |hash, key| hash[key] = "a #{key} just got SMASHED!" }    
  2. => {}    
  3.     
  4. >> smash[:plum] = "cannot smash."    
  5. => {:plum=>"cannot smash."}    
  6.     
  7. >> smash[:watermelon]    
  8. => {:plum=>"cannot smash.":watermelon=>"a watermelon just got SMASHED!"}    
  9.  

將代碼段用于生產(chǎn)散列可以方便的保持一些未定義的初始值,特別是在斐波納契計算中很適合(我沒有看出來怎么用)

  1. Array#sort_by   
  2.  
  3. >> cars = %w[beetle volt camry]    
  4. => ["beetle""volt""camry"]    
  5.     
  6. >> cars.sort_by { |car| car.size }    
  7. => ["volt""camry""beetle"]    
  8.  

序列的sort_by方法用來對代碼段的返回值排序,就如同對于Symbol#to_proc進行map或者sort

  1. String#present?   
  2.  
  3. >> "brain".present?    
  4. => true    
  5.     
  6. >> "".present?    
  7. => false    
  8.  

Rails的開發(fā)者可能對于blank?比較熟悉,然而對于present呢?實際上判斷返回值是否正確這也是很好用的方法。

這里我確實想起來,對于find(:all)和find(:first)是否有返回值的判斷的不同。還有一個
.exists?
.empty?
.blank?
.nil?

比較多見到吧。

本文來自夜鳴豬的博客:《幾個Ruby用法的小技巧》

【編輯推薦】

  1. Ruby使用心得匯總:尋找高效的實現(xiàn)
  2. Ruby on Rails入門之道
  3. Ruby on Rails 2.3.3發(fā)布 主要修復(fù)Bug
  4. Ruby on Rails開發(fā)的五點建議
  5. 淺談Ruby和JRuby的學(xué)習(xí)
責(zé)任編輯:yangsai 來源: 夜鳴豬的博客
相關(guān)推薦

2011-06-15 14:55:42

Session

2022-10-11 23:18:28

散列表函數(shù)數(shù)組

2009-12-14 09:33:04

Ruby安裝

2009-12-15 10:23:23

Ruby應(yīng)用技巧

2009-12-14 18:23:38

Ruby DSL測試

2009-12-14 15:30:43

安裝Ruby on R

2009-10-14 09:15:15

2009-12-15 18:15:24

Ruby連接到LDAP

2010-02-24 17:41:05

WCF集合反序列化

2009-12-18 10:47:16

Ruby裝飾模式

2009-12-15 18:24:02

Ruby連接到orac

2009-12-17 17:37:42

Ruby on Rai

2019-08-12 06:41:26

PHP反序列化漏洞

2009-12-18 14:10:29

Ruby訪問剪貼板

2009-12-14 16:35:28

Ruby文件行數(shù)計算

2009-12-18 14:19:45

Ruby on Rai

2009-12-16 11:04:51

Ruby操作文件權(quán)限

2009-12-17 10:18:17

Ruby創(chuàng)建構(gòu)造器

2022-05-12 11:12:46

MongoDB索引元數(shù)據(jù)

2009-12-18 13:13:59

Ruby on Rai
點贊
收藏

51CTO技術(shù)棧公眾號