# 在 irb 中執行
[].blank?
結果:true
# 在 rb 檔中執行
puts [].blank?
結果:undefined method `blank?' for []:Array (NoMethodError)
# 解決方式:掛上 active_support(整個程式碼如下)
require 'active_support/core_ext'
puts [].blank?
結果:true (會正常)
Define a function that takes one integer argument and returns logical value true or false depending on if the integer is a prime.
Per Wikipedia, a prime number (or a prime) is a natural number greater than 1 that has no positive divisors other than 1 and itself.
# 翻譯:講那麼多...就是要確認是否是質數
def isPrime(input)
end
RSpec.describe "isPrime" do
it "Should return false for non-prime numbers." do
expect(isPrime(4)).to eq(false)
expect(isPrime(100)).to eq(false)
expect(isPrime(999)).to eq(false)
expect(isPrime(958297)).to eq(false)
expect(isPrime(-7)).to eq(false)
end
it "Should return true for prime numbers." do
expect(isPrime(2)).to eq(true)
expect(isPrime(3)).to eq(true)
expect(isPrime(5)).to eq(true)
expect(isPrime(457)).to eq(true)
expect(isPrime(39229)).to eq(true)
end
end
def isPrime(input)
return false if input < 1
return true if input == 2
[*(2..(Math.sqrt(input).ceil))].select { |i| (input % i) == 0 }.empty?
end
require 'prime'
def isPrime(input)
input.prime?
end
(我無言……有…現成的…)