作者fsj (夏天就是要去野台!)
看板Ruby
標題[問題] LRTHW ex18 疑問
時間Tue Apr 9 00:01:56 2013
環境: Ruby 2.0.0p0
我在Learn Ruby The Hard Way的第18個練習裡有一些小疑問想請教
http://ruby.learncodethehardway.org/book/ex18.html
# this one is like your scripts with argv
def puts_two(
*args)
arg1, arg2 = args
puts "arg1: #{arg1}, arg2: #{arg2}"
end
# ok, that *args is actually pointless, we can just do this
def puts_two_again(arg1, arg2)
puts "arg1: #{arg1}, arg2: #{arg2}"
end
作者說 *args 其實是 pointless, 但是程式仍然可以執行?
那*args的意義到底是?
因為我如果把*args換成任意的*____ , 比如說:
def puts_two(
*a)
arg1, arg2 = a
puts "arg1: #{arg1}, arg2: #{arg2}"
end
程式也都能執行, 還是說*號有特別的意義?
小弟是新手, 沒有程式底子, 請各位多指教
謝謝解惑!
--
http://www.facebook.com/AdJudge
廣告, 創意無限
--
※ 發信站: 批踢踢實業坊(ptt.cc)
◆ From: 1.164.49.131
1F:→ aquarianboy:你可以試著用 ruby + splat 當做關鍵字 google一下 :) 04/09 06:21
2F:→ fsj:太感謝了! 因為有個*號, 我不知道怎麼google....再次感謝! 04/09 11:02
3F:→ mars90226:現在又多了**和keyword argument~XD 04/09 14:22
4F:→ david50407:*args 的意思是把所有參數push進去args這個array裡 04/09 21:08
5F:→ david50407:所以換成*a就是把兩個參數放進a裡囉~ 04/09 21:09
6F:→ fsj:感謝樓上! 04/09 22:32