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 1 year has passed since last update.

WEB開発をやり始めてみる(Ruby:条件分岐&メソッド)

Last updated at Posted at 2022-10-16

目的

Rubyの学習でまずは基礎文法のやり直し
ほとんど忘れているため、メモとして残して見直せれるように

やったこと

if文

score = 90
if score >= 90
  puts"A"
elsif score >= 80
  puts"B"
elsif score >= 70
  puts"C"
else
  puts "D"
end

$ ruby hello.rb
A

### 省略形での書き方(1行の場合)
puts "A判定" if score >= 90

### true/faluseの場合
puts score >= 90 ? "A判定です" : "不合格"

case文

frute = "banana"

case frute
when "banana"
    puts"イエロー"
when "Apple"
    puts"レッド"
when "orange"
    puts "オレンジ"
else
    puts"それ以外"
end

$ ruby case.rb 
イエロ-

メソッド

def cry(animal)
    if animal=="cat"
        "にゃー"
    else
        "????"
    end
end

puts cry("cat")
puts cry("dog")

>> ruby fizzbuzz.rb 
にゃー
????

def sum(price,num)
  return "0以上にしてください" if num <= 0
  "#{price}円を#{num}個買った、合計金額は#{price * num}円"
end

puts disp_sum(2000,20)
puts disp_sum(200,0)

>> ruby fizzbuzz.rb 
2000円の商品を20個買ったので、合計金額は40000です
0以上にしてください
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?