2013IT鐵人賽-25-ruby05-練習Learn Ruby The Hard Way網站範例Part 3
今天就來繼續練習網站上面的範例吧 :-)
首先先切換到工作目錄吧(我的 git 工作目錄在 /root 目錄下面, 請依造自己的設定來調整)
# cd /root
將 GitHub 上面的資料同步回來
# git pull origin master
remote: Counting objects: 33, done.
remote: Compressing objects: 100% (27/27), done.
remote: Total 31 (delta 7), reused 28 (delta 4)
Unpacking objects: 100% (31/31), done.
From https://github.com/sakanamax/2013ironman
* branch master -> FETCH_HEAD
Updating 0cd1772..43d240c
Fast-forward
java/Basic_006_inputData.class | Bin 0 -> 1382 bytes
java/Basic_006_inputData.java | 44 ++++++++++++++++++++++++++++++
java/Basic_007_arithmeticOperators.class | Bin 0 -> 1249 bytes
java/Basic_007_arithmeticOperators.java | 39 ++++++++++++++++++++++++++
java/Basic_008_autoCasting.class | Bin 0 -> 755 bytes
java/Basic_008_autoCasting.java | 16 +++++++++++
java/Basic_009_forceCasting.class | Bin 0 -> 766 bytes
java/Basic_009_forceCasting.java | 19 +++++++++++++
java/Basic_010_noticeCasting.class | Bin 0 -> 908 bytes
java/Basic_010_noticeCasting.java | 19 +++++++++++++
java/Basic_011_if.class | Bin 0 -> 923 bytes
java/Basic_011_if.java | 29 ++++++++++++++++++++
java/Basic_012_ifElse.class | Bin 0 -> 957 bytes
java/Basic_012_ifElse.java | 31 +++++++++++++++++++++
14 files changed, 197 insertions(+)
create mode 100644 java/Basic_006_inputData.class
create mode 100644 java/Basic_006_inputData.java
create mode 100644 java/Basic_007_arithmeticOperators.class
create mode 100644 java/Basic_007_arithmeticOperators.java
create mode 100644 java/Basic_008_autoCasting.class
create mode 100644 java/Basic_008_autoCasting.java
create mode 100644 java/Basic_009_forceCasting.class
create mode 100644 java/Basic_009_forceCasting.java
create mode 100644 java/Basic_010_noticeCasting.class
create mode 100644 java/Basic_010_noticeCasting.java
create mode 100644 java/Basic_011_if.class
create mode 100644 java/Basic_011_if.java
create mode 100644 java/Basic_012_ifElse.class
create mode 100644 java/Basic_012_ifElse.java
接下來練習 習題 7: 更多印出
建立一個檔案 ex7.rb 於我們建立好的Lab-LRTHW目錄下, 內容如下
# cat ruby/Lab-LRTHW/ex7.rb
puts "Mary had a little lamb."
#藉由 % 將 snow 帶入列印的顯示
puts "Its fleece was white as %s," % 'snow'
puts "And everywhere that Mary went."
# 使用 * 10 來列印 10次 .
puts "." * 10
end1 = "C"
end2 = "h"
end3 = "e"
end4 = "e"
end5 = "s"
end6 = "e"
end7 = "B"
end8 = "u"
end9 = "r"
end10 = "g"
end11 = "e"
end12 = "r"
#print 不像 puts 會執行換行, 然後使用 + 連接字串
print end1 + end2 + end3 + end4 + end5 + end6
print end7 + end8 + end9 + end10 + end11 + end12
puts
並使用 ruby 指令執行該程式
# ruby ruby/Lab-LRTHW/ex7.rb
Mary had a little lamb.
Its fleece was white as snow,
And everywhere that Mary went.
..........
CheeseBurger
接下來練習 習題 8: 印出,印出
建立一個檔案 ex8.rb 於我們建立好的Lab-LRTHW目錄下, 內容如下
# cat ruby/Lab-LRTHW/ex8.rb
#定義一個變數 formatter
formatter = "%s %s %s %s"
#帶入相關變數的值, 然後列印出來
puts formatter % [1, 2, 3, 4]
puts formatter % ["one", "two", "three", "four"]
#這邊因為沒有給值, 所以列印4次 %s %s %s %s
puts formatter % [formatter, formatter, formatter, formatter]
#也可以後來帶入值
puts formatter % [
"I had this thing.",
"That you could type up right.",
"But it didn't sing.",
"So I said goodnight."
]
並使用 ruby 指令執行該程式
# ruby ruby/Lab-LRTHW/ex8.rb
1 2 3 4
one two three four
%s %s %s %s %s %s %s %s %s %s %s %s %s %s %s %s
I had this thing. That you could type up right. But it didn't sing. So I said goodnight.
接下來練習 習題 9: 印出,印出,印出
建立一個檔案 ex9.rb 於我們建立好的Lab-LRTHW目錄下, 內容如下
# cat ruby/Lab-LRTHW/ex9.rb
# Here's some new strange stuff, remember type it exactly.
days = "Mon Tue Wed Thu Fri Sat Sun"
#這邊使用 \n 來進行換行
months = "Jan\nFed\nMar\nApr\nMay\nJun\nJul\nAug"
#puts 後面用接兩個物件, 使用逗號來區隔
puts "Here are days: ", days
puts "Here are the months: ", months
puts <<PARAGRAPH
There's something going on here.
With the three double-quotes.
We'll be able to type as much as we like.
Even 4 lines if we want, or 5 or 6.
PARAGRAPH
並使用 ruby 指令執行該程式
# ruby ruby/Lab-LRTHW/ex9.rb
Here are days:
Mon Tue Wed Thu Fri Sat Sun
Here are the months:
Jan
Fed
Mar
Apr
May
Jun
Jul
Aug
There's something going on here.
With the three double-quotes.
We'll be able to type as much as we like.
Even 4 lines if we want, or 5 or 6.
最後加入到 git 並傳送到 GitHub
# git add ruby/*
# git commit -m "Add ruby exercise file 7-9"
[master 670ee65] Add ruby exercise file 7-9
3 files changed, 57 insertions(+)
create mode 100644 ruby/Lab-LRTHW/ex7.rb
create mode 100644 ruby/Lab-LRTHW/ex8.rb
create mode 100644 ruby/Lab-LRTHW/ex9.rb
# git push origin master
Username for 'https://github.com': 您的帳號
Password for 'https://sakanamax@github.com': 您的密碼
Counting objects: 10, done.
Compressing objects: 100% (7/7), done.
Writing objects: 100% (7/7), 1.43 KiB, done.
Total 7 (delta 2), reused 0 (delta 0)
To https://github.com/sakanamax/2013ironman.git
43d240c..670ee65 master -> master
Fun with Day 25 ~
沒有留言:
張貼留言