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?

メソッドのオーバーライド、クラスメソッドの継承

Posted at

メソッドのオーバーライド

スーパークラスと同名のメソッドを定義することで、スーパークラスの処理を上書きすることが
出来ます。これをメソッドのオーバーライドといいます。

irb(main):001* class Product
irb(main):002*   attr_reader :name, :price
irb(main):003*
irb(main):004*   def initialize(name, price)
irb(main):005*     @name = name
irb(main):006*     @price = price
irb(main):007*   end
irb(main):008> end
=> :initialize
irb(main):009* class DVD < Product
irb(main):010*   attr_reader :running_time
irb(main):011*
irb(main):012*   def initialize(name, price, running_time)
irb(main):013*     super(name, price)
irb(main):014*     @running_time = running_time
irb(main):015*   end
irb(main):016> end
=> :initialize
irb(main):017> priduct = Product.new("A great movie", 1000)
=> #<Product:0x000000010e357758 @name="A great movie", @price=1000>
irb(main):018> priduct.to_s
=> "#<Product:0x000000010e357758>"
irb(main):019> dvd = DVD.new("An awesome film", 3000, 120)
=> #<DVD:0x000000010e46ea38 @name="An awesome film", @price=3000, @running_time=120>
irb(main):020> dvd.to_s
=> "#<DVD:0x000000010e46ea38>"
irb(main):021* class Product
irb(main):022*   attr_reader :name, :price
irb(main):023*
irb(main):024*   def initialize(name, price)
irb(main):025*     @name = name
irb(main):026*     @price = price
irb(main):027*   end
irb(main):028*
irb(main):029*   def to_s
irb(main):030*     "name: #{name}, price: #{price}"
irb(main):031*   end
irb(main):032> end
=> :to_s
irb(main):033> dvd = DVD.new("An awesome film", 3000, 120)
=> #<DVD:0x000000010e4868e0 @name="An awesome film", @price=3000, @running_time=120>
irb(main):034> dvd.to_s
=> "name: An awesome film, price: 3000"

クラスメソッドの継承

クラスを継承すると、クラスメソッドも継承されます。

irb(main):035* class Foo
irb(main):036*   def self.hello
irb(main):037*     'hello'
irb(main):038*   end
irb(main):039> end
=> :hello

irb(main):041* class Bar < Foo
irb(main):042> end
=> nil
irb(main):043> Bar.hello
=> "hello"
irb(main):045> Foo.hello
=> "hello"

Railsのgithubを見てみる

メソッドのオーバーライド

 class UserProvidedDefault < FromUser # :nodoc:
      def initialize(name, value, type, database_default)
        @user_provided_value = value
        super(name, value, type, database_default)
      end
      ...

FromUserクラスのサブクラスUserProvidedDefaultクラスが継承してる
super(name, value, type, database_default)
多分value, type, database_defaultのいずれかがこのクラスで追加されているのだろう。

クラスメソッドの継承

      def with_type(type)
        self.class.new(name, user_provided_value, type, original_attribute)
      end

見つけることはできなかったがこのクラスを継承するとこのメソッドを使える

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?