1
1

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.

いろいろなコードの書き方

Last updated at Posted at 2020-04-13

##return 〇〇 if n == 0(処理はn==0の条件を満たすまで継続)
####return 〇〇 if n == 0

def factorial(n)
    return 1 if n == 0
    n * factorial(n - 1) # 関数の中で自身を呼び出している
end
p factorial(5) # => 120

####別の書き方

def factorial(n)
  if n == 0
    return 1
  else
    n * factorial(n - 1)
  end
end
p factorial(5)

##attr_accessorメソッド
####attr_accessor

class School
  attr_accessor :name
end

####別の書き方

class School
  def name=(value)
    @name = value
  end
  def name
    @name
  end
end
1
1
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
1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?