LoginSignup
208
208

More than 5 years have passed since last update.

rubyのクラスやらモジュールがモヤっとしている人へ

Last updated at Posted at 2013-10-11

どうも。今、やっとこの「モヤッ」が解消されたgogotanakaです。

この記事はrubyやrailsはそれなりに書けるけど

実はclassとかmoduleの継承とかインスタンスとかそいう言葉を出されると「モヤッ」となる、

方々のためにこの辺をまとめてみましたよ。

という体裁の自分用の備忘録である。

表記法

よく本とかに載っている表記法だけど一応確認。

ThisIsClass#this_is_method

これはThisIsClassクラスのインスタンスのメソッドthis_is_methodを指してる

ThisIsClass.this_is_method

ThisIsClass::this_is_method

これはThisIsClassクラスのクラスメソッドthis_is_methodを指している

一回 class Hoge ~ end を忘れよう

rubyをいじった事がある方にはおなじみ、

ex1.rb
class Foo < Bar
  def foo
    p "foo↑↑"
  end
end

この構文ある種の糖衣構文(シンタックスシュガー)です.

一回忘れると良いです.

ちなみにこのある種の糖衣構文を使わなければ、

ex1.rb
Bar = Class.new
Foo = Class.new(Bar) do |c|
  def foo
    p "foo↑↑"
  end
end

まあ、def もある種の糖衣構文とも言えるのだけどそれは後ほど.

What is "self" ?

self とはカレントオブジェクトと呼ばれたりもするが、

その時点でのメソッドの呼び出し口、と言っていいだろう.

self.rb

class ThisIsClass
  ##self は ThisIsClassクラス自体
  def this_is_method
    ##self は ThisIsClassクラスのインスタンス
    puth 'うんこしたいよー'
  end
end

この辺でモヤッとなってる方は一度この記事に目を通してからもう一度読み直すと良いだろう。

クラスメソッド

以下はThisIsClassクラスにクラスメソッドthis_is_methodを定義するやり方を4つ紹介している。

class-method.rb

class ThisIsClass
  def ThisIsClass.this_is_method
    puth 'うんこしたいよー'
  end
end

class ThisIsClass
  def self.this_is_method
    puth 'うんこしたいよー'
  end
end

class ThisIsClass
  class << ThisIsClass
    def this_is_method
      puth 'うんこしたいよー'
    end
  end
end

class ThisIsClass
  class << self
    def this_is_method
      puth 'うんこしたいよー'
    end
  end
end

インスタンスメソッド

instance-method.rb
class ThisIsClass
  def ThisIsClass.this_is_method
    puth 'うんこしたいよー'
  end
end

this_is_instance = ThisIsClass.new

ThisIsClassクラスに対してクラスメソッドnewを呼び出すとThisIsClassのインスタンスが返ってくる。

インスタンスメソッドとは、そいつらに対してのメソッドである。

クラス定数

いつか書くお

クラス変数

いつか書くお

インスタンス変数

いつか書くお

クラスの拡張

既に定義されたクラスにメソッドを新たに追加する事。(上書きではなく新規作成

クラスの拡張.rb

class ThisIsClass
  def this_is_method
    puts 'うんこしたいよー'
  end
end

class ThisIsClass
  def this_is_additional_method
    puts 'やっぱしたくないよー'
  end
end

継承

クラスを定義する時に

class 新しいクラス << 親クラス

という形式で定義すると、親クラス内で定義したメソッドを新しいクラス内でも使う事が出来る。

さらに親クラスのメソッドの編集も行える。

差分を新しいクラスで実装する事が出来る。

class-method.rb

class ParentClass
  def this_is_method(n)
    puts "#{n.to_s}回うんこしたいよー"
  end
end

class ChildClass << ParentClass
  def this_is_method(n)
    m = n + 1
    super(m)
  end
end

super は同名のメソッドを親クラスから呼び出す事を意味する。

これでChildClassクラスのメソッドthis_is_methodはParentClassクラスのメソッドthis_is_methodよりも一回多くうんこをしたい事になる。

モジュール

以下編集中だけどだしちゃう

*インスタンスを持たない
*継承できない
クラスはモノと処理だがモジュールは処理だけ

1.名前空間の提供
2.モジュール関す
3.mix-in

class-method.rb
module Hoge
  CONSTANT = 123
  def this_is_method(n)
    puts "#{n.to_s}回うんこしたいよー"
  end
  module_function :hello
end

p Hoge::CONSTANT #=> 123
p Hoge.this_is_method(2)

include Hoge
p CONSTANT #=> 123
this_is_method(n)

カプセルか
ポリモルフィズムを積極的に活用したのがダックタイピング
「アヒルのように歩きアヒルの用に泣くものはアヒルに違いない」
つまり「オブジェクトを特徴付けるのは実際のクラスや継承関係ではなく、そのオブジェクトがどのように振る舞うか」
「種類ではなくどのように振る舞うか」

class-method.rb
module Hoge
  def hey(ary)
    ary[1]
  end
end

hey({ 1 => "uuu"})でも動く

ArrayかHashは関係ない

208
208
8

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
208
208