U:RDoc::NormalClass[iI"Numeric:ET@I"Object;To:RDoc::Markup::Document:@parts[o;;[ :
@fileI"lib/mathn.rb;T:0@omit_headings_from_table_of_contents_below0o;;[o:RDoc::Markup::Paragraph;[I"UNumeric is the class from which all higher-level numeric classes should inherit.;To:RDoc::Markup::BlankLine o;;[I"`Numeric allows instantiation of heap-allocated objects. Other core numeric classes such as ;TI"`Integer are implemented as immediates, which means that each Integer is a single immutable ;TI",object which is always passed by value.;T@o:RDoc::Markup::Verbatim;[I"a = 1
;TI"0puts 1.object_id == a.object_id #=> true
;T:@format0o;;[I"\There can only ever be one instance of the integer +1+, for example. Ruby ensures this ;TI"]by preventing instantiation. If duplication is attempted, the same instance is returned.;T@o;
;[I"bInteger.new(1) #=> NoMethodError: undefined method `new' for Integer:Class
;TI",1.dup #=> 1
;TI"/1.object_id == 1.dup.object_id #=> true
;T;0o;;[I"QFor this reason, Numeric should be used when defining other numeric classes.;T@o;;[I"\Classes which inherit from Numeric must implement +coerce+, which returns a two-member ;TI"XArray containing an object that has been coerced into an instance of the new class ;TI"and +self+ (see #coerce).;T@o;;[ I"[Inheriting classes should also implement arithmetic operator methods (+
, ;TI"_-
, *
and /
) and the <=>
operator (see ;TI"UComparable). These methods may rely on +coerce+ to ensure interoperability with ;TI"(instances of other numeric classes.;T@o;
;[.I"class Tally < Numeric
;TI" def initialize(string)
;TI" @string = string
;TI" end
;TI"
;TI" def to_s
;TI" @string
;TI" end
;TI"
;TI" def to_i
;TI" @string.size
;TI" end
;TI"
;TI" def coerce(other)
;TI"2 [self.class.new('|' * other.to_i), self]
;TI" end
;TI"
;TI" def <=>(other)
;TI" to_i <=> other.to_i
;TI" end
;TI"
;TI" def +(other)
;TI"3 self.class.new('|' * (to_i + other.to_i))
;TI" end
;TI"
;TI" def -(other)
;TI"3 self.class.new('|' * (to_i - other.to_i))
;TI" end
;TI"
;TI" def *(other)
;TI"3 self.class.new('|' * (to_i * other.to_i))
;TI" end
;TI"
;TI" def /(other)
;TI"3 self.class.new('|' * (to_i / other.to_i))
;TI" end
;TI" end
;TI"
;TI"tally = Tally.new('||')
;TI"*puts tally * 2 #=> "||||"
;TI"'puts tally > 1 #=> true;T;0; I"numeric.c;T;
0; 0;
0[ [ [[I"Comparable;To;;[ ; @_;
0I"numeric.c;T[[I"
class;T[[:public[ [:protected[ [:private[ [I"
instance;T[[;[3[I"%;T@g[I"+@;T@g[I"-@;T@g[I"<=>;T@g[I"abs;T@g[I" abs2;TI"complex.c;T[I"
angle;T@~[I"arg;T@~[I" ceil;T@g[I"coerce;T@g[I" conj;T@~[I"conjugate;T@~[I"denominator;TI"rational.c;T[I"div;T@g[I"divmod;T@g[I" eql?;T@g[I" fdiv;T@g[I"finite?;T@g[I"
floor;T@g[I"i;T@g[I" imag;T@~[I"imaginary;T@~[I"infinite?;T@g[I"initialize_copy;T@g[I"
integer?;T@g[I"magnitude;T@g[I"modulo;T@g[I"negative?;T@g[I"
nonzero?;T@g[I"numerator;T@[I"
phase;T@~[I"
polar;T@~[I"positive?;T@g[I"quo;T@[I" real;T@~[I"
real?;T@g[I" rect;T@~[I"rectangular;T@~[I"remainder;T@g[I"
round;T@g[I"singleton_method_added;T@g[I" step;T@g[I" to_c;T@~[I"to_int;T@g[I"
truncate;T@g[I"
zero?;T@g[;[ [;[ [ [U:RDoc::Context::Section[i 0o;;[ ; 0;
0[ I"complex.c;T@
@_I"rational.c;T@cRDoc::TopLevel