2013IT鐵人賽-15-ruby04-練習Learn Ruby The Hard Way網站範例Part 2
今天就來繼續練習網站上面的範例吧 :-)
首先先切換到工作目錄吧(我的 git 工作目錄在 /root 目錄下面, 請依造自己的設定來調整)
# cd /root
接下來練習 習題 4: 變數(variable)和命名
建立一個檔案 ex4.rb 於我們建立好的Lab-LRTHW目錄下, 內容如下
# cat ruby/Lab-LRTHW/ex4.rb
cars = 100 #在 11行被引用
space_in_a_car = 4.0 #於第7行運算被引用
drivers = 30 #在12行被引用
passengers =90 #在15行被引用, 於第8行運算被引用
cars_not_driven = cars - drivers #在13行被引用 => 70 (100 - 30)
cars_driven = drivers #於第7行運算被引用, 於第8行運算被引用 => 30
carpool_capacity = cars_driven * space_in_a_car #在14行被引用 => 120 (30 * 4.0)
average_passengers_per_car = passengers / cars_driven #於16行被引用 => 3 (90 / 30)
# 使用 #{cars} 將 cars 變數帶進來
puts "There are #{cars} cars available."
puts "There are only #{drivers} drivers available."
puts "There will be #{cars_not_driven} empty cars today."
puts "We can transport #{carpool_capacity} people today."
puts "We have #{passengers} passengers to carpool today."
puts "We need to put about #{average_passengers_per_car} in each car."
並使用 ruby 指令執行該程式
# ruby ruby/Lab-LRTHW/ex4.rb
There are 100 cars available.
There are only 30 drivers available.
There will be 70 empty cars today.
We can transport 120.0 people today.
We have 90 passengers to carpool today.
We need to put about 3 in each car.
接下來練習 習題 5: 更多的變數和印出
建立一個檔案 ex5.rb 於我們建立好的Lab-LRTHW目錄下, 內容如下
# cat ruby/Lab-LRTHW/ex5.rb
my_name = 'Zed A. Shaw'
my_age = 35 # not a lie
my_height = 74 # inches
my_weight = 180 # lbs
my_eyes = 'Blue'
my_teeth = 'White'
my_hair = 'Brown'
# 使用 % 的方式帶入變數
puts "Let's talk about %s." % my_name
puts "He's %d inches tall." % my_height
puts "He's %d pounds heavy." % my_weight
puts "Actually that's not too heavy."
puts "He's got %s eyes and %s hair." % [my_eyes, my_hair]
puts "His teech are usually %s depending on the coffee." % my_teeth
# this line is tricky, try to get it exactly right
puts "If I add %d, %d, and %d I get %d." % [my_age, my_height, my_weight, my_age + my_height + my_weight]
並使用 ruby 指令執行該程式
# ruby ruby/Lab-LRTHW/ex5.rb
Let's talk about Zed A. Shaw.
He's 74 inches tall.
He's 180 pounds heavy.
Actually that's not too heavy.
He's got Blue eyes and Brown hair.
His teech are usually White depending on the coffee.
If I add 35, 74, and 180 I get 289.
接下來練習 習題 6: 字串(string)和文字
建立一個檔案 ex6.rb 於我們建立好的Lab-LRTHW目錄下, 內容如下
# cat ruby/Lab-LRTHW/ex6.rb
x = "There are #{10} types of people."
binary = "binary"
do_not = "don't"
y = "Those who know #{binary} and those who #{do_not}"
#列印出 x => There are 10 types of people.
puts x
#列印出 y => Those who know binary and those who don't
puts y
#
puts "I said #{x}."
#雙引號裏面是可以執行變數呼叫的,裏面那兩個單引號會被視為符號
puts "I also said: '#{y}'."
hilarious = false
joke_evaluation = "Isn't that joke so funny?! #{hilarious}"
puts joke_evaluation
w = "This is the left side of ..."
e = "a string with a right side."
# 使用 + 符號串接文字
puts w + e
並使用 ruby 指令執行該程式
# ruby ruby/Lab-LRTHW/ex6.rb
There are 10 types of people.
Those who know binary and those who don't
I said There are 10 types of people..
I also said: 'Those who know binary and those who don't'.
Isn't that joke so funny?! false
This is the left side of ...a string with a right side.
最後加入到 git 並傳送到 GitHub
# git add ruby/*
# git commit -m "Lab and exercise Learn Ruby The Hard Way ex4.rb to ex6.rb"
[master 16f1565] Lab and exercise Learn Ruby The Hard Way ex4.rb to ex6.rb
3 files changed, 57 insertions(+)
create mode 100644 ruby/Lab-LRTHW/ex4.rb
create mode 100644 ruby/Lab-LRTHW/ex5.rb
create mode 100644 ruby/Lab-LRTHW/ex6.rb
# git push origin master
Username for 'https://github.com': 您的帳號
Counting objects: 10, done.
Compressing objects: 100% (7/7), done.
Writing objects: 100% (7/7), 1.69 KiB, done.
Total 7 (delta 1), reused 0 (delta 0)
To https://github.com/sakanamax/2013ironman.git
f8c8ad6..16f1565 master -> master
Fun with Day 15 ~
沒有留言:
張貼留言