Ruby Loop

Ruby 教學 2 – 基本語法:Output, Ruby Loop, Conditions

接著介紹一下 Ruby 的一些基本語法:輸出、迴圈、條件句等等。

1. Output

有三種 methods 可以 output 結果:puts, print, p

1.1 puts

  • 輸出後會換行
  • 回傳 nil (類似 JavaScript 的 null)
  • puts array 時,每輸出一個 element 會自動換行
puts "Hello, world!"
puts 123
hash = {name: "Jimmy", age: 26}
puts hash
puts [1, 2, 3]
p puts

Hello, world!
123
{:name=>"Jimmy", :age=>26}
1
2
3

nil

1.2 print

  • 不會換行
  • 回傳 nil
print "Hello, world!"
print [1, 2, 3]
p print

Hello, world![1, 2, 3]nil

1.3 p

  • 輸出後會換行
  • 回傳輸出的東西
p "Hello, world!"
p 123
p [1, 2, 3]
p p "Hello, world!"

"Hello, world!"
123
[1, 2, 3]
"Hello, world!"
"Hello, world!"

2. 變數, 常數

2.1 宣告變數

Ruby 並沒有前綴詞來宣告變數,直接在變數名稱後賦值即可:

str = "Hello World!"
p str
# "Hello World!"

2.2 更新變數

直接重新賦值即可:

str = "Hello World!"
p str
# "Hello World!"

str = "Update str"
p str
# "Update str"

2.3 宣告常數

不像 JavaScript 是用 const 和 let 來區分變數和常數,Ruby 是用命名方式來區分,在 Ruby 中,如果名稱第一個字為大寫則為常數,需要注意的是,在 Ruby 中如果不小心重新賦值常數,並不會報錯,只會有 warning 而已,使用上要注意:

Str = "Hello World!"
p Str
# "Hello World!"

Str = "Update str"
p Str
warning: already initialized constant Str
warning: previous definition of Str was here
"Update str"

2. 迴圈 – Ruby Loop

在 Ruby 中有許多種方式的 loop

2.1 times

times 是最基本的 loop,直接指定要重複執行的次數:

3.times do
  p "Jimmy is handsome"
end

# "Jimmy is handsome"
# "Jimmy is handsome"
# "Jimmy is handsome"

可以加上 local variable,取得目前在第幾個迴圈 (local variable 只存在於迴圈中):

ps: 在 Ruby 中如果 do, end 之間只有一行,可以簡化成用 {} 包起來:

3.times do |i|
  p "Jimmy is handsome, loop #{i}"
end
# "Jimmy is handsome, loop 0"
# "Jimmy is handsome, loop 1"
# "Jimmy is handsome, loop 2"

p i
# undefined local variable or method `i' for main:Object (NameError)

# 寫法等同於:
3.times { |i| p "Jimmy is handsome, loop #{i}" }

2.2 downto

可以用來指定 local variable 的初始值,並且遞減到什麼值停止迴圈:

3.downto(1) do |i|
  p "Jimmy is handsome, loop #{i}"
end
# "Jimmy is handsome, loop 3"
# "Jimmy is handsome, loop 2"
# "Jimmy is handsome, loop 1"

# 寫法等同於:
3.downto(1) { |i| p "Jimmy is handsome, loop #{i}" }

2.3 upto

downto 的相反,遞增到某個值停止迴圈:

1.upto(3) do |i|
  p "Jimmy is handsome, loop #{i}"
end
# "Jimmy is handsome, loop 1"
# "Jimmy is handsome, loop 2"
# "Jimmy is handsome, loop 3"

# 寫法等同於:
1.upto(3) { |i| p "Jimmy is handsome, loop #{i}" }

2.4 step

可以指定 local variable 的初始值及要遞增的值:

0.step(9, 3) do |i|
  p "Jimmy is handsome, loop #{i}"
end
# "Jimmy is handsome, loop 0"
# "Jimmy is handsome, loop 3"
# "Jimmy is handsome, loop 6"
# "Jimmy is handsome, loop 9"

