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?

privateメソッドを呼び出す

Posted at

「クラス外から呼び出せず、クラスの内部でのみ使えるメソッド」だと言えます
p246

2.7からself付きでもprivateメソッドを呼び出せる

レシーバをselfとしてprivateメソッドを呼び出すことが許容されるようになりました。

def foo
end
private :foo
self.foo

irb(main):001* class User
irb(main):002*   private
irb(main):003*   def hello
irb(main):004*     "Hello"
irb(main):005*   end
irb(main):006> end
=> :hello
irb(main):007> user = User.new
=> #<User:0x000000011317fcf0>
irb(main):008> user.hello
(irb):8:in `<main>': private method `hello' called for #***********(NoMethodError)
irb(main):001* class User
irb(main):002*   def hello
irb(main):003*     "hello, I am #{name}."
irb(main):004*   end
irb(main):005*
irb(main):006*   private
irb(main):007*
irb(main):008*   def name
irb(main):009*     "Alice"
irb(main):010*   end
irb(main):011> end
=> :name
irb(main):012> user = User.new
=> #<User:0x0000000104feed40>
irb(main):013> user.hello
=> "hello, I am Alice."

self有無関係なくprivateメソッドを呼び出せる

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?