Hot

2012年7月15日 星期日

Ruby入門(2)

練習二:

在練習一加分題已學會使用註解,但還是要練習 = )。

# A comment, this is so you can read your program later.
# Anything after the # is ignored by Ruby.

puts "I could have code like this." # and the comment after is ignored

# You can also use a comment to "disable" or comment out a piece of code:
# print "This won't run."

puts "This will run."

加分題:

1.#  英文為 octothorpe   = >成功
2.ex2.rb中。由最後一行逐個單字檢查回第一行
   雖然不太清楚題目要的到底是什麼,如果是檢查打錯字,那算 = >成功
   # print "This won't run."

3.改正錯誤  = >成功
4.讀出每個字符,改正錯誤  = >成功


我學到了:

1.註解使用方式
2.閱讀要仔細


附記:

# print "This won't run."
打開,發現
print "This won't run."一樣可以顯示,而且

print "This won't run.\n"

只要一個\n就有換行效果,但puts 與print的差異為何呢??


"print 會準確輸出所提供的內容;puts 則一定會以輸出行 (output line) 結尾"
所以還是不懂= ~ =

於是經另一個網站中說明後:

http://wiki.plweb.org/Ruby/Chapter

才知道

puts 會自動幫你換行,"有隱含的\n"意思(等同上上個網站中說的)


printf 就是單純印出你想印的,加\n才換行

另外上個網站還有提到有第三種方法   

p      可以辨別印出的是數值還是字串(字串會連" "一起印出)

三個都是Kernel模組所提供的方法 = )

感謝作者們 = )








沒有留言:

張貼留言