Created
May 11, 2019 13:01
-
-
Save stevencch99/bcd6e8edc37bb4568635f82383d43929 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 定義一個 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