0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

[Ruby] 真偽知を返すメソッド

Last updated at Posted at 2020-06-16

Progate学習メモ
メソッドの戻り値に条件式を記述して真偽知を返すようにする

.rb
def shipping_free?(price)
  return price >= 3000
end

真偽値を返すメソッドは、メソッド名の末尾に「?」をつける慣習がある。
このメソッドは、priceが3000以上だとtrue、
priceが3000より少ないとfalseを返す

if文に組み込むとこんな感じ

.rb
if shipping_free?(4000)
  puts "送料無料です。"
else
  puts "送料500円がかかります。"
end

これの実行結果は、

送料無料です。
``

---
参考:[Progate](https://prog-8.com)
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?