git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@52038 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
svn 2015-10-05 06:42:18 +00:00
Родитель 2c5a139a84
Коммит 57149f76eb
1 изменённых файлов: 17 добавлений и 17 удалений

Просмотреть файл

@ -4074,69 +4074,69 @@ fix_even_p(VALUE num)
* Document-class: Numeric * Document-class: Numeric
* *
* Numeric is the class from which all higher-level numeric classes should inherit. * Numeric is the class from which all higher-level numeric classes should inherit.
* *
* Numeric allows instantiation of heap-allocated objects. Other core numeric classes such as * Numeric allows instantiation of heap-allocated objects. Other core numeric classes such as
* Integer are implemented as immediates, which means that each Integer is a single immutable * Integer are implemented as immediates, which means that each Integer is a single immutable
* object which is always passed by value. * object which is always passed by value.
* *
* a = 1 * a = 1
* puts 1.object_id == a.object_id #=> true * puts 1.object_id == a.object_id #=> true
* *
* There can only ever be one instance of the integer +1+, for example. Ruby ensures this * There can only ever be one instance of the integer +1+, for example. Ruby ensures this
* by preventing instantiation and duplication. * by preventing instantiation and duplication.
* *
* Integer.new(1) #=> NoMethodError: undefined method `new' for Integer:Class * Integer.new(1) #=> NoMethodError: undefined method `new' for Integer:Class
* 1.dup #=> TypeError: can't dup Fixnum * 1.dup #=> TypeError: can't dup Fixnum
* *
* For this reason, Numeric should be used when defining other numeric classes. * For this reason, Numeric should be used when defining other numeric classes.
* *
* Classes which inherit from Numeric must implement +coerce+, which returns a two-member * Classes which inherit from Numeric must implement +coerce+, which returns a two-member
* Array containing an object that has been coerced into an instance of the new class * Array containing an object that has been coerced into an instance of the new class
* and +self+ (see #coerce). * and +self+ (see #coerce).
* *
* Inheriting classes should also implement arithmetic operator methods (<code>+</code>, * Inheriting classes should also implement arithmetic operator methods (<code>+</code>,
* <code>-</code>, <code>*</code> and <code>/</code>) and the <code><=></code> operator (see * <code>-</code>, <code>*</code> and <code>/</code>) and the <code><=></code> operator (see
* Comparable). These methods may rely on +coerce+ to ensure interoperability with * Comparable). These methods may rely on +coerce+ to ensure interoperability with
* instances of other numeric classes. * instances of other numeric classes.
* *
* class Tally < Numeric * class Tally < Numeric
* def initialize(string) * def initialize(string)
* @string = string * @string = string
* end * end
* *
* def to_s * def to_s
* @string * @string
* end * end
* *
* def to_i * def to_i
* @string.size * @string.size
* end * end
* *
* def coerce(other) * def coerce(other)
* [self.class.new('|' * other.to_i), self] * [self.class.new('|' * other.to_i), self]
* end * end
* *
* def <=>(other) * def <=>(other)
* to_i <=> other.to_i * to_i <=> other.to_i
* end * end
* *
* def +(other) * def +(other)
* self.class.new('|' * (to_i + other.to_i)) * self.class.new('|' * (to_i + other.to_i))
* end * end
* *
* def -(other) * def -(other)
* self.class.new('|' * (to_i - other.to_i)) * self.class.new('|' * (to_i - other.to_i))
* end * end
* *
* def *(other) * def *(other)
* self.class.new('|' * (to_i * other.to_i)) * self.class.new('|' * (to_i * other.to_i))
* end * end
* *
* def /(other) * def /(other)
* self.class.new('|' * (to_i / other.to_i)) * self.class.new('|' * (to_i / other.to_i))
* end * end
* end * end
* *
* tally = Tally.new('||') * tally = Tally.new('||')
* puts tally * 2 #=> "||||" * puts tally * 2 #=> "||||"
* puts tally > 1 #=> true * puts tally > 1 #=> true