Skip to content

Instantly share code, notes, and snippets.

@stevencch99
Created May 11, 2019 13:01
Show Gist options
  • Save stevencch99/bcd6e8edc37bb4568635f82383d43929 to your computer and use it in GitHub Desktop.
Save stevencch99/bcd6e8edc37bb4568635f82383d43929 to your computer and use it in GitHub Desktop.
# 定義一個 Porc 物件把 block 包起來,當呼叫的時候會在裡面做運算並回傳布林值(true or false)
divisible_by_15 = proc { |number| (number % 15).zero? }
divisible_by_5 = proc { |number| (number % 5).zero? }
divisible_by_3 = proc { |number| (number % 3).zero? }
num = 9
case num
# when 會呼叫 divisible_by_15 的 === 方法,將 num 作為引數傳進去
# divisible_by_15 === 9
when divisible_by_15
puts "FizzBuzz"
when divisible_by_5
puts "Buzz"
when divisible_by_3
puts "Fizz"
else
puts num
end
# Fizz
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment