ruby雜記事本
2014-12-04 08:25:28

如何在iterate一個string的時候同時取得index與char?

with_index即可

my_str.each_char.with_index {|char, index| p index; p char; }

字元與ASCII之間的切換

字元轉ASCII

"a".ord # 97

ASCII轉字元

97.chr # "a"

取亂數的做法

在0到9之間取一個整數亂數

rand(10) # 6

在0到1之間取一個小數亂數

rand() # 0.7651037452881309