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

第7回

Last updated at Posted at 2020-12-01

!Mac OS X-10.15.7 !ruby-2.7.1p83

if文

if 式 then
  処理
elsif 式 then
  処理
else
  処理
end

else ifではなくelsif.thenは省略可.

case文

case 対象オブジェクト
when 値1 then
  値1と一致する時に行う処理
when 値2 then
  値2と一致する時に行う処理
else
  値1,値2以外の時に行う処理
end

thenは省略可.

一致しているかの値は複数可.

when 値1,値2 then
  処理
when 値3,値4,値5 then
  処理
end

each文

オブジェクト.each do |変数|
  処理1
  処理2
end

具体的には以下

range=5..10
range.each do |num|
  puts("num=",num)
end

for文使ったものが以下

for num in 5..10 do
 puts("num=",num)
end

例題

うるう年かどうか判断

def leap?(year)
  if year%400==0
    p true
  elsif year%100==0
    p false
  elsif year%4==0
    p true
  else
    p false
  end
end

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

  • source ~/grad_members_20f/members/Kenny0704/c7.org
2
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
2
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?