Ruby Tips

rand

Desc: 如何在自動生成一個簡單的Range範圍的隨機數

Code:

rand(10) + 10
rand(10...20)
Random.new.rand(10...20)

awesoome_print

Desc: 能格式化JSON,HASH的輸出

Code:

gem install awesome_print
require 'ap'
ap [1,2,3]
ap [1,2,3].inject({}) { |x, y| x[y.to_s] = y; x }

String

Desc: 如何simple或者sub string?

Code:

str = "this is a test"
str["test"] # will return "test"
str["yyy"] # will return nil


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章