星期六, 9月 28, 2013

2013IT鐵人賽-13-ruby03-練習Learn Ruby The Hard Way網站範例

2013IT鐵人賽-13-ruby03-練習Learn Ruby The Hard Way網站範例




這次我們的讀書會選用的是Oreilly 的Ruby程式設計這本書, 然後建議的練習是採用”笨方法學 Ruby” http://lrthw.github.io/ 上面的範例.


所以今天就來練習網站上面的範例吧 :-)


本次練習還是使用 EasyCloud 的 VM 來練習, 並連接終端機來操作.


首先先建立工作目錄吧(我的 git 工作目錄在 /root 目錄下面, 請依造自己的設定來調整)
# cd  /root
# mkdir  ruby/Lab-LRTHW


首先來練習 習題1: 第一個程式
建立一個檔案 ex1.rb 於我們剛剛建立好的Lab-LRTHW目錄下, 內容如下


# cat   ruby/Lab-LRTHW/ex1.rb
# puts 的相關練習
puts "Hello World!"
puts "Hello Again"
puts "I like typing this."
puts "This is fun."
puts "Yay! Printing"
puts "I'd much rather you 'not'."
#這邊透過單引號 ' ' 來讓雙引號顯示於螢幕上面, 單引號內一律視為字串
puts 'I "said" do not touch this.'


並使用 ruby 指令執行該程式
# ruby   ruby/Lab-LRTHW/ex1.rb
Hello World!
Hello Again
I like typing this.
This is fun.
Yay! Printing
I'd much rather you 'not'.
I "said" do not touch this.


接下來練習 習題 2: 註釋和井號


建立一個檔案 ex2.rb 於我們剛剛建立好的Lab-LRTHW目錄下, 內容如下


# cat   ruby/Lab-LRTHW/ex2.rb
# A comment, this is so you can read your program later.
# Anything after the # is ignored by Ruby.
# 註解是用來看程式的說明的, 在#符號後面的字元都會被 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."



並使用 ruby 指令執行該程式
# ruby   ruby/Lab-LRTHW/ex2.rb
I could have code like this.
This will run.



接下來練習 習題 3: 數字和數學計算
建立一個檔案 ex3.rb 於我們剛剛建立好的Lab-LRTHW目錄下, 內容如下


# cat   ruby/Lab-LRTHW/ex3.rb
# 數字和數學計算
puts "I will now count my chickens"


=begin
這邊會先印出 Hens 然後換行
顯示 30 => 因為是 25 + 5 (30/6)的結果 / 除法的優先性大於+加法
=end
puts "Hens", 25 + 30 / 6


=begin
這邊會先印出 Roosters 然後換行
顯示 97 => 100 - 3 (75 / 4 的餘數)的結果,
75 為 25*3 的結果, %為求出餘數, 故 75 除以 4 為 18 餘 3.
=end
puts "Roosters", 100 - 25 * 3 % 4


puts "Now I will count the eggs:"


=begin
這邊會顯示 7 => 3 + 2 + 1 - 5 + 0 - 0 + 6
4 % 2 => 0 餘數為 0 這個部份比較沒有爭議
1 / 4 => 0 這邊其實要注意的是因為沒有宣告為浮點數, 所以只會當成整數來處理, 所以 1 / 4 => 0.25 因為是整數, 所以值為0
=end
puts 3 + 2 + 1 - 5 + 4 % 2 - 1 / 4 + 6
# 利用一個小範例來顯示 1/4 的結果
print "print 1/4 result: "
puts 1/4


puts "Is it true that 3 + 2 < 5 - 7?"


# 因為有 < 符號, 所以會判斷 5 < -2 是否為真
# 故顯示 false
puts 3 + 2 < 5 - 7


puts "What is 3 + 2?", 3 + 2
puts "What is 5 - 7?", 5 - 7


puts "Oh, that's why it's false."


puts "How about some more."


# 這邊是進行邏輯判斷, 我認為把運算式寫入字串顯示會比較好
puts "Is it greater? 5 > -2", 5 > -2
puts "Is it greater or equal? 5 >= -2", 5 >= -2
puts "Is it less or equal? 5 <= -2", 5 <= -2


並使用 ruby 指令執行該程式
# ruby   ruby/Lab-LRTHW/ex3.rb
I will now count my chickens
Hens
30
Roosters
97
Now I will count the eggs:
7
print 1/4 result: 0
Is it true that 3 + 2 < 5 - 7?
false
What is 3 + 2?
5
What is 5 - 7?
-2
Oh, that's why it's false.
How about some more.
Is it greater? 5 > -2
true
Is it greater or equal? 5 >= -2
true
Is it less or equal? 5 <= -2
false


最後加入到 git  並傳送到 GitHub


# git  add ruby/*


# git  commit  -m "Lab and exercise Learn Ruby The Hard Way ex1.rb to ex3.rb"
[master e510744] Lab and exercise Learn Ruby The Hard Way ex1.rb to ex3.rb
3 files changed, 66 insertions(+)
create mode 100644 ruby/Lab-LRTHW/ex1.rb
create mode 100644 ruby/Lab-LRTHW/ex2.rb
create mode 100644 ruby/Lab-LRTHW/ex3.rb


# git push  origin  master
Username for 'https://github.com': 您的帳號
Password for 'https://sakanamax@github.com': 您的密碼
Counting objects: 9, done.
Compressing objects: 100% (7/7), done.
Writing objects: 100% (7/7), 1.78 KiB, done.
Total 7 (delta 1), reused 0 (delta 0)
To https://github.com/sakanamax/2013ironman.git
  abde303..e510744  master -> master




Fun with Day 13 ~

沒有留言: