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

privateメソッドとは

Posted at

#privateメソッドとは
オブジェクトの内側からは利用できるが、外側からは利用できないようにしたメソッド。
メソッドを定義する前にprivateを宣言すれば使える。

Person.rb
class Person
  def initialize(age)
   @age = age
  end

  def over30th?
   age > 30
  end

 private #有無の違いで後の出力結果が変わる

  def age
   @age 
  end

person = Person.new(35)

=======================================

#private無しの場合
> person.over30th?
=> true
> person.age
=> 35

#private有りの場合
> person.over30th?
=> true
> person.age
=> NoMethodEroor
0
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
0
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?