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?

More than 1 year has passed since last update.

to_s

Posted at

何がsuperなのか

irb(main):001* module A
irb(main):002*   def to_s
irb(main):003*     "<A> #{super}"
irb(main):004*   end
irb(main):005> end
=> :to_s
irb(main):006* module B
irb(main):007*   def to_s
irb(main):008*     "<B> #{super}"
irb(main):009*   end
irb(main):010> end
=> :to_s
irb(main):011* class Product
irb(main):012*   prepend A
irb(main):013*   prepend B
irb(main):014*   def to_s
irb(main):015*     "<Product> #{super}"
irb(main):016*   end
irb(main):017> end
=> :to_s
irb(main):018> product = Product.new
=> #<Product:0x000000010cbc7d68>
irb(main):019> product.to_s
=> "<B> <A> <Product> #<Product:0x000000010cbc7d68>"

メソッド名も色々変更してみた

irb(main):002* module A
irb(main):003*   def hihi
irb(main):004*     "#{super}"
irb(main):005*   end
irb(main):006> end
=> :hihi
irb(main):007* class B
irb(main):008*   include A
irb(main):009*   def hihi
irb(main):010*     "#{super} #{super}"
irb(main):011*   end
irb(main):012> end
=> :hihi
irb(main):013> b = B.new
=> #<B:0x0000000104f5ec68>
irb(main):014> b.hihi
(irb):4:in `hihi': super: no superclass method `hihi' for #<B:0x0000000104f5ec68> (NoMethodError)

no superclass method

super [bug]
Kernelのメソッド内でsuperを呼んだ時に、存在しないsuperclass にアクセスしようとするバグの修正。

module Kernel def foo super end end

foo

# => ruby 1.8.3 (2005-09-21) [i686-linux] -:3:in foo': method foo' called on terminated object (0xb7e06970) (NotImplementedError) from -:7 # => ruby 1.8.4 (2005-12-22) [i686-linux] -:3:in foo'-:3: warning: too many arguments for format string : super: no superclass method foo' (NoMethodError) from -:7

irb(main):001* module A
irb(main):002*   def to_s
irb(main):003*     "#{super}"
irb(main):004*   end
irb(main):005> end
=> :to_s
irb(main):006* class B
irb(main):007*   include A
irb(main):008*   def to_s
irb(main):009*     "#{super} #{super}"
irb(main):010*   end
irb(main):011> end
=> :to_s
irb(main):012> b = B.new
=> #<B:0x000000011029f750>
irb(main):013> b.to_s
=> "#<B:0x000000011029f750> #<B:0x000000011029f750>"

to_sとは?

to_s -> String
オブジェクトの文字列表現を返します。

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?