[問題] learn ruby the hard way 41
想請教關於 learn ruby the hard way Exercise41 的一段程式碼
http://learnrubythehardway.org/book/ex41.html
在oop_test.rb這個檔案裡面有一段
從
50 param_names = craft_params(rand_words, snippet, /@@@/)
跳到
36 def craft_params(rand_words, snippet, pattern)
37 names = (0...snippet.scan(pattern).length).map do
38 param_count = rand(3) + 1
39 params = (0...param_count).map {|x| rand_words.pop() }
40 params.join(', ')
41 end
42
43 return names * 2
44 end
這段看不太懂,
我嘗試 puts (0...snippet.scan(pattern).length).map
但是印出來的結果是 #<Enumerator:0x000000028ee138> <==??
接下來我嘗試
puts (0...3).map
結果是只出現一個 #<Enumerator:0x000000028653b0>
而不是
#<Enumerator:0x000...>
#<Enumerator:0x000...>
#<Enumerator:0x000...>
這樣的話假設我的param_count = 3
那39行怎麼知道要pop三個word出來呢?
--
※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 220.133.230.3
※ 文章網址: https://www.ptt.cc/bbs/Ruby/M.1437040333.A.551.html
→
07/16 19:25, , 1F
07/16 19:25, 1F
→
07/16 19:25, , 2F
07/16 19:25, 2F
→
07/16 19:26, , 3F
07/16 19:26, 3F
→
07/16 19:26, , 4F
07/16 19:26, 4F
→
07/16 19:27, , 5F
07/16 19:27, 5F
→
07/16 19:28, , 6F
07/16 19:28, 6F
→
07/16 19:28, , 7F
07/16 19:28, 7F
→
07/16 19:29, , 8F
07/16 19:29, 8F
→
07/16 19:32, , 9F
07/16 19:32, 9F
→
07/16 19:32, , 10F
07/16 19:32, 10F
→
07/16 19:33, , 11F
07/16 19:33, 11F
→
07/16 19:34, , 12F
07/16 19:34, 12F
推
07/16 20:31, , 13F
07/16 20:31, 13F
→
07/16 20:32, , 14F
07/16 20:32, 14F
→
07/16 20:33, , 15F
07/16 20:33, 15F
推
07/17 00:17, , 16F
07/17 00:17, 16F
→
07/17 10:16, , 17F
07/17 10:16, 17F
Ruby 近期熱門文章
PTT數位生活區 即時熱門文章