9.step(0, -3) do |i|
  p "Jimmy is handsome, loop #{i}"
end
# "Jimmy is handsome, loop 9"
# "Jimmy is handsome, loop 6"
# "Jimmy is handsome, loop 3"
# "Jimmy is handsome, loop 0"

2.5 while

while 後面接執行迴圈的條件,只要這個條件成立,while 迴圈就會繼續執行。
Ruby 的 while loop 沒有 local variable,因此必須在外面宣告用來停止條件相關的 variable:

i = 0
while i < 3
  p "Jimmy is handsome, loop #{i}"
  i += 1
end
# "Jimmy is handsome, loop 0"
# "Jimmy is handsome, loop 1"
# "Jimmy is handsome, loop 2"

p i
# 3

2.6 until

until 和 while 相反,後面接停止迴圈的條件,只要這個條件成立,迴圈就會停止。

i = 0
until i >= 3
  p "Jimmy is handsome, loop #{i}"
  i += 1
end
# "Jimmy is handsome, loop 0"
# "Jimmy is handsome, loop 1"
# "Jimmy is handsome, loop 2"

p i
# 3

2.7 for loop

Ruby 的 for loop 比較少用到,因為只能用在 array 和 range 裡:

# 用在 array 中,會遍歷每個元素
for i in [0, 1, 2]
  p "Jimmy is handsome, loop #{i}"
end
# "Jimmy is handsome, loop 0"
# "Jimmy is handsome, loop 1"
# "Jimmy is handsome, loop 2"

p i
# 2

# 用在 range 中,會遍歷 range 中的所有數字
# range 是 Ruby 一個特別的 object,代表某個範圍的所有數字,後面會介紹到
for i in 0..2
  p "Jimmy is handsome, loop #{i}"
end
# "Jimmy is handsome, loop 0"
# "Jimmy is handsome, loop 1"
# "Jimmy is handsome, loop 2"

p i
# 2

2.8 loop

loop 中必須要有 break,用來設定迴圈停止的條件:

i = 0
loop do
  p "Jimmy is handsome, loop #{i}"
  i += 1
  if i >= 3
    break
  end
end
# "Jimmy is handsome, loop 0"
# "Jimmy is handsome, loop 1"
# "Jimmy is handsome, loop 2"

p i
# 3

3. 條件 – Conditions

3.1 if, elsif, else

if 是最常用也是最基本的 conditions,也會搭配 elsif ( 注意是 elsif 不是 elseif!) 和 else 一起使用:

color = "red"
if color == "red"
  p "red is good"
end
# "red is good"

color = "blue"
if color == "red"
  p "red is good"
elsif color == "green"
  p "green is good"
else
  p "Jimmy is handsome"
end
# "Jimmy is handsome"

如果 if 和 end 中間只有一行,可以將 end 移掉,並將 if 移到那一行後面:

color = "red"
if color == "red"
  p "red is good"
end

# 等同於:
p "red is good" if color == "red"

3.2 三元運算子 – ternary operator

?前面的條件成立的話,回傳?後面的 value,否則回傳:後面的 value:

color = "green"
p color == "green" ? "green is good" : "Jimmy is handsome"
# "green is good"

color = "blue"
p color == "green" ? "green is good" : "Jimmy is handsome"
# "Jimmy is handsome"

3.3 unless

和 if 相反,unless 後面的條件不成立的話,就會執行 end 之間的程式:

color = "red"
unless color != "red"
  p "red is good"
end
# "red is good"

# 和以下寫法一樣:
p "red is good" unless color != "red"
p "red is good" if color == "red"

3.4 case

可以直接宣告多種條件:

color = "green"
case color
when "red"
  p "red is good"
when "green"
  p "green is good"
when "blue"
  p "blue is good"
else
  p "Jimmy is handsome"
end

# "green is good"

4. 參考資料

Ruby | Loops (for, while, do..while, until) – GeeksforGeeks
Learn to Code with Ruby

如果覺得我的文章有幫助的話,歡迎幫我的粉專按讚哦~謝謝你!

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top