8
6

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 5 years have passed since last update.

クラスメソッド・インスタンスメソッドの呼び出し方

Last updated at Posted at 2018-11-17

メソッドの呼び出し方を整理した

ポイント

①クラスメソッドかインスタンスメソッドか
②呼び出し位置
③継承

クラスメソッド
・クラスメソッドは定義したクラス自身が使えるメソッド
・クラスメソッドの定義はメソッドの前に.selfをつけ、呼び出しは『クラス名.メソッド名(引数)』

インスタンスメソッド
・クラスにメソッドを作成するとそれがインスタンスメソッドとなる。特に追加で記述する必要はない
・使用するにはインスタンスを作成(.new)しなければならない

呼び出し方

user.rb

class User < ApplicationRecord
# クラスメソッド
def self.hoge
  "hoge"
end
# インスタンスメソッド
def moge
  "moge"
end

end

コンソール(Userクラス外)からの呼び出し

pry(main)>User.hoge
=> "hoge"

pry(main)> u = User.new
pry(main)> u.moge
=> "moge"


【エラー例】

pry(main)>hoge
NameError: undefined local variable or method `hoge' for main:Object
# console(Userクラス外)からの呼び出しなのでレシーバが必要

pry(main)>moge
NameError: undefined local variable or method `moge' for main:Object
# インスタンスを生成していないため呼び出せない
# レシーバがないと呼び出せない

pry(main)>User.moge
NoMethodError: undefined method `moge' for #<Class:0x007fcbef2724b0>
# mogeはインスタンスメソッドなのでクラスからは呼び出せない

Userクラス内の呼び出し

user.rb
hoge
# "hoge"

User.hoge
# “hoge"

self.hoge
# “hoge"

u = User.new
u.moge
# "moge"


def self.call
   hoge
  # “hoge”
  User.hoge
  # “hoge"
  self.hoge
  # “hoge”
  u = User.new
  u.moge
  # “moge”
end


def call
  hoge
  #エラー: インスタンスメソッド内でクラスメソッドはレシーバなしに呼び出せない
  User.hoge
  # “hoge"
  self.hoge
  #エラー: selfはインスタンス自身を指すためクラスメソッドは呼び出せない
  u = User.new
  u.moge
  # “moge”
end

エラー例

moge
NameError: undefined local variable or method `moge' for main:Object
# インスタンスを生成していないため呼び出せない
# レシーバがないと呼び出せない

User.moge
NoMethodError: undefined method `moge' for #<Class:0x007fcbef2724b0>
# mogeはインスタンスメソッドなのでクラスからは呼び出せない

self.moge
NoMethodError: undefined method `moge' for #<Class:0x007fcbef2724b0>
# selfは自身のクラスを指すのでインスタンスメソッドのレシーバには出来ない

継承とは(ex:pメソッドはなぜ呼び出せるのか)

user.rb

class User < ApplicationRecord
  def hoge
    p hoge"
  end

end

pメソッドはUserクラス内で定義されていない。
しかし全てのクラスの親クラスであるObjectクラスで定義されUserクラスに継承されているためpメソッドを使える
Objectクラス

ApplicationRecordクラス

Useクラス

Objectクラス参考
Rubyリファレンス:Object
https://ref.xaio.jp/ruby/classes/object

クラスのselfとインスタンスのself

user.rb
class User < ApplicationRecord
# クラスメソッド
def self.hoge
  p  self
end
# インスタンスメソッド
def moge
  p self
end

end

コンソール

pry(main)>User.hoge
User(id: integer, screen_name: string, country_id: integer, email: string, encrypted_password: string, reset_password_token: string, reset_password_sent_at: datetime,  profile_image: string, japanese_food_shop: integer)

pry(main)>u = User.new(id: 1)
pry(main)>u.moge
# <User id: 1, screen_name: nil, country_id: nil, email: "", created_at: nil, updated_at: nil, first_name: nil, mid_name: nil, last_name: nil, status: nil, japanese_food_shop: nil>

pry(main)>u = User.new(id: 2)
pry(main)>u.moge
# <User id: 2, screen_name: nil, country_id: nil, email: "", created_at: nil, updated_at: nil, first_name: nil, mid_name: nil, last_name: nil, status: nil,  japanese_food_shop: nil>
8
6
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
8
6

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?