1
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.

R7.org

Last updated at Posted at 2020-12-23

!Mac OS X-10.15.7 !ruby-2.7.1p83

第7回

チャート式Rubyの第3回目

条件分岐とArray.each

def leap?(year)
  return case 
    when year % 400 == 0 ; true
    when year % 100 == 0 ; false
    when year % 4 == 0   ; true
    else                 ; false
    end
end

[2000, 1900, 2004, 1999].each do |year|
  p year
  p leap?(year)
end

上記のコードを実行してみる

> ruby check_leap_year.rb
2000
true
1900
false
2004
true
1999
false

年数とうるう年か否かが判定されている。三項演算子でもスマートに書くことが出来るがcase文の方が理解しやすく感じる。

参考サイト

チャート式ruby-lll


  • source ~/grad_members_20f/members/skona/memo/R7.org
1
0
1

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
1
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?