クラス定義基本(コンストラクタ, 継承, 自己参照, 親メソッド呼び出し)

– initialize メソッドがコンストラクタになる。

class Foo
  def initialize(arg)
    print “Hello, ” + arg + “!”
  end
end
 
Foo.new(“world”)
# result:
# Hello, world!

 
– 継承は “<"。シェルで使うような取り込むイメージ

class Parent
end
 
class Child < Parent
end

 
– 親クラスメソッド呼び出しはオーバーライドするメソッドで super とする。

class Parent
    def foo(arg)
       print “parent “, arg, “\n”
    end
end
 
class Child < Parent
    def foo(arg)
       print “child\n”
       super(arg)
    end
    
end
 
Child.new.foo(“abc”)
#
# result:
# child
# parent abc
#

– メソッドのオーバーロードはできない
オーバーロードしようとすると最後の定義のみ有効になり、それ以外の呼び出しでは引数の数が違うというエラーが出る。

wrong number of arguments (1 for 0) (ArgumentError)

 
– 自己参照は this ではなく self

class A
  attr_reader :name # 必須
  
  def initialize
    @name = “John”;
  end
  
  def foo
    print self.name
  end
end
 
A.new.foo
#
# result:
# John

self での参照はクラス外部からの参照と同等となるのでアクセス制限に注意!!
 
– 多重継承はモジュールに対してのみ可能
<http://www.ruby-lang.org/ja/man/?cmd=view;name=Module>
include で疑似的に実装する。

class Parent
end
 
module SubParent # module で宣言されていることに注意
  def foo
  end
end
 
module SubParent2 # module で宣言されていることに注意
  include SubParent # module -> module の継承
  def woo
  end
end
 
class Child < Parent # class -> class の継承
    include SubParent # module -> class の(多重)継承
    include SubParent2 # module -> class の(多重)継承
end

ちゃんと SubParent, SubParent2 の子として認識されるのでまぁ安心して使える。
多重にするには親を module にしないとだめって事で制限があるので、 Java でいうところの interface に近い使い方になるのかな。
 
– 委譲 (delegate)
デリゲート用のライブラリがあった。
<http://www.ruby-lang.org/ja/man/?cmd=view;name=delegate.rb>
でもデリゲートしてるのに宣言時点で親クラスにならないとだめとか微妙なような..
include delegate(Array)
みたいな使い方ならいいのにな。

アクセサの定義の仕方

その1

attr_accessor :attr1, :attr2, :attr3# read/write
attr_reader :attr3, :attr4 # read-only
attr_writer :attr5 # write-only

 : を使う宣言式共通の注意事項だが

attr_accessor: attr1

などとするとエラーになるので注意。
 : は先頭の宣言にかかるのではなく各プロパティにかかるので、各プロパティごとに空白を挟まず先頭に : をつけないといけない。
 @ をつけた同名のインスタンス変数に対してのアクセサとなる。
 
その2

# reader
def attr1
  return @attr1
end
# writer
def attr1= (arg)
  @attr1 = arg
end

任意の実装をしたい場合はこちらを使う。
 
毎回忘れるのでメモ。公式ドキュメント読みづらいっす。
参考:
<http://www.ruby-lang.org/ja/man/?cmd=view;name=FAQ%3A%3A%A5%E1%A5%BD
%A5%C3%A5%C9
>
– Ruby 公式ドキュメント -> FAQ -> 5. メソッド