{: rooto:"YARD::CodeObjects::RootObject:@childrenIC:&YARD::CodeObjects::CodeObjectList[o:#YARD::CodeObjects::ClassObject;IC;[@o:$YARD::CodeObjects::MethodObject:@module_functionF:@scope:
instance:@visibility:public:
@pathI"Float#arg:EF:@parameters[ :@files[[I"complex.c;Ti:@current_file_has_commentsT:
@name:arg:@source_type0:
@tags[ :@docstrings{ :@docstringIC:YARD::Docstring"6Returns 0 if the value is positive, pi otherwise.
;T;[o:YARD::Tags::OverloadTag
:@tag_nameI"
overload;F:
@text0;;:@types0:@signatureI"arg;T;IC;" ;T;[o:YARD::Tags::Tag
;I"return;F;I" ;T;0;[I"0;TI"
Float;T:@object@
:@ref_tags[ : @allI"@return [ 0, Float];T:@unresolved_reference0;"@
:@hash_flagF:@ref_tag_recurse_counti :
@summary0;[ ;"@
o;
;I"
overload;F;0;:
angle;0; I"
angle;T;IC;" ;T;[o;!
;I"return;F;I" ;T;0;[I"0;TI"
Float;T;"@
;#[ ;$I"@return [ 0, Float];T;%0;"@
;&F;'i ;(0;[ ;"@
o;
;I"
overload;F;0;:
phase;0; I"
phase;T;IC;" ;T;[o;!
;I"return;F;I" ;T;0;[I"0;TI"
Float;T;"@
;#[ ;$I"@return [ 0, Float];T;%0;"@
;&F;'i ;(0;[ ;"@
;#[ ;$I"Returns 0 if the value is positive, pi otherwise.
@overload arg
@return [ 0, Float]
@overload angle
@return [ 0, Float]
@overload phase
@return [ 0, Float];T;%0;"@
;&F:@line_rangeo:
Range: exclT:
begini:endi:@namespace@:@sourceI"static VALUE
float_arg(VALUE self)
{
if (isnan(RFLOAT_VALUE(self)))
return self;
if (f_tpositive_p(self))
return INT2FIX(0);
return rb_const_get(rb_mMath, id_PI);
};T; I"static VALUE;T:@explicitTo;
;F;;
;;;I"Float#angle;F;[ ;[[@i;T;;);0;[ ;{ ;IC;"6Returns 0 if the value is positive, pi otherwise.
;T;[o;
;I"
overload;F;0;;;0; I"arg;T;IC;" ;T;[o;!
;I"return;F;I" ;T;0;[I"0;TI"
Float;T;"@C;#[ ;$I"@return [ 0, Float];T;%0;"@C;&F;'i ;(0;[ ;"@Co;
;I"
overload;F;0;;);0; I"
angle;T;IC;" ;T;[o;!
;I"return;F;I" ;T;0;[I"0;TI"
Float;T;"@C;#[ ;$I"@return [ 0, Float];T;%0;"@C;&F;'i ;(0;[ ;"@Co;
;I"
overload;F;0;;*;0; I"
phase;T;IC;" ;T;[o;!
;I"return;F;I" ;T;0;[I"0;TI"
Float;T;"@C;#[ ;$I"@return [ 0, Float];T;%0;"@C;&F;'i ;(0;[ ;"@C;#[ ;$@?;%0;"@C;&F;+o;,;-T;.i;/i;0@;1I"static VALUE
float_arg(VALUE self)
{
if (isnan(RFLOAT_VALUE(self)))
return self;
if (f_tpositive_p(self))
return INT2FIX(0);
return rb_const_get(rb_mMath, id_PI);
};T; I"static VALUE;T;2To;
;F;;
;;;I"Float#phase;F;[ ;[[@i;T;;*;0;[ ;{ ;IC;"6Returns 0 if the value is positive, pi otherwise.
;T;[o;
;I"
overload;F;0;;;0; I"arg;T;IC;" ;T;[o;!
;I"return;F;I" ;T;0;[I"0;TI"
Float;T;"@z;#[ ;$I"@return [ 0, Float];T;%0;"@z;&F;'i ;(0;[ ;"@zo;
;I"
overload;F;0;;);0; I"
angle;T;IC;" ;T;[o;!
;I"return;F;I" ;T;0;[I"0;TI"
Float;T;"@z;#[ ;$I"@return [ 0, Float];T;%0;"@z;&F;'i ;(0;[ ;"@zo;
;I"
overload;F;0;;*;0; I"
phase;T;IC;" ;T;[o;!
;I"return;F;I" ;T;0;[I"0;TI"
Float;T;"@z;#[ ;$I"@return [ 0, Float];T;%0;"@z;&F;'i ;(0;[ ;"@z;#[ ;$@?;%0;"@z;&F;+o;,;-T;.i;/i;0@;1I"static VALUE
float_arg(VALUE self)
{
if (isnan(RFLOAT_VALUE(self)))
return self;
if (f_tpositive_p(self))
return INT2FIX(0);
return rb_const_get(rb_mMath, id_PI);
};T; I"static VALUE;T;2To;
;F;;
;;;I"Float#numerator;F;[ ;[[I"rational.c;Ti;T;:numerator;0;[ ;{ ;IC;"Returns the numerator. The result is machine dependent.
n = 0.3.numerator #=> 5404319552844595
d = 0.3.denominator #=> 18014398509481984
n.fdiv(d) #=> 0.3
See also Float#denominator.
;T;[o;
;I"
overload;F;0;;3;0; I"numerator;T;IC;" ;T;[o;!
;I"return;F;I" ;T;0;[I"Integer;T;"@;#[ ;$I"@return [Integer];T;%0;"@;&F;'i ;(0;[ ;"@;#[ ;$I"Returns the numerator. The result is machine dependent.
n = 0.3.numerator #=> 5404319552844595
d = 0.3.denominator #=> 18014398509481984
n.fdiv(d) #=> 0.3
See also Float#denominator.
@overload numerator
@return [Integer];T;%0;"@;&F;+o;,;-T;.i;/i;0@;1I"VALUE
rb_float_numerator(VALUE self)
{
double d = RFLOAT_VALUE(self);
VALUE r;
if (isinf(d) || isnan(d))
return self;
r = float_to_r(self);
return nurat_numerator(r);
};T; I"
VALUE;T;2To;
;F;;
;;;I"Float#denominator;F;[ ;[[@i2;T;:denominator;0;[ ;{ ;IC;"lReturns the denominator (always positive). The result is machine
dependent.
See also Float#numerator.
;T;[o;
;I"
overload;F;0;;4;0; I"denominator;T;IC;" ;T;[o;!
;I"return;F;I" ;T;0;[I"Integer;T;"@;#[ ;$I"@return [Integer];T;%0;"@;&F;'i ;(0;[ ;"@;#[ ;$I"Returns the denominator (always positive). The result is machine
dependent.
See also Float#numerator.
@overload denominator
@return [Integer];T;%0;"@;&F;+o;,;-T;.i);/i0;0@;1I"VALUE
rb_float_denominator(VALUE self)
{
double d = RFLOAT_VALUE(self);
VALUE r;
if (isinf(d) || isnan(d))
return INT2FIX(1);
r = float_to_r(self);
return nurat_denominator(r);
};T; I"
VALUE;T;2To;
;F;;
;;;I"Float#to_r;F;[ ;[[@i;T;: to_r;0;[ ;{ ;IC;"Returns the value as a rational.
2.0.to_r #=> (2/1)
2.5.to_r #=> (5/2)
-0.75.to_r #=> (-3/4)
0.0.to_r #=> (0/1)
0.3.to_r #=> (5404319552844595/18014398509481984)
NOTE: 0.3.to_r isn't the same as "0.3".to_r. The latter is
equivalent to "3/10".to_r, but the former isn't so.
0.3.to_r == 3/10r #=> false
"0.3".to_r == 3/10r #=> true
See also Float#rationalize.
;T;[o;
;I"
overload;F;0;;5;0; I" to_r;T;IC;" ;T;[ ;#[ ;$I" ;T;%0;"@;&F;'i ;(0;[ ;"@;#[ ;$I"Returns the value as a rational.
2.0.to_r #=> (2/1)
2.5.to_r #=> (5/2)
-0.75.to_r #=> (-3/4)
0.0.to_r #=> (0/1)
0.3.to_r #=> (5404319552844595/18014398509481984)
NOTE: 0.3.to_r isn't the same as "0.3".to_r. The latter is
equivalent to "3/10".to_r, but the former isn't so.
0.3.to_r == 3/10r #=> false
"0.3".to_r == 3/10r #=> true
See also Float#rationalize.
@overload to_r;T;%0;"@;&F;+o;,;-T;.i;/i;0@;1I"static VALUE
float_to_r(VALUE self)
{
VALUE f;
int n;
float_decode_internal(self, &f, &n);
#if FLT_RADIX == 2
if (n == 0)
return rb_rational_new1(f);
if (n > 0)
return rb_rational_new1(rb_int_lshift(f, INT2FIX(n)));
n = -n;
return rb_rational_new2(f, rb_int_lshift(ONE, INT2FIX(n)));
#else
f = rb_int_mul(f, rb_int_pow(INT2FIX(FLT_RADIX), n));
if (RB_TYPE_P(f, T_RATIONAL))
return f;
return rb_rational_new1(f);
#endif
};T; I"static VALUE;T;2To;
;F;;
;;;I"Float#rationalize;F;[[I"
*args;T0;[[@i;T;:rationalize;0;[ ;{ ;IC;"/Returns a simpler approximation of the value (flt-|eps| <= result
<= flt+|eps|). If the optional argument +eps+ is not given,
it will be chosen automatically.
0.3.rationalize #=> (3/10)
1.333.rationalize #=> (1333/1000)
1.333.rationalize(0.01) #=> (4/3)
See also Float#to_r.
;T;[o;
;I"
overload;F;0;;6;0; I"rationalize([eps]);T;IC;" ;T;[ ;#[ ;$I" ;T;%0;"@;&F;'i ;(0;[[I"
[eps];T0;"@;#[ ;$I"NReturns a simpler approximation of the value (flt-|eps| <= result
<= flt+|eps|). If the optional argument +eps+ is not given,
it will be chosen automatically.
0.3.rationalize #=> (3/10)
1.333.rationalize #=> (1333/1000)
1.333.rationalize(0.01) #=> (4/3)
See also Float#to_r.
@overload rationalize([eps]);T;%0;"@;&F;+o;,;-T;.i;/i;0@;1I"static VALUE
float_rationalize(int argc, VALUE *argv, VALUE self)
{
double d = RFLOAT_VALUE(self);
VALUE rat;
int neg = d < 0.0;
if (neg) self = DBL2NUM(-d);
if (rb_check_arity(argc, 0, 1)) {
rat = rb_flt_rationalize_with_prec(self, argv[0]);
}
else {
rat = rb_flt_rationalize(self);
}
if (neg) RATIONAL_SET_NUM(rat, rb_int_uminus(RRATIONAL(rat)->num));
return rat;
};T; I"static VALUE;T;2To:&YARD::CodeObjects::ConstantObject;[[I"numeric.c;Ti;F;:
RADIX;:c;;;[ ;{ ;IC;"The base of the floating point, or number of unique digits used to
represent the number.
Usually defaults to 2 on most systems, which would represent a base-10 decimal.
;T;[ ;#[ ;$I"The base of the floating point, or number of unique digits used to
represent the number.
Usually defaults to 2 on most systems, which would represent a base-10 decimal.
;T;%0;"@;&F;+o;,;-T;.i;/i;0@;I"Float::RADIX;F:@valueI"INT2FIX(FLT_RADIX);To;7;[[@i;F;:
MANT_DIG;;9;;;[ ;{ ;IC;"SThe number of base digits for the +double+ data type.
Usually defaults to 53.
;T;[ ;#[ ;$I"TThe number of base digits for the +double+ data type.
Usually defaults to 53.
;T;%0;"@ ;&F;+o;,;-T;.i;/i;0@;I"Float::MANT_DIG;F;:I"INT2FIX(DBL_MANT_DIG);To;7;[[@i;F;:DIG;;9;;;[ ;{ ;IC;"tThe minimum number of significant decimal digits in a double-precision
floating point.
Usually defaults to 15.
;T;[ ;#[ ;$I"uThe minimum number of significant decimal digits in a double-precision
floating point.
Usually defaults to 15.
;T;%0;"@,;&F;+o;,;-T;.i;/i;0@;I"Float::DIG;F;:I"INT2FIX(DBL_DIG);To;7;[[@i;F;:MIN_EXP;;9;;;[ ;{ ;IC;"kThe smallest possible exponent value in a double-precision floating
point.
Usually defaults to -1021.
;T;[ ;#[ ;$I"lThe smallest possible exponent value in a double-precision floating
point.
Usually defaults to -1021.
;T;%0;"@8;&F;+o;,;-T;.i;/i;0@;I"Float::MIN_EXP;F;:I"INT2FIX(DBL_MIN_EXP);To;7;[[@i;F;:MAX_EXP;;9;;;[ ;{ ;IC;"iThe largest possible exponent value in a double-precision floating
point.
Usually defaults to 1024.
;T;[ ;#[ ;$I"jThe largest possible exponent value in a double-precision floating
point.
Usually defaults to 1024.
;T;%0;"@D;&F;+o;,;-T;.i;/i;0@;I"Float::MAX_EXP;F;:I"INT2FIX(DBL_MAX_EXP);To;7;[[@i;F;:MIN_10_EXP;;9;;;[ ;{ ;IC;"The smallest negative exponent in a double-precision floating point
where 10 raised to this power minus 1.
Usually defaults to -307.
;T;[ ;#[ ;$I"The smallest negative exponent in a double-precision floating point
where 10 raised to this power minus 1.
Usually defaults to -307.
;T;%0;"@P;&F;+o;,;-T;.i;/i;0@;I"Float::MIN_10_EXP;F;:I"INT2FIX(DBL_MIN_10_EXP);To;7;[[@i;F;:MAX_10_EXP;;9;;;[ ;{ ;IC;"The largest positive exponent in a double-precision floating point where
10 raised to this power minus 1.
Usually defaults to 308.
;T;[ ;#[ ;$I"The largest positive exponent in a double-precision floating point where
10 raised to this power minus 1.
Usually defaults to 308.
;T;%0;"@\;&F;+o;,;-T;.i;/i ;0@;I"Float::MAX_10_EXP;F;:I"INT2FIX(DBL_MAX_10_EXP);To;7;[[@i;F;:MIN;;9;;;[ ;{ ;IC;"m:MIN.
0.0.next_float returns the smallest positive floating point number
including denormalized numbers.
;T;[ ;#[ ;$I"n:MIN.
0.0.next_float returns the smallest positive floating point number
including denormalized numbers.
;T;%0;"@h;&F;+o;,;-T;.i;/i;0@;I"Float::MIN;F;:I"The smallest positive normalized number in a double-precision floating point.
Usually defaults to 2.2250738585072014e-308.
If the platform supports denormalized numbers,
there are numbers between zero and Float;To;7;[[@i;F;:MAX;;9;;;[ ;{ ;IC;"|The largest possible integer in a double-precision floating point number.
Usually defaults to 1.7976931348623157e+308.
;T;[ ;#[ ;$I"}The largest possible integer in a double-precision floating point number.
Usually defaults to 1.7976931348623157e+308.
;T;%0;"@t;&F;+o;,;-T;.i;/i;0@;I"Float::MAX;F;:I"DBL2NUM(DBL_MAX);To;7;[[@i#;F;:EPSILON;;9;;;[ ;{ ;IC;"The difference between 1 and the smallest double-precision floating
point number greater than 1.
Usually defaults to 2.2204460492503131e-16.
;T;[ ;#[ ;$I"The difference between 1 and the smallest double-precision floating
point number greater than 1.
Usually defaults to 2.2204460492503131e-16.
;T;%0;"@;&F;+o;,;-T;.i;/i!;0@;I"Float::EPSILON;F;:I"DBL2NUM(DBL_EPSILON);To;7;[[@i';F;:
INFINITY;;9;;;[ ;{ ;IC;"2An expression representing positive infinity.
;T;[ ;#[ ;$I"3An expression representing positive infinity.
;T;%0;"@;&F;+o;,;-T;.i$;/i%;0@;I"Float::INFINITY;F;:I"DBL2NUM(HUGE_VAL);To;7;[[@i+;F;:NAN;;9;;;[ ;{ ;IC;"@An expression representing a value which is "not a number".
;T;[ ;#[ ;$I"AAn expression representing a value which is "not a number".
;T;%0;"@;&F;+o;,;-T;.i(;/i);0@;I"Float::NAN;F;:I"DBL2NUM(nan(""));To;
;F;;
;;;I"Float#to_s;F;[ ;[[@i;T;: to_s;0;[ ;{ ;IC;"Returns a string containing a representation of +self+.
As well as a fixed or exponential form of the +float+,
the call may return +NaN+, +Infinity+, and +-Infinity+.
;T;[o;
;I"
overload;F;0;;F;0; I" to_s;T;IC;" ;T;[o;!
;I"return;F;I" ;T;0;[I"String;T;"@;#[ ;$I"@return [String];T;%0;"@;&F;'i ;(0;[ ;"@;#[ ;$I"Returns a string containing a representation of +self+.
As well as a fixed or exponential form of the +float+,
the call may return +NaN+, +Infinity+, and +-Infinity+.
@overload to_s
@return [String];T;%0;"o;
;F;;
;;;I"Float#inspect;F;[ ;[[@i.;F;:inspect;: ruby;[ ;{ ;@;0@;1I"static VALUE
flo_to_s(VALUE flt)
{
enum {decimal_mant = DBL_MANT_DIG-DBL_DIG};
enum {float_dig = DBL_DIG+1};
char buf[float_dig + (decimal_mant + CHAR_BIT - 1) / CHAR_BIT + 10];
double value = RFLOAT_VALUE(flt);
VALUE s;
char *p, *e;
int sign, decpt, digs;
if (isinf(value)) {
static const char minf[] = "-Infinity";
const int pos = (value > 0); /* skip "-" */
return rb_usascii_str_new(minf+pos, strlen(minf)-pos);
}
else if (isnan(value))
return rb_usascii_str_new2("NaN");
p = ruby_dtoa(value, 0, 0, &decpt, &sign, &e);
s = sign ? rb_usascii_str_new_cstr("-") : rb_usascii_str_new(0, 0);
if ((digs = (int)(e - p)) >= (int)sizeof(buf)) digs = (int)sizeof(buf) - 1;
memcpy(buf, p, digs);
xfree(p);
if (decpt > 0) {
if (decpt < digs) {
memmove(buf + decpt + 1, buf + decpt, digs - decpt);
buf[decpt] = '.';
rb_str_cat(s, buf, digs + 1);
}
else if (decpt <= DBL_DIG) {
long len;
char *ptr;
rb_str_cat(s, buf, digs);
rb_str_resize(s, (len = RSTRING_LEN(s)) + decpt - digs + 2);
ptr = RSTRING_PTR(s) + len;
if (decpt > digs) {
memset(ptr, '0', decpt - digs);
ptr += decpt - digs;
}
memcpy(ptr, ".0", 2);
}
else {
goto exp;
}
}
else if (decpt > -4) {
long len;
char *ptr;
rb_str_cat(s, "0.", 2);
rb_str_resize(s, (len = RSTRING_LEN(s)) - decpt + digs);
ptr = RSTRING_PTR(s);
memset(ptr += len, '0', -decpt);
memcpy(ptr -= decpt, buf, digs);
}
else {
goto exp;
}
return s;
exp:
if (digs > 1) {
memmove(buf + 2, buf + 1, digs - 1);
}
else {
buf[2] = '0';
digs++;
}
buf[1] = '.';
rb_str_cat(s, buf, digs + 1);
rb_str_catf(s, "e%+03d", decpt - 1);
return s;
};T; I"static VALUE;T;&F;+o;,;-T;.i;/i;0@;1I"static VALUE
flo_to_s(VALUE flt)
{
enum {decimal_mant = DBL_MANT_DIG-DBL_DIG};
enum {float_dig = DBL_DIG+1};
char buf[float_dig + (decimal_mant + CHAR_BIT - 1) / CHAR_BIT + 10];
double value = RFLOAT_VALUE(flt);
VALUE s;
char *p, *e;
int sign, decpt, digs;
if (isinf(value)) {
static const char minf[] = "-Infinity";
const int pos = (value > 0); /* skip "-" */
return rb_usascii_str_new(minf+pos, strlen(minf)-pos);
}
else if (isnan(value))
return rb_usascii_str_new2("NaN");
p = ruby_dtoa(value, 0, 0, &decpt, &sign, &e);
s = sign ? rb_usascii_str_new_cstr("-") : rb_usascii_str_new(0, 0);
if ((digs = (int)(e - p)) >= (int)sizeof(buf)) digs = (int)sizeof(buf) - 1;
memcpy(buf, p, digs);
xfree(p);
if (decpt > 0) {
if (decpt < digs) {
memmove(buf + decpt + 1, buf + decpt, digs - decpt);
buf[decpt] = '.';
rb_str_cat(s, buf, digs + 1);
}
else if (decpt <= DBL_DIG) {
long len;
char *ptr;
rb_str_cat(s, buf, digs);
rb_str_resize(s, (len = RSTRING_LEN(s)) + decpt - digs + 2);
ptr = RSTRING_PTR(s) + len;
if (decpt > digs) {
memset(ptr, '0', decpt - digs);
ptr += decpt - digs;
}
memcpy(ptr, ".0", 2);
}
else {
goto exp;
}
}
else if (decpt > -4) {
long len;
char *ptr;
rb_str_cat(s, "0.", 2);
rb_str_resize(s, (len = RSTRING_LEN(s)) - decpt + digs);
ptr = RSTRING_PTR(s);
memset(ptr += len, '0', -decpt);
memcpy(ptr -= decpt, buf, digs);
}
else {
goto exp;
}
return s;
exp:
if (digs > 1) {
memmove(buf + 2, buf + 1, digs - 1);
}
else {
buf[2] = '0';
digs++;
}
buf[1] = '.';
rb_str_cat(s, buf, digs + 1);
rb_str_catf(s, "e%+03d", decpt - 1);
return s;
};T; @;2T@o;
;F;;
;;;I"Float#coerce;F;[[I"y;T0;[[@i;T;:coerce;0;[ ;{ ;IC;"Returns an array with both +numeric+ and +float+ represented as Float
objects.
This is achieved by converting +numeric+ to a Float.
1.2.coerce(3) #=> [3.0, 1.2]
2.5.coerce(1.1) #=> [1.1, 2.5]
;T;[o;
;I"
overload;F;0;;I;0; I"coerce(numeric);T;IC;" ;T;[o;!
;I"return;F;I" ;T;0;[I"
Array;T;"@;#[ ;$I"@return [Array];T;%0;"@;&F;'i ;(0;[[I"numeric;T0;"@;#[ ;$I"Returns an array with both +numeric+ and +float+ represented as Float
objects.
This is achieved by converting +numeric+ to a Float.
1.2.coerce(3) #=> [3.0, 1.2]
2.5.coerce(1.1) #=> [1.1, 2.5]
@overload coerce(numeric)
@return [Array];T;%0;"@;&F;+o;,;-T;.i;/i ;0@;1I"[static VALUE
flo_coerce(VALUE x, VALUE y)
{
return rb_assoc_new(rb_Float(y), x);
};T; I"static VALUE;T;2To;
;F;;
;;;I"
Float#-@;F;[ ;[[@i;T;:-@;0;[ ;{ ;IC;"Returns +float+, negated.
;T;[o;
;I"
overload;F;0;:-;0; I"-float;T;IC;" ;T;[o;!
;I"return;F;I" ;T;0;[I"
Float;T;"@;#[ ;$I"@return [Float];T;%0;"@;&F;'i ;(0;[ ;"@;#[ ;$I"CReturns +float+, negated.
@overload -float
@return [Float];T;%0;"@;&F;+o;,;-T;.i ;/i
;0@;1I"QVALUE
rb_float_uminus(VALUE flt)
{
return DBL2NUM(-RFLOAT_VALUE(flt));
};T; I"
VALUE;T;2To;
;F;;
;;;I"Float#+;F;[[I"y;T0;[[@i;T;:+;0;[ ;{ ;IC;"AReturns a new Float which is the sum of +float+ and +other+.
;T;[o;
;I"
overload;F;0;;L;0; I"
+(other);T;IC;" ;T;[o;!
;I"return;F;I" ;T;0;[I"
Float;T;"@;#[ ;$I"@return [Float];T;%0;"@;&F;'i ;(0;[[I"
other;T0;"@;#[ ;$I"hReturns a new Float which is the sum of +float+ and +other+.
@overload +(other)
@return [Float];T;%0;"@;&F;+o;,;-T;.i;/i;0@;1I"VALUE
rb_float_plus(VALUE x, VALUE y)
{
if (RB_TYPE_P(y, T_FIXNUM)) {
return DBL2NUM(RFLOAT_VALUE(x) + (double)FIX2LONG(y));
}
else if (RB_TYPE_P(y, T_BIGNUM)) {
return DBL2NUM(RFLOAT_VALUE(x) + rb_big2dbl(y));
}
else if (RB_TYPE_P(y, T_FLOAT)) {
return DBL2NUM(RFLOAT_VALUE(x) + RFLOAT_VALUE(y));
}
else {
return rb_num_coerce_bin(x, y, '+');
}
};T; I"
VALUE;T;2To;
;F;;
;;;I"Float#-;F;[[I"y;T0;[[@i5;T;;K;0;[ ;{ ;IC;"HReturns a new Float which is the difference of +float+ and +other+.
;T;[o;
;I"
overload;F;0;;K;0; I"
-(other);T;IC;" ;T;[o;!
;I"return;F;I" ;T;0;[I"
Float;T;"@ ;#[ ;$I"@return [Float];T;%0;"@ ;&F;'i ;(0;[[I"
other;T0;"@ ;#[ ;$I"oReturns a new Float which is the difference of +float+ and +other+.
@overload -(other)
@return [Float];T;%0;"@ ;&F;+o;,;-T;.i.;/i2;0@;1I"VALUE
rb_float_minus(VALUE x, VALUE y)
{
if (RB_TYPE_P(y, T_FIXNUM)) {
return DBL2NUM(RFLOAT_VALUE(x) - (double)FIX2LONG(y));
}
else if (RB_TYPE_P(y, T_BIGNUM)) {
return DBL2NUM(RFLOAT_VALUE(x) - rb_big2dbl(y));
}
else if (RB_TYPE_P(y, T_FLOAT)) {
return DBL2NUM(RFLOAT_VALUE(x) - RFLOAT_VALUE(y));
}
else {
return rb_num_coerce_bin(x, y, '-');
}
};T; I"
VALUE;T;2To;
;F;;
;;;I"Float#*;F;[[I"y;T0;[[@iM;T;:*;0;[ ;{ ;IC;"EReturns a new Float which is the product of +float+ and +other+.
;T;[o;
;I"
overload;F;0;;M;0; I"
*(other);T;IC;" ;T;[o;!
;I"return;F;I" ;T;0;[I"
Float;T;"@?;#[ ;$I"@return [Float];T;%0;"@?;&F;'i ;(0;[[I"
other;T0;"@?;#[ ;$I"lReturns a new Float which is the product of +float+ and +other+.
@overload *(other)
@return [Float];T;%0;"@?;&F;+o;,;-T;.iF;/iJ;0@;1I"~VALUE
rb_float_mul(VALUE x, VALUE y)
{
if (RB_TYPE_P(y, T_FIXNUM)) {
return DBL2NUM(RFLOAT_VALUE(x) * (double)FIX2LONG(y));
}
else if (RB_TYPE_P(y, T_BIGNUM)) {
return DBL2NUM(RFLOAT_VALUE(x) * rb_big2dbl(y));
}
else if (RB_TYPE_P(y, T_FLOAT)) {
return DBL2NUM(RFLOAT_VALUE(x) * RFLOAT_VALUE(y));
}
else {
return rb_num_coerce_bin(x, y, '*');
}
};T; I"
VALUE;T;2To;
;F;;
;;;I"Float#/;F;[[I"y;T0;[[@i;T;:/;0;[ ;{ ;IC;"LReturns a new Float which is the result of dividing +float+ by +other+.
;T;[o;
;I"
overload;F;0;;N;0; I"
/(other);T;IC;" ;T;[o;!
;I"return;F;I" ;T;0;[I"
Float;T;"@^;#[ ;$I"@return [Float];T;%0;"@^;&F;'i ;(0;[[I"
other;T0;"@^;#[ ;$I"sReturns a new Float which is the result of dividing +float+ by +other+.
@overload /(other)
@return [Float];T;%0;"@^;&F;+o;,;-T;.i|;/i;0@;1I"VALUE
rb_float_div(VALUE x, VALUE y)
{
double num = RFLOAT_VALUE(x);
double den;
double ret;
if (RB_TYPE_P(y, T_FIXNUM)) {
den = FIX2LONG(y);
}
else if (RB_TYPE_P(y, T_BIGNUM)) {
den = rb_big2dbl(y);
}
else if (RB_TYPE_P(y, T_FLOAT)) {
den = RFLOAT_VALUE(y);
}
else {
return rb_num_coerce_bin(x, y, '/');
}
ret = double_div_double(num, den);
return DBL2NUM(ret);
};T; I"
VALUE;T;2To;
;F;;
;;;I"Float#quo;F;[[I"y;T0;[[@i;T;:quo;0;[ ;{ ;IC;";Returns float / numeric
, same as Float#/.
;T;[o;
;I"
overload;F;0;: fdiv;0; I"fdiv(numeric);T;IC;" ;T;[o;!
;I"return;F;I" ;T;0;[I"
Float;T;"@};#[ ;$I"@return [Float];T;%0;"@};&F;'i ;(0;[[I"numeric;T0;"@}o;
;I"
overload;F;0;;O;0; I"quo(numeric);T;IC;" ;T;[o;!
;I"return;F;I" ;T;0;[I"
Float;T;"@};#[ ;$I"@return [Float];T;%0;"@};&F;'i ;(0;[[I"numeric;T0;"@};#[ ;$I"Returns float / numeric
, same as Float#/.
@overload fdiv(numeric)
@return [Float]
@overload quo(numeric)
@return [Float];T;%0;"@};&F;+o;,;-T;.i;/i;0@;1I"Sstatic VALUE
flo_quo(VALUE x, VALUE y)
{
return num_funcall1(x, '/', y);
};T; I"static VALUE;T;2To;
;F;;
;;;I"Float#fdiv;F;[[I"y;T0;[[@i;T;;P;0;[ ;{ ;IC;";Returns float / numeric
, same as Float#/.
;T;[o;
;I"
overload;F;0;;P;0; I"fdiv(numeric);T;IC;" ;T;[o;!
;I"return;F;I" ;T;0;[I"
Float;T;"@;#[ ;$I"@return [Float];T;%0;"@;&F;'i ;(0;[[I"numeric;T0;"@o;
;I"
overload;F;0;;O;0; I"quo(numeric);T;IC;" ;T;[o;!
;I"return;F;I" ;T;0;[I"
Float;T;"@;#[ ;$I"@return [Float];T;%0;"@;&F;'i ;(0;[[I"numeric;T0;"@;#[ ;$@;%0;"@;&F;+o;,;-T;.i;/i;0@;1I"Sstatic VALUE
flo_quo(VALUE x, VALUE y)
{
return num_funcall1(x, '/', y);
};T; I"static VALUE;T;2To;
;F;;
;;;I"Float#%;F;[[I"y;T0;[[@i;T;:%;0;[ ;{ ;IC;"Returns the modulo after division of +float+ by +other+.
6543.21.modulo(137) #=> 104.21000000000004
6543.21.modulo(137.24) #=> 92.92999999999961
;T;[o;
;I"
overload;F;0;;Q;0; I"
%(other);T;IC;" ;T;[o;!
;I"return;F;I" ;T;0;[I"
Float;T;"@;#[ ;$I"@return [Float];T;%0;"@;&F;'i ;(0;[[I"
other;T0;"@o;
;I"
overload;F;0;:modulo;0; I"modulo(other);T;IC;" ;T;[o;!
;I"return;F;I" ;T;0;[I"
Float;T;"@;#[ ;$I"@return [Float];T;%0;"@;&F;'i ;(0;[[I"
other;T0;"@;#[ ;$I"Returns the modulo after division of +float+ by +other+.
6543.21.modulo(137) #=> 104.21000000000004
6543.21.modulo(137.24) #=> 92.92999999999961
@overload %(other)
@return [Float]
@overload modulo(other)
@return [Float];T;%0;"@;&F;+o;,;-T;.i;/i;0@;1I"rstatic VALUE
flo_mod(VALUE x, VALUE y)
{
double fy;
if (RB_TYPE_P(y, T_FIXNUM)) {
fy = (double)FIX2LONG(y);
}
else if (RB_TYPE_P(y, T_BIGNUM)) {
fy = rb_big2dbl(y);
}
else if (RB_TYPE_P(y, T_FLOAT)) {
fy = RFLOAT_VALUE(y);
}
else {
return rb_num_coerce_bin(x, y, '%');
}
return DBL2NUM(ruby_float_mod(RFLOAT_VALUE(x), fy));
};T; I"static VALUE;T;2To;
;F;;
;;;I"Float#modulo;F;[[I"y;T0;[[@i;T;;R;0;[ ;{ ;IC;"Returns the modulo after division of +float+ by +other+.
6543.21.modulo(137) #=> 104.21000000000004
6543.21.modulo(137.24) #=> 92.92999999999961
;T;[o;
;I"
overload;F;0;;Q;0; I"
%(other);T;IC;" ;T;[o;!
;I"return;F;I" ;T;0;[I"
Float;T;"@;#[ ;$I"@return [Float];T;%0;"@;&F;'i ;(0;[[I"
other;T0;"@o;
;I"
overload;F;0;;R;0; I"modulo(other);T;IC;" ;T;[o;!
;I"return;F;I" ;T;0;[I"
Float;T;"@;#[ ;$I"@return [Float];T;%0;"@;&F;'i ;(0;[[I"
other;T0;"@;#[ ;$@;%0;"@;&F;+o;,;-T;.i;/i;0@;1I"rstatic VALUE
flo_mod(VALUE x, VALUE y)
{
double fy;
if (RB_TYPE_P(y, T_FIXNUM)) {
fy = (double)FIX2LONG(y);
}
else if (RB_TYPE_P(y, T_BIGNUM)) {
fy = rb_big2dbl(y);
}
else if (RB_TYPE_P(y, T_FLOAT)) {
fy = RFLOAT_VALUE(y);
}
else {
return rb_num_coerce_bin(x, y, '%');
}
return DBL2NUM(ruby_float_mod(RFLOAT_VALUE(x), fy));
};T; I"static VALUE;T;2To;
;F;;
;;;I"Float#divmod;F;[[I"y;T0;[[@i;T;:divmod;0;[ ;{ ;IC;"[See Numeric#divmod.
42.0.divmod(6) #=> [7, 0.0]
42.0.divmod(5) #=> [8, 2.0]
;T;[o;
;I"
overload;F;0;;S;0; I"divmod(numeric);T;IC;" ;T;[o;!
;I"return;F;I" ;T;0;[I"
Array;T;"@3;#[ ;$I"@return [Array];T;%0;"@3;&F;'i ;(0;[[I"numeric;T0;"@3;#[ ;$I"See Numeric#divmod.
42.0.divmod(6) #=> [7, 0.0]
42.0.divmod(5) #=> [8, 2.0]
@overload divmod(numeric)
@return [Array];T;%0;"@3;&F;+o;,;-T;.i;/i;0@;1I"static VALUE
flo_divmod(VALUE x, VALUE y)
{
double fy, div, mod;
volatile VALUE a, b;
if (RB_TYPE_P(y, T_FIXNUM)) {
fy = (double)FIX2LONG(y);
}
else if (RB_TYPE_P(y, T_BIGNUM)) {
fy = rb_big2dbl(y);
}
else if (RB_TYPE_P(y, T_FLOAT)) {
fy = RFLOAT_VALUE(y);
}
else {
return rb_num_coerce_bin(x, y, id_divmod);
}
flodivmod(RFLOAT_VALUE(x), fy, &div, &mod);
a = dbl2ival(div);
b = DBL2NUM(mod);
return rb_assoc_new(a, b);
};T; I"static VALUE;T;2To;
;F;;
;;;I"
Float#**;F;[[I"y;T0;[[@i/;T;:**;0;[ ;{ ;IC;"ARaises +float+ to the power of +other+.
2.0**3 #=> 8.0
;T;[o;
;I"
overload;F;0;;T;0; I"**(other);T;IC;" ;T;[o;!
;I"return;F;I" ;T;0;[I"
Float;T;"@R;#[ ;$I"@return [Float];T;%0;"@R;&F;'i ;(0;[[I"
other;T0;"@R;#[ ;$I"iRaises +float+ to the power of +other+.
2.0**3 #=> 8.0
@overload **(other)
@return [Float];T;%0;"@R;&F;+o;,;-T;.i&;/i,;0@;1I"bVALUE
rb_float_pow(VALUE x, VALUE y)
{
double dx, dy;
if (y == INT2FIX(2)) {
dx = RFLOAT_VALUE(x);
return DBL2NUM(dx * dx);
}
else if (RB_TYPE_P(y, T_FIXNUM)) {
dx = RFLOAT_VALUE(x);
dy = (double)FIX2LONG(y);
}
else if (RB_TYPE_P(y, T_BIGNUM)) {
dx = RFLOAT_VALUE(x);
dy = rb_big2dbl(y);
}
else if (RB_TYPE_P(y, T_FLOAT)) {
dx = RFLOAT_VALUE(x);
dy = RFLOAT_VALUE(y);
if (dx < 0 && dy != round(dy))
return rb_dbl_complex_new_polar_pi(pow(-dx, dy), dy);
}
else {
return rb_num_coerce_bin(x, y, idPow);
}
return DBL2NUM(pow(dx, dy));
};T; I"
VALUE;T;2To;
;F;;
;;;I"
Float#==;F;[ ;[ ;F;:==;;H;[ ;{ ;IC;" ;T;[ ;#[ ;$I" ;T;%0;"@q;0@;2To;
;F;;
;;;I"Float#===;F;[ ;[ ;F;:===;;H;[ ;{ ;IC;" ;T;[ ;#[ ;$@z;%0;"@{;0@;2To;
;F;;
;;;I"Float#<=>;F;[[I"y;T0;[[@i;T;:<=>;0;[ ;{ ;IC;":Returns -1, 0, or +1 depending on whether +float+ is
less than, equal to, or greater than +real+.
This is the basis for the tests in the Comparable module.
The result of NaN <=> NaN
is undefined,
so an implementation-dependent value is returned.
+nil+ is returned if the two values are incomparable.
;T;[o;
;I"
overload;F;0;;W;0; I"<=>(real);T;IC;" ;T;[o;!
;I"return;F;I" ;T;0;[ I"-1;TI"0;TI"+1;TI"nil;T;"@;#[ ;$I" @return [ -1, 0, +1, nil];T;%0;"@;&F;'i ;(0;[[I" real;T0;"@;#[ ;$I"nReturns -1, 0, or +1 depending on whether +float+ is
less than, equal to, or greater than +real+.
This is the basis for the tests in the Comparable module.
The result of NaN <=> NaN
is undefined,
so an implementation-dependent value is returned.
+nil+ is returned if the two values are incomparable.
@overload <=>(real)
@return [ -1, 0, +1, nil];T;%0;"@;&F;+o;,;-T;.i;/i;0@;1I"static VALUE
flo_cmp(VALUE x, VALUE y)
{
double a, b;
VALUE i;
a = RFLOAT_VALUE(x);
if (isnan(a)) return Qnil;
if (RB_TYPE_P(y, T_FIXNUM) || RB_TYPE_P(y, T_BIGNUM)) {
VALUE rel = rb_integer_float_cmp(y, x);
if (FIXNUM_P(rel))
return LONG2FIX(-FIX2LONG(rel));
return rel;
}
else if (RB_TYPE_P(y, T_FLOAT)) {
b = RFLOAT_VALUE(y);
}
else {
if (isinf(a) && (i = rb_check_funcall(y, rb_intern("infinite?"), 0, 0)) != Qundef) {
if (RTEST(i)) {
int j = rb_cmpint(i, x, y);
j = (a > 0.0) ? (j > 0 ? 0 : +1) : (j < 0 ? 0 : -1);
return INT2FIX(j);
}
if (a > 0.0) return INT2FIX(1);
return INT2FIX(-1);
}
return rb_num_coerce_cmp(x, y, id_cmp);
}
return rb_dbl_cmp(a, b);
};T; I"static VALUE;T;2To;
;F;;
;;;I"Float#>;F;[[I"y;T0;[[@i ;T;:>;0;[ ;{ ;IC;"Returns +true+ if +float+ is greater than +real+.
The result of NaN > NaN
is undefined,
so an implementation-dependent value is returned.
;T;[o;
;I"
overload;F;0;;X;0; I">(real);T;IC;" ;T;[o;!
;I"return;F;I" ;T;0;[I"Boolean;T;"@;#[ ;$I"@return [Boolean];T;%0;"@;&F;'i ;(0;[[I" real;T0;"@;#[ ;$I"Returns +true+ if +float+ is greater than +real+.
The result of NaN > NaN
is undefined,
so an implementation-dependent value is returned.
@overload >(real)
@return [Boolean];T;%0;"@;&F;+o;,;-T;.i;/i;0@;1I"QVALUE
rb_float_gt(VALUE x, VALUE y)
{
double a, b;
a = RFLOAT_VALUE(x);
if (RB_TYPE_P(y, T_FIXNUM) || RB_TYPE_P(y, T_BIGNUM)) {
VALUE rel = rb_integer_float_cmp(y, x);
if (FIXNUM_P(rel))
return -FIX2LONG(rel) > 0 ? Qtrue : Qfalse;
return Qfalse;
}
else if (RB_TYPE_P(y, T_FLOAT)) {
b = RFLOAT_VALUE(y);
#if MSC_VERSION_BEFORE(1300)
if (isnan(b)) return Qfalse;
#endif
}
else {
return rb_num_coerce_relop(x, y, '>');
}
#if MSC_VERSION_BEFORE(1300)
if (isnan(a)) return Qfalse;
#endif
return (a > b)?Qtrue:Qfalse;
};T; I"
VALUE;T;2To;
;F;;
;;;I"
Float#>=;F;[[I"y;T0;[[@i%;T;:>=;0;[ ;{ ;IC;"Returns +true+ if +float+ is greater than or equal to +real+.
The result of NaN >= NaN
is undefined,
so an implementation-dependent value is returned.
;T;[o;
;I"
overload;F;0;;Y;0; I"
>=(real);T;IC;" ;T;[o;!
;I"return;F;I" ;T;0;[I"Boolean;T;"@;#[ ;$I"@return [Boolean];T;%0;"@;&F;'i ;(0;[[I" real;T0;"@;#[ ;$I"Returns +true+ if +float+ is greater than or equal to +real+.
The result of NaN >= NaN
is undefined,
so an implementation-dependent value is returned.
@overload >=(real)
@return [Boolean];T;%0;"@;&F;+o;,;-T;.i;/i";0@;1I"Vstatic VALUE
flo_ge(VALUE x, VALUE y)
{
double a, b;
a = RFLOAT_VALUE(x);
if (RB_TYPE_P(y, T_FIXNUM) || RB_TYPE_P(y, T_BIGNUM)) {
VALUE rel = rb_integer_float_cmp(y, x);
if (FIXNUM_P(rel))
return -FIX2LONG(rel) >= 0 ? Qtrue : Qfalse;
return Qfalse;
}
else if (RB_TYPE_P(y, T_FLOAT)) {
b = RFLOAT_VALUE(y);
#if MSC_VERSION_BEFORE(1300)
if (isnan(b)) return Qfalse;
#endif
}
else {
return rb_num_coerce_relop(x, y, idGE);
}
#if MSC_VERSION_BEFORE(1300)
if (isnan(a)) return Qfalse;
#endif
return (a >= b)?Qtrue:Qfalse;
};T; I"static VALUE;T;2To;
;F;;
;;;I"Float#<;F;[[I"y;T0;[[@iJ;T;:<;0;[ ;{ ;IC;"Returns +true+ if +float+ is less than +real+.
The result of NaN < NaN
is undefined,
so an implementation-dependent value is returned.
;T;[o;
;I"
overload;F;0;;Z;0; I"<(real);T;IC;" ;T;[o;!
;I"return;F;I" ;T;0;[I"Boolean;T;"@;#[ ;$I"@return [Boolean];T;%0;"@;&F;'i ;(0;[[I" real;T0;"@;#[ ;$I"Returns +true+ if +float+ is less than +real+.
The result of NaN < NaN
is undefined,
so an implementation-dependent value is returned.
@overload <(real)
@return [Boolean];T;%0;"@;&F;+o;,;-T;.i@;/iG;0@;1I"Sstatic VALUE
flo_lt(VALUE x, VALUE y)
{
double a, b;
a = RFLOAT_VALUE(x);
if (RB_TYPE_P(y, T_FIXNUM) || RB_TYPE_P(y, T_BIGNUM)) {
VALUE rel = rb_integer_float_cmp(y, x);
if (FIXNUM_P(rel))
return -FIX2LONG(rel) < 0 ? Qtrue : Qfalse;
return Qfalse;
}
else if (RB_TYPE_P(y, T_FLOAT)) {
b = RFLOAT_VALUE(y);
#if MSC_VERSION_BEFORE(1300)
if (isnan(b)) return Qfalse;
#endif
}
else {
return rb_num_coerce_relop(x, y, '<');
}
#if MSC_VERSION_BEFORE(1300)
if (isnan(a)) return Qfalse;
#endif
return (a < b)?Qtrue:Qfalse;
};T; I"static VALUE;T;2To;
;F;;
;;;I"
Float#<=;F;[[I"y;T0;[[@io;T;:<=;0;[ ;{ ;IC;"Returns +true+ if +float+ is less than or equal to +real+.
The result of NaN <= NaN
is undefined,
so an implementation-dependent value is returned.
;T;[o;
;I"
overload;F;0;;[;0; I"
<=(real);T;IC;" ;T;[o;!
;I"return;F;I" ;T;0;[I"Boolean;T;"@;#[ ;$I"@return [Boolean];T;%0;"@;&F;'i ;(0;[[I" real;T0;"@;#[ ;$I"Returns +true+ if +float+ is less than or equal to +real+.
The result of NaN <= NaN
is undefined,
so an implementation-dependent value is returned.
@overload <=(real)
@return [Boolean];T;%0;"@;&F;+o;,;-T;.ie;/il;0@;1I"Vstatic VALUE
flo_le(VALUE x, VALUE y)
{
double a, b;
a = RFLOAT_VALUE(x);
if (RB_TYPE_P(y, T_FIXNUM) || RB_TYPE_P(y, T_BIGNUM)) {
VALUE rel = rb_integer_float_cmp(y, x);
if (FIXNUM_P(rel))
return -FIX2LONG(rel) <= 0 ? Qtrue : Qfalse;
return Qfalse;
}
else if (RB_TYPE_P(y, T_FLOAT)) {
b = RFLOAT_VALUE(y);
#if MSC_VERSION_BEFORE(1300)
if (isnan(b)) return Qfalse;
#endif
}
else {
return rb_num_coerce_relop(x, y, idLE);
}
#if MSC_VERSION_BEFORE(1300)
if (isnan(a)) return Qfalse;
#endif
return (a <= b)?Qtrue:Qfalse;
};T; I"static VALUE;T;2To;
;F;;
;;;I"Float#eql?;F;[ ;[ ;F;: eql?;;H;[ ;{ ;IC;" ;T;[o;!
;I"return;F;@z;0;[I"Boolean;T;"@";#[ ;$@z;%0;"@";'i ;0@;2To;
;F;;
;;;I"Float#hash;F;[ ;[[@i;T;: hash;0;[ ;{ ;IC;"?Returns a hash code for this float.
See also Object#hash.
;T;[o;
;I"
overload;F;0;;];0; I" hash;T;IC;" ;T;[o;!
;I"return;F;I" ;T;0;[I"Integer;T;"@/;#[ ;$I"@return [Integer];T;%0;"@/;&F;'i ;(0;[ ;"@/;#[ ;$I"dReturns a hash code for this float.
See also Object#hash.
@overload hash
@return [Integer];T;%0;"@/;&F;+o;,;-T;.i;/i;0@;1I"Tstatic VALUE
flo_hash(VALUE num)
{
return rb_dbl_hash(RFLOAT_VALUE(num));
};T; I"static VALUE;T;2To;
;F;;
;;;I"Float#to_f;F;[ ;[[@i;T;: to_f;0;[ ;{ ;IC;"6Since +float+ is already a Float, returns +self+.
;T;[o;
;I"
overload;F;0;;^;0; I" to_f;T;IC;" ;T;[o;!
;I"return;F;I" ;T;0;[I" self;T;"@J;#[ ;$I"@return [self];T;%0;"@J;&F;'i ;(0;[ ;"@J;#[ ;$I"XSince +float+ is already a Float, returns +self+.
@overload to_f
@return [self];T;%0;"@J;&F;+o;,;-T;.i;/i;0@;1I"9static VALUE
flo_to_f(VALUE num)
{
return num;
};T; I"static VALUE;T;2To;
;F;;
;;;I"Float#abs;F;[ ;[[@i;T;:abs;0;[ ;{ ;IC;"Returns the absolute value of +float+.
(-34.56).abs #=> 34.56
-34.56.abs #=> 34.56
34.56.abs #=> 34.56
Float#magnitude is an alias for Float#abs.
;T;[o;
;I"
overload;F;0;;_;0; I"abs;T;IC;" ;T;[o;!
;I"return;F;I" ;T;0;[I"
Float;T;"@e;#[ ;$I"@return [Float];T;%0;"@e;&F;'i ;(0;[ ;"@eo;
;I"
overload;F;0;:magnitude;0; I"magnitude;T;IC;" ;T;[o;!
;I"return;F;I" ;T;0;[I"
Float;T;"@e;#[ ;$I"@return [Float];T;%0;"@e;&F;'i ;(0;[ ;"@e;#[ ;$I"Returns the absolute value of +float+.
(-34.56).abs #=> 34.56
-34.56.abs #=> 34.56
34.56.abs #=> 34.56
Float#magnitude is an alias for Float#abs.
@overload abs
@return [Float]
@overload magnitude
@return [Float];T;%0;"@e;&F;+o;,;-T;.i;/i;0@;1I"iVALUE
rb_float_abs(VALUE flt)
{
double val = fabs(RFLOAT_VALUE(flt));
return DBL2NUM(val);
};T; I"
VALUE;T;2To;
;F;;
;;;I"Float#magnitude;F;[ ;[[@i;T;;`;0;[ ;{ ;IC;"Returns the absolute value of +float+.
(-34.56).abs #=> 34.56
-34.56.abs #=> 34.56
34.56.abs #=> 34.56
Float#magnitude is an alias for Float#abs.
;T;[o;
;I"
overload;F;0;;_;0; I"abs;T;IC;" ;T;[o;!
;I"return;F;I" ;T;0;[I"
Float;T;"@;#[ ;$I"@return [Float];T;%0;"@;&F;'i ;(0;[ ;"@o;
;I"
overload;F;0;;`;0; I"magnitude;T;IC;" ;T;[o;!
;I"return;F;I" ;T;0;[I"
Float;T;"@;#[ ;$I"@return [Float];T;%0;"@;&F;'i ;(0;[ ;"@;#[ ;$@;%0;"@;&F;+o;,;-T;.i;/i;0@;1I"iVALUE
rb_float_abs(VALUE flt)
{
double val = fabs(RFLOAT_VALUE(flt));
return DBL2NUM(val);
};T; I"
VALUE;T;2To;
;F;;
;;;I"Float#zero?;F;[ ;[[@i;T;:
zero?;0;[ ;{ ;IC;"&Returns +true+ if +float+ is 0.0.;T;[o;
;I"
overload;F;0;;a;0; I"
zero?;T;IC;" ;T;[o;!
;I"return;F;I" ;T;0;[I"Boolean;T;"@;#[ ;$I"@return [Boolean];T;%0;"@;&F;'i ;(0;[ ;"@;#[ ;$I"LReturns +true+ if +float+ is 0.0.
@overload zero?
@return [Boolean];T;%0;"@;&F;+o;,;-T;.i;/i;'i ;0@;1I"Xstatic VALUE
flo_zero_p(VALUE num)
{
return flo_iszero(num) ? Qtrue : Qfalse;
};T; I"static VALUE;T;2To;
;F;;
;;;I"Float#to_i;F;[ ;[[@i] ;T;: to_i;0;[ ;{ ;IC;"Returns the +float+ truncated to an Integer.
1.2.to_i #=> 1
(-1.2).to_i #=> -1
Note that the limited precision of floating point arithmetic
might lead to surprising results:
(0.3 / 0.1).to_i #=> 2 (!)
#to_int is an alias for #to_i.
;T;[o;
;I"
overload;F;0;;b;0; I" to_i;T;IC;" ;T;[o;!
;I"return;F;I" ;T;0;[I"Integer;T;"@;#[ ;$I"@return [Integer];T;%0;"@;&F;'i ;(0;[ ;"@o;
;I"
overload;F;0;:to_int;0; I"to_int;T;IC;" ;T;[o;!
;I"return;F;I" ;T;0;[I"Integer;T;"@;#[ ;$I"@return [Integer];T;%0;"@;&F;'i ;(0;[ ;"@;#[ ;$I"EReturns the +float+ truncated to an Integer.
1.2.to_i #=> 1
(-1.2).to_i #=> -1
Note that the limited precision of floating point arithmetic
might lead to surprising results:
(0.3 / 0.1).to_i #=> 2 (!)
#to_int is an alias for #to_i.
@overload to_i
@return [Integer]
@overload to_int
@return [Integer];T;%0;"@;&F;+o;,;-T;.iK ;/i[ ;0@;1I"static VALUE
flo_to_i(VALUE num)
{
double f = RFLOAT_VALUE(num);
if (f > 0.0) f = floor(f);
if (f < 0.0) f = ceil(f);
return dbl2ival(f);
};T; I"static VALUE;T;2To;
;F;;
;;;I"Float#to_int;F;[ ;[[@i] ;T;;c;0;[ ;{ ;IC;"Returns the +float+ truncated to an Integer.
1.2.to_i #=> 1
(-1.2).to_i #=> -1
Note that the limited precision of floating point arithmetic
might lead to surprising results:
(0.3 / 0.1).to_i #=> 2 (!)
#to_int is an alias for #to_i.
;T;[o;
;I"
overload;F;0;;b;0; I" to_i;T;IC;" ;T;[o;!
;I"return;F;I" ;T;0;[I"Integer;T;"@;#[ ;$I"@return [Integer];T;%0;"@;&F;'i ;(0;[ ;"@o;
;I"
overload;F;0;;c;0; I"to_int;T;IC;" ;T;[o;!
;I"return;F;I" ;T;0;[I"Integer;T;"@;#[ ;$I"@return [Integer];T;%0;"@;&F;'i ;(0;[ ;"@;#[ ;$@;%0;"@;&F;+o;,;-T;.iK ;/i[ ;0@;1I"static VALUE
flo_to_i(VALUE num)
{
double f = RFLOAT_VALUE(num);
if (f > 0.0) f = floor(f);
if (f < 0.0) f = ceil(f);
return dbl2ival(f);
};T; I"static VALUE;T;2To;
;F;;
;;;I"Float#floor;F;[[@0;[[@i;T;:
floor;0;[ ;{ ;IC;"Returns the largest number less than or equal to +float+ with
a precision of +ndigits+ decimal digits (default: 0).
When the precision is negative, the returned value is an integer
with at least ndigits.abs
trailing zeros.
Returns a floating point number when +ndigits+ is positive,
otherwise returns an integer.
1.2.floor #=> 1
2.0.floor #=> 2
(-1.2).floor #=> -2
(-2.0).floor #=> -2
1.234567.floor(2) #=> 1.23
1.234567.floor(3) #=> 1.234
1.234567.floor(4) #=> 1.2345
1.234567.floor(5) #=> 1.23456
34567.89.floor(-5) #=> 0
34567.89.floor(-4) #=> 30000
34567.89.floor(-3) #=> 34000
34567.89.floor(-2) #=> 34500
34567.89.floor(-1) #=> 34560
34567.89.floor(0) #=> 34567
34567.89.floor(1) #=> 34567.8
34567.89.floor(2) #=> 34567.89
34567.89.floor(3) #=> 34567.89
Note that the limited precision of floating point arithmetic
might lead to surprising results:
(0.3 / 0.1).floor #=> 2 (!)
;T;[o;
;I"
overload;F;0;;d;0; I"floor([ndigits]);T;IC;" ;T;[o;!
;I"return;F;I" ;T;0;[I"Integer;TI"
Float;T;"@;#[ ;$I"@return [Integer, Float];T;%0;"@;&F;'i ;(0;[[I"[ndigits];T0;"@;#[ ;$I"Returns the largest number less than or equal to +float+ with
a precision of +ndigits+ decimal digits (default: 0).
When the precision is negative, the returned value is an integer
with at least ndigits.abs
trailing zeros.
Returns a floating point number when +ndigits+ is positive,
otherwise returns an integer.
1.2.floor #=> 1
2.0.floor #=> 2
(-1.2).floor #=> -2
(-2.0).floor #=> -2
1.234567.floor(2) #=> 1.23
1.234567.floor(3) #=> 1.234
1.234567.floor(4) #=> 1.2345
1.234567.floor(5) #=> 1.23456
34567.89.floor(-5) #=> 0
34567.89.floor(-4) #=> 30000
34567.89.floor(-3) #=> 34000
34567.89.floor(-2) #=> 34500
34567.89.floor(-1) #=> 34560
34567.89.floor(0) #=> 34567
34567.89.floor(1) #=> 34567.8
34567.89.floor(2) #=> 34567.89
34567.89.floor(3) #=> 34567.89
Note that the limited precision of floating point arithmetic
might lead to surprising results:
(0.3 / 0.1).floor #=> 2 (!)
@overload floor([ndigits])
@return [Integer, Float];T;%0;"@;&F;+o;,;-T;.i;/i;0@;1I"static VALUE
flo_floor(int argc, VALUE *argv, VALUE num)
{
int ndigits = 0;
if (rb_check_arity(argc, 0, 1)) {
ndigits = NUM2INT(argv[0]);
}
return rb_float_floor(num, ndigits);
};T; I"static VALUE;T;2To;
;F;;
;;;I"Float#ceil;F;[[@0;[[@i;T;: ceil;0;[ ;{ ;IC;"Returns the smallest number greater than or equal to +float+ with
a precision of +ndigits+ decimal digits (default: 0).
When the precision is negative, the returned value is an integer
with at least ndigits.abs
trailing zeros.
Returns a floating point number when +ndigits+ is positive,
otherwise returns an integer.
1.2.ceil #=> 2
2.0.ceil #=> 2
(-1.2).ceil #=> -1
(-2.0).ceil #=> -2
1.234567.ceil(2) #=> 1.24
1.234567.ceil(3) #=> 1.235
1.234567.ceil(4) #=> 1.2346
1.234567.ceil(5) #=> 1.23457
34567.89.ceil(-5) #=> 100000
34567.89.ceil(-4) #=> 40000
34567.89.ceil(-3) #=> 35000
34567.89.ceil(-2) #=> 34600
34567.89.ceil(-1) #=> 34570
34567.89.ceil(0) #=> 34568
34567.89.ceil(1) #=> 34567.9
34567.89.ceil(2) #=> 34567.89
34567.89.ceil(3) #=> 34567.89
Note that the limited precision of floating point arithmetic
might lead to surprising results:
(2.1 / 0.7).ceil #=> 4 (!)
;T;[o;
;I"
overload;F;0;;e;0; I"ceil([ndigits]);T;IC;" ;T;[o;!
;I"return;F;I" ;T;0;[I"Integer;TI"
Float;T;"@=;#[ ;$I"@return [Integer, Float];T;%0;"@=;&F;'i ;(0;[[I"[ndigits];T0;"@=;#[ ;$I"Returns the smallest number greater than or equal to +float+ with
a precision of +ndigits+ decimal digits (default: 0).
When the precision is negative, the returned value is an integer
with at least ndigits.abs
trailing zeros.
Returns a floating point number when +ndigits+ is positive,
otherwise returns an integer.
1.2.ceil #=> 2
2.0.ceil #=> 2
(-1.2).ceil #=> -1
(-2.0).ceil #=> -2
1.234567.ceil(2) #=> 1.24
1.234567.ceil(3) #=> 1.235
1.234567.ceil(4) #=> 1.2346
1.234567.ceil(5) #=> 1.23457
34567.89.ceil(-5) #=> 100000
34567.89.ceil(-4) #=> 40000
34567.89.ceil(-3) #=> 35000
34567.89.ceil(-2) #=> 34600
34567.89.ceil(-1) #=> 34570
34567.89.ceil(0) #=> 34568
34567.89.ceil(1) #=> 34567.9
34567.89.ceil(2) #=> 34567.89
34567.89.ceil(3) #=> 34567.89
Note that the limited precision of floating point arithmetic
might lead to surprising results:
(2.1 / 0.7).ceil #=> 4 (!)
@overload ceil([ndigits])
@return [Integer, Float];T;%0;"@=;&F;+o;,;-T;.i;/i;0@;1I"static VALUE
flo_ceil(int argc, VALUE *argv, VALUE num)
{
int ndigits = 0;
if (rb_check_arity(argc, 0, 1)) {
ndigits = NUM2INT(argv[0]);
}
return rb_float_ceil(num, ndigits);
};T; I"static VALUE;T;2To;
;F;;
;;;I"Float#round;F;[[@0;[[@i ;T;:
round;0;[ ;{ ;IC;"Returns +float+ rounded to the nearest value with
a precision of +ndigits+ decimal digits (default: 0).
When the precision is negative, the returned value is an integer
with at least ndigits.abs
trailing zeros.
Returns a floating point number when +ndigits+ is positive,
otherwise returns an integer.
1.4.round #=> 1
1.5.round #=> 2
1.6.round #=> 2
(-1.5).round #=> -2
1.234567.round(2) #=> 1.23
1.234567.round(3) #=> 1.235
1.234567.round(4) #=> 1.2346
1.234567.round(5) #=> 1.23457
34567.89.round(-5) #=> 0
34567.89.round(-4) #=> 30000
34567.89.round(-3) #=> 35000
34567.89.round(-2) #=> 34600
34567.89.round(-1) #=> 34570
34567.89.round(0) #=> 34568
34567.89.round(1) #=> 34567.9
34567.89.round(2) #=> 34567.89
34567.89.round(3) #=> 34567.89
If the optional +half+ keyword argument is given,
numbers that are half-way between two possible rounded values
will be rounded according to the specified tie-breaking +mode+:
* :up
or +nil+: round half away from zero (default)
* :down
: round half toward zero
* :even
: round half toward the nearest even number
2.5.round(half: :up) #=> 3
2.5.round(half: :down) #=> 2
2.5.round(half: :even) #=> 2
3.5.round(half: :up) #=> 4
3.5.round(half: :down) #=> 3
3.5.round(half: :even) #=> 4
(-2.5).round(half: :up) #=> -3
(-2.5).round(half: :down) #=> -2
(-2.5).round(half: :even) #=> -2
;T;[o;
;I"
overload;F;0;;f;0; I"$round([ndigits] [, half: mode]);T;IC;" ;T;[o;!
;I"return;F;I" ;T;0;[I"Integer;TI"
Float;T;"@\;#[ ;$I"@return [Integer, Float];T;%0;"@\;&F;'i ;(0;[[I"[ndigits][, half:;TI"
mode];T;"@\;#[ ;$I"8Returns +float+ rounded to the nearest value with
a precision of +ndigits+ decimal digits (default: 0).
When the precision is negative, the returned value is an integer
with at least ndigits.abs
trailing zeros.
Returns a floating point number when +ndigits+ is positive,
otherwise returns an integer.
1.4.round #=> 1
1.5.round #=> 2
1.6.round #=> 2
(-1.5).round #=> -2
1.234567.round(2) #=> 1.23
1.234567.round(3) #=> 1.235
1.234567.round(4) #=> 1.2346
1.234567.round(5) #=> 1.23457
34567.89.round(-5) #=> 0
34567.89.round(-4) #=> 30000
34567.89.round(-3) #=> 35000
34567.89.round(-2) #=> 34600
34567.89.round(-1) #=> 34570
34567.89.round(0) #=> 34568
34567.89.round(1) #=> 34567.9
34567.89.round(2) #=> 34567.89
34567.89.round(3) #=> 34567.89
If the optional +half+ keyword argument is given,
numbers that are half-way between two possible rounded values
will be rounded according to the specified tie-breaking +mode+:
* :up
or +nil+: round half away from zero (default)
* :down
: round half toward zero
* :even
: round half toward the nearest even number
2.5.round(half: :up) #=> 3
2.5.round(half: :down) #=> 2
2.5.round(half: :even) #=> 2
3.5.round(half: :up) #=> 4
3.5.round(half: :down) #=> 3
3.5.round(half: :even) #=> 4
(-2.5).round(half: :up) #=> -3
(-2.5).round(half: :down) #=> -2
(-2.5).round(half: :even) #=> -2
@overload round([ndigits] [, half: mode])
@return [Integer, Float];T;%0;"@\;&F;+o;,;-T;.i;/i ;0@;1I"rstatic VALUE
flo_round(int argc, VALUE *argv, VALUE num)
{
double number, f, x;
VALUE nd, opt;
int ndigits = 0;
enum ruby_num_rounding_mode mode;
if (rb_scan_args(argc, argv, "01:", &nd, &opt)) {
ndigits = NUM2INT(nd);
}
mode = rb_num_get_rounding_option(opt);
number = RFLOAT_VALUE(num);
if (number == 0.0) {
return ndigits > 0 ? DBL2NUM(number) : INT2FIX(0);
}
if (ndigits < 0) {
return rb_int_round(flo_to_i(num), ndigits, mode);
}
if (ndigits == 0) {
x = ROUND_CALL(mode, round, (number, 1.0));
return dbl2ival(x);
}
if (isfinite(number)) {
int binexp;
frexp(number, &binexp);
if (float_round_overflow(ndigits, binexp)) return num;
if (float_round_underflow(ndigits, binexp)) return DBL2NUM(0);
f = pow(10, ndigits);
x = ROUND_CALL(mode, round, (number, f));
return DBL2NUM(x / f);
}
return num;
};T; I"static VALUE;T;2To;
;F;;
;;;I"Float#truncate;F;[[@0;[[@i ;T;:
truncate;0;[ ;{ ;IC;"AReturns +float+ truncated (toward zero) to
a precision of +ndigits+ decimal digits (default: 0).
When the precision is negative, the returned value is an integer
with at least ndigits.abs
trailing zeros.
Returns a floating point number when +ndigits+ is positive,
otherwise returns an integer.
2.8.truncate #=> 2
(-2.8).truncate #=> -2
1.234567.truncate(2) #=> 1.23
34567.89.truncate(-2) #=> 34500
Note that the limited precision of floating point arithmetic
might lead to surprising results:
(0.3 / 0.1).truncate #=> 2 (!)
;T;[o;
;I"
overload;F;0;;g;0; I"truncate([ndigits]);T;IC;" ;T;[o;!
;I"return;F;I" ;T;0;[I"Integer;TI"
Float;T;"@|;#[ ;$I"@return [Integer, Float];T;%0;"@|;&F;'i ;(0;[[I"[ndigits];T0;"@|;#[ ;$I"|Returns +float+ truncated (toward zero) to
a precision of +ndigits+ decimal digits (default: 0).
When the precision is negative, the returned value is an integer
with at least ndigits.abs
trailing zeros.
Returns a floating point number when +ndigits+ is positive,
otherwise returns an integer.
2.8.truncate #=> 2
(-2.8).truncate #=> -2
1.234567.truncate(2) #=> 1.23
34567.89.truncate(-2) #=> 34500
Note that the limited precision of floating point arithmetic
might lead to surprising results:
(0.3 / 0.1).truncate #=> 2 (!)
@overload truncate([ndigits])
@return [Integer, Float];T;%0;"@|;&F;+o;,;-T;.ih ;/i} ;0@;1I"static VALUE
flo_truncate(int argc, VALUE *argv, VALUE num)
{
if (signbit(RFLOAT_VALUE(num)))
return flo_ceil(argc, argv, num);
else
return flo_floor(argc, argv, num);
};T; I"static VALUE;T;2To;
;F;;
;;;I"Float#nan?;F;[ ;[[@i;T;: nan?;0;[ ;{ ;IC;"Returns +true+ if +float+ is an invalid IEEE floating point number.
a = -1.0 #=> -1.0
a.nan? #=> false
a = 0.0/0.0 #=> NaN
a.nan? #=> true;T;[o;
;I"
overload;F;0;;h;0; I" nan?;T;IC;" ;T;[o;!
;I"return;F;I" ;T;0;[I"Boolean;T;"@;#[ ;$I"@return [Boolean];T;%0;"@;&F;'i ;(0;[ ;"@;#[ ;$I"Returns +true+ if +float+ is an invalid IEEE floating point number.
a = -1.0 #=> -1.0
a.nan? #=> false
a = 0.0/0.0 #=> NaN
a.nan? #=> true
@overload nan?
@return [Boolean];T;%0;"@;&F;+o;,;-T;.i;/i;'i ;0@;1I"~static VALUE
flo_is_nan_p(VALUE num)
{
double value = RFLOAT_VALUE(num);
return isnan(value) ? Qtrue : Qfalse;
};T; I"static VALUE;T;2To;
;F;;
;;;I"Float#infinite?;F;[ ;[[@ii;T;:infinite?;0;[ ;{ ;IC;" ;T;[o;!
;I"return;F;@z;0;[@-;"@;#[ ;$@z;%0;"@;'i ;0@;1I"+VALUE rb_flo_is_infinite_p(VALUE num);;T; I"+VALUE rb_flo_is_infinite_p(VALUE num);;T;2To;
;F;;
;;;I"Float#finite?;F;[ ;[[@iY;T;:finite?;0;[ ;{ ;IC;" ;T;[o;!
;I"return;F;@z;0;[@-;"@;#[ ;$@z;%0;"@;'i ;0@;1I")VALUE rb_flo_is_finite_p(VALUE num);;T; I")VALUE rb_flo_is_finite_p(VALUE num);;T;2To;
;F;;
;;;I"Float#next_float;F;[ ;[[@iX;T;:next_float;0;[ ;{ ;IC;"EReturns the next representable floating point number.
Float::MAX.next_float and Float::INFINITY.next_float is Float::INFINITY.
Float::NAN.next_float is Float::NAN.
For example:
0.01.next_float #=> 0.010000000000000002
1.0.next_float #=> 1.0000000000000002
100.0.next_float #=> 100.00000000000001
0.01.next_float - 0.01 #=> 1.734723475976807e-18
1.0.next_float - 1.0 #=> 2.220446049250313e-16
100.0.next_float - 100.0 #=> 1.4210854715202004e-14
f = 0.01; 20.times { printf "%-20a %s\n", f, f.to_s; f = f.next_float }
#=> 0x1.47ae147ae147bp-7 0.01
# 0x1.47ae147ae147cp-7 0.010000000000000002
# 0x1.47ae147ae147dp-7 0.010000000000000004
# 0x1.47ae147ae147ep-7 0.010000000000000005
# 0x1.47ae147ae147fp-7 0.010000000000000007
# 0x1.47ae147ae148p-7 0.010000000000000009
# 0x1.47ae147ae1481p-7 0.01000000000000001
# 0x1.47ae147ae1482p-7 0.010000000000000012
# 0x1.47ae147ae1483p-7 0.010000000000000014
# 0x1.47ae147ae1484p-7 0.010000000000000016
# 0x1.47ae147ae1485p-7 0.010000000000000018
# 0x1.47ae147ae1486p-7 0.01000000000000002
# 0x1.47ae147ae1487p-7 0.010000000000000021
# 0x1.47ae147ae1488p-7 0.010000000000000023
# 0x1.47ae147ae1489p-7 0.010000000000000024
# 0x1.47ae147ae148ap-7 0.010000000000000026
# 0x1.47ae147ae148bp-7 0.010000000000000028
# 0x1.47ae147ae148cp-7 0.01000000000000003
# 0x1.47ae147ae148dp-7 0.010000000000000031
# 0x1.47ae147ae148ep-7 0.010000000000000033
f = 0.0
100.times { f += 0.1 }
f #=> 9.99999999999998 # should be 10.0 in the ideal world.
10-f #=> 1.9539925233402755e-14 # the floating point error.
10.0.next_float-10 #=> 1.7763568394002505e-15 # 1 ulp (unit in the last place).
(10-f)/(10.0.next_float-10) #=> 11.0 # the error is 11 ulp.
(10-f)/(10*Float::EPSILON) #=> 8.8 # approximation of the above.
"%a" % 10 #=> "0x1.4p+3"
"%a" % f #=> "0x1.3fffffffffff5p+3" # the last hex digit is 5. 16 - 5 = 11 ulp.
;T;[o;
;I"
overload;F;0;;k;0; I"next_float;T;IC;" ;T;[o;!
;I"return;F;I" ;T;0;[I"
Float;T;"@;#[ ;$I"@return [Float];T;%0;"@;&F;'i ;(0;[ ;"@;#[ ;$I"nReturns the next representable floating point number.
Float::MAX.next_float and Float::INFINITY.next_float is Float::INFINITY.
Float::NAN.next_float is Float::NAN.
For example:
0.01.next_float #=> 0.010000000000000002
1.0.next_float #=> 1.0000000000000002
100.0.next_float #=> 100.00000000000001
0.01.next_float - 0.01 #=> 1.734723475976807e-18
1.0.next_float - 1.0 #=> 2.220446049250313e-16
100.0.next_float - 100.0 #=> 1.4210854715202004e-14
f = 0.01; 20.times { printf "%-20a %s\n", f, f.to_s; f = f.next_float }
#=> 0x1.47ae147ae147bp-7 0.01
# 0x1.47ae147ae147cp-7 0.010000000000000002
# 0x1.47ae147ae147dp-7 0.010000000000000004
# 0x1.47ae147ae147ep-7 0.010000000000000005
# 0x1.47ae147ae147fp-7 0.010000000000000007
# 0x1.47ae147ae148p-7 0.010000000000000009
# 0x1.47ae147ae1481p-7 0.01000000000000001
# 0x1.47ae147ae1482p-7 0.010000000000000012
# 0x1.47ae147ae1483p-7 0.010000000000000014
# 0x1.47ae147ae1484p-7 0.010000000000000016
# 0x1.47ae147ae1485p-7 0.010000000000000018
# 0x1.47ae147ae1486p-7 0.01000000000000002
# 0x1.47ae147ae1487p-7 0.010000000000000021
# 0x1.47ae147ae1488p-7 0.010000000000000023
# 0x1.47ae147ae1489p-7 0.010000000000000024
# 0x1.47ae147ae148ap-7 0.010000000000000026
# 0x1.47ae147ae148bp-7 0.010000000000000028
# 0x1.47ae147ae148cp-7 0.01000000000000003
# 0x1.47ae147ae148dp-7 0.010000000000000031
# 0x1.47ae147ae148ep-7 0.010000000000000033
f = 0.0
100.times { f += 0.1 }
f #=> 9.99999999999998 # should be 10.0 in the ideal world.
10-f #=> 1.9539925233402755e-14 # the floating point error.
10.0.next_float-10 #=> 1.7763568394002505e-15 # 1 ulp (unit in the last place).
(10-f)/(10.0.next_float-10) #=> 11.0 # the error is 11 ulp.
(10-f)/(10*Float::EPSILON) #=> 8.8 # approximation of the above.
"%a" % 10 #=> "0x1.4p+3"
"%a" % f #=> "0x1.3fffffffffff5p+3" # the last hex digit is 5. 16 - 5 = 11 ulp.
@overload next_float
@return [Float];T;%0;"@;&F;+o;,;-T;.i$;/iV;0@;1I"Vstatic VALUE
flo_next_float(VALUE vx)
{
return flo_nextafter(vx, HUGE_VAL);
};T; I"static VALUE;T;2To;
;F;;
;;;I"Float#prev_float;F;[ ;[[@i;T;:prev_float;0;[ ;{ ;IC;"Returns the previous representable floating point number.
(-Float::MAX).prev_float and (-Float::INFINITY).prev_float is -Float::INFINITY.
Float::NAN.prev_float is Float::NAN.
For example:
0.01.prev_float #=> 0.009999999999999998
1.0.prev_float #=> 0.9999999999999999
100.0.prev_float #=> 99.99999999999999
0.01 - 0.01.prev_float #=> 1.734723475976807e-18
1.0 - 1.0.prev_float #=> 1.1102230246251565e-16
100.0 - 100.0.prev_float #=> 1.4210854715202004e-14
f = 0.01; 20.times { printf "%-20a %s\n", f, f.to_s; f = f.prev_float }
#=> 0x1.47ae147ae147bp-7 0.01
# 0x1.47ae147ae147ap-7 0.009999999999999998
# 0x1.47ae147ae1479p-7 0.009999999999999997
# 0x1.47ae147ae1478p-7 0.009999999999999995
# 0x1.47ae147ae1477p-7 0.009999999999999993
# 0x1.47ae147ae1476p-7 0.009999999999999992
# 0x1.47ae147ae1475p-7 0.00999999999999999
# 0x1.47ae147ae1474p-7 0.009999999999999988
# 0x1.47ae147ae1473p-7 0.009999999999999986
# 0x1.47ae147ae1472p-7 0.009999999999999985
# 0x1.47ae147ae1471p-7 0.009999999999999983
# 0x1.47ae147ae147p-7 0.009999999999999981
# 0x1.47ae147ae146fp-7 0.00999999999999998
# 0x1.47ae147ae146ep-7 0.009999999999999978
# 0x1.47ae147ae146dp-7 0.009999999999999976
# 0x1.47ae147ae146cp-7 0.009999999999999974
# 0x1.47ae147ae146bp-7 0.009999999999999972
# 0x1.47ae147ae146ap-7 0.00999999999999997
# 0x1.47ae147ae1469p-7 0.009999999999999969
# 0x1.47ae147ae1468p-7 0.009999999999999967
;T;[o;
;I"
overload;F;0;;l;0; I"prev_float;T;IC;" ;T;[o;!
;I"return;F;I" ;T;0;[I"
Float;T;"@;#[ ;$I"@return [Float];T;%0;"@;&F;'i ;(0;[ ;"@;#[ ;$I"
Returns the previous representable floating point number.
(-Float::MAX).prev_float and (-Float::INFINITY).prev_float is -Float::INFINITY.
Float::NAN.prev_float is Float::NAN.
For example:
0.01.prev_float #=> 0.009999999999999998
1.0.prev_float #=> 0.9999999999999999
100.0.prev_float #=> 99.99999999999999
0.01 - 0.01.prev_float #=> 1.734723475976807e-18
1.0 - 1.0.prev_float #=> 1.1102230246251565e-16
100.0 - 100.0.prev_float #=> 1.4210854715202004e-14
f = 0.01; 20.times { printf "%-20a %s\n", f, f.to_s; f = f.prev_float }
#=> 0x1.47ae147ae147bp-7 0.01
# 0x1.47ae147ae147ap-7 0.009999999999999998
# 0x1.47ae147ae1479p-7 0.009999999999999997
# 0x1.47ae147ae1478p-7 0.009999999999999995
# 0x1.47ae147ae1477p-7 0.009999999999999993
# 0x1.47ae147ae1476p-7 0.009999999999999992
# 0x1.47ae147ae1475p-7 0.00999999999999999
# 0x1.47ae147ae1474p-7 0.009999999999999988
# 0x1.47ae147ae1473p-7 0.009999999999999986
# 0x1.47ae147ae1472p-7 0.009999999999999985
# 0x1.47ae147ae1471p-7 0.009999999999999983
# 0x1.47ae147ae147p-7 0.009999999999999981
# 0x1.47ae147ae146fp-7 0.00999999999999998
# 0x1.47ae147ae146ep-7 0.009999999999999978
# 0x1.47ae147ae146dp-7 0.009999999999999976
# 0x1.47ae147ae146cp-7 0.009999999999999974
# 0x1.47ae147ae146bp-7 0.009999999999999972
# 0x1.47ae147ae146ap-7 0.00999999999999997
# 0x1.47ae147ae1469p-7 0.009999999999999969
# 0x1.47ae147ae1468p-7 0.009999999999999967
@overload prev_float
@return [Float];T;%0;"@;&F;+o;,;-T;.i^;/i;0@;1I"Wstatic VALUE
flo_prev_float(VALUE vx)
{
return flo_nextafter(vx, -HUGE_VAL);
};T; I"static VALUE;T;2To;
;F;;
;;;I"Float#positive?;F;[ ;[[@i ;T;:positive?;0;[ ;{ ;IC;"1Returns +true+ if +float+ is greater than 0.;T;[o;
;I"
overload;F;0;;m;0; I"positive?;T;IC;" ;T;[o;!
;I"return;F;I" ;T;0;[I"Boolean;T;"@
;#[ ;$I"@return [Boolean];T;%0;"@
;&F;'i ;(0;[ ;"@
;#[ ;$I"[Returns +true+ if +float+ is greater than 0.
@overload positive?
@return [Boolean];T;%0;"@
;&F;+o;,;-T;.i ;/i ;'i ;0@;1I"vstatic VALUE
flo_positive_p(VALUE num)
{
double f = RFLOAT_VALUE(num);
return f > 0.0 ? Qtrue : Qfalse;
};T; I"static VALUE;T;2To;
;F;;
;;;I"Float#negative?;F;[ ;[[@i ;T;:negative?;0;[ ;{ ;IC;".Returns +true+ if +float+ is less than 0.;T;[o;
;I"
overload;F;0;;n;0; I"negative?;T;IC;" ;T;[o;!
;I"return;F;I" ;T;0;[I"Boolean;T;"@%;#[ ;$I"@return [Boolean];T;%0;"@%;&F;'i ;(0;[ ;"@%;#[ ;$I"XReturns +true+ if +float+ is less than 0.
@overload negative?
@return [Boolean];T;%0;"@%;&F;+o;,;-T;.i ;/i ;'i ;0@;1I"vstatic VALUE
flo_negative_p(VALUE num)
{
double f = RFLOAT_VALUE(num);
return f < 0.0 ? Qtrue : Qfalse;
};T; I"static VALUE;T;2T:@owner@:@class_mixinsIC;[ ;o@:@instance_mixinsIC;[ ;o@:@attributesIC:SymbolHash{:
classIC;s{ :@symbolize_valueT;
IC;s{ ;uT;uT:
@aliases{@;F:@groups[ ;[[@i[@i;T;:
Float;;H;;;[ ;{ ;IC;" ******************************************************************
Float objects represent inexact real numbers using the native
architecture's double-precision floating point representation.
Floating point has a different arithmetic and is an inexact number.
So you should know its esoteric system. See following:
- https://docs.oracle.com/cd/E19957-01/806-3568/ncg_goldberg.html
- https://github.com/rdp/ruby_tutorials_core/wiki/Ruby-Talk-FAQ#floats_imprecise
- https://en.wikipedia.org/wiki/Floating_point#Accuracy_problems;T;[ ;#[ ;$I"!******************************************************************
Float objects represent inexact real numbers using the native
architecture's double-precision floating point representation.
Floating point has a different arithmetic and is an inexact number.
So you should know its esoteric system. See following:
- https://docs.oracle.com/cd/E19957-01/806-3568/ncg_goldberg.html
- https://github.com/rdp/ruby_tutorials_core/wiki/Ruby-Talk-FAQ#floats_imprecise
- https://en.wikipedia.org/wiki/Floating_point#Accuracy_problems
;T;%0;"@;&F;+o;,;-T;.i;/i;'i ;0@;I"
Float;F:@superclasso; ;IC;[4o;
;F;;
;;;I"#Numeric#singleton_method_added;F;[[I" name;T0;[[@i;T;:singleton_method_added;0;[ ;{ ;IC;":nodoc:
Trap attempts to add methods to Numeric objects. Always raises a TypeError.
Numerics should be values; singleton_methods should not be added to them.
;T;[ ;#[ ;$I":nodoc:
Trap attempts to add methods to Numeric objects. Always raises a TypeError.
Numerics should be values; singleton_methods should not be added to them.
;T;%0;"@T;&F;+o;,;-T;.i;/i;0@R;1I"static VALUE
num_sadded(VALUE x, VALUE name)
{
ID mid = rb_to_id(name);
/* ruby_frame = ruby_frame->prev; */ /* pop frame for "singleton_method_added" */
rb_remove_method_id(rb_singleton_class(x), mid);
rb_raise(rb_eTypeError,
"can't define singleton method \"%"PRIsVALUE"\" for %"PRIsVALUE,
rb_id2str(mid),
rb_obj_class(x));
UNREACHABLE_RETURN(Qnil);
};T; I"static VALUE;T;2To;
;F;;
;;;I"Numeric#coerce;F;[[I"y;T0;[[@i;T;;I;0;[ ;{ ;IC;"If +numeric+ is the same type as +num+, returns an array
[numeric, num]
. Otherwise, returns an array with both
+numeric+ and +num+ represented as Float objects.
This coercion mechanism is used by Ruby to handle mixed-type numeric
operations: it is intended to find a compatible common type between the two
operands of the operator.
1.coerce(2.5) #=> [2.5, 1.0]
1.2.coerce(3) #=> [3.0, 1.2]
1.coerce(2) #=> [2, 1]
;T;[o;
;I"
overload;F;0;;I;0; I"coerce(numeric);T;IC;" ;T;[o;!
;I"return;F;I" ;T;0;[I"
Array;T;"@d;#[ ;$I"@return [Array];T;%0;"@d;&F;'i ;(0;[[I"numeric;T0;"@d;#[ ;$I"If +numeric+ is the same type as +num+, returns an array
[numeric, num]
. Otherwise, returns an array with both
+numeric+ and +num+ represented as Float objects.
This coercion mechanism is used by Ruby to handle mixed-type numeric
operations: it is intended to find a compatible common type between the two
operands of the operator.
1.coerce(2.5) #=> [2.5, 1.0]
1.2.coerce(3) #=> [3.0, 1.2]
1.coerce(2) #=> [2, 1]
@overload coerce(numeric)
@return [Array];T;%0;"@d;&F;+o;,;-T;.i;/i;0@R;1I"static VALUE
num_coerce(VALUE x, VALUE y)
{
if (CLASS_OF(x) == CLASS_OF(y))
return rb_assoc_new(y, x);
x = rb_Float(x);
y = rb_Float(y);
return rb_assoc_new(y, x);
};T; I"static VALUE;T;2To;
;F;;
;;;I"Numeric#clone;F;[[@0;[[@i;T;:
clone;0;[ ;{ ;IC;"7Returns the receiver. +freeze+ cannot be +false+.
;T;[o;
;I"
overload;F;0;;{;0; I"clone(freeze: true);T;IC;" ;T;[o;!
;I"return;F;I" ;T;0;[I"Numeric;T;"@;#[ ;$I"@return [Numeric];T;%0;"@;&F;'i ;(0;[[I"freeze:;TI" true;T;"@;#[ ;$I"kReturns the receiver. +freeze+ cannot be +false+.
@overload clone(freeze: true)
@return [Numeric];T;%0;"@;&F;+o;,;-T;.i;/i;0@R;1I"qstatic VALUE
num_clone(int argc, VALUE *argv, VALUE x)
{
return rb_immutable_obj_clone(argc, argv, x);
};T; I"static VALUE;T;2To;
;F;;
;;;I"Numeric#dup;F;[ ;[[@i;T;:dup;0;[ ;{ ;IC;"Returns the receiver.
;T;[o;
;I"
overload;F;0;;|;0; I"dup;T;IC;" ;T;[o;!
;I"return;F;I" ;T;0;[I"Numeric;T;"@;#[ ;$I"@return [Numeric];T;%0;"@;&F;'i ;(0;[ ;"@;#[ ;$I">Returns the receiver.
@overload dup
@return [Numeric];T;%0;"@;&F;+o;,;-T;.i;/i;0@R;1I"4static VALUE
num_dup(VALUE x)
{
return x;
};T; I"static VALUE;T;2To;
;F;;
;;;I"Numeric#i;F;[ ;[[@i5;T;:i;0;[ ;{ ;IC;"|Returns the corresponding imaginary number.
Not available for complex numbers.
-42.i #=> (0-42i)
2.0.i #=> (0+2.0i)
;T;[o;
;I"
overload;F;0;;};0; I"i;T;IC;" ;T;[o;!
;I"return;F;I"Complex(0];T;0;[I"Complex(0];T;"@;#[ ;$I"@return [ Complex(0];T;%0;"@;&F;'i ;(0;[ ;"@;#[ ;$I"Returns the corresponding imaginary number.
Not available for complex numbers.
-42.i #=> (0-42i)
2.0.i #=> (0+2.0i)
@overload i
@return [ Complex(0];T;%0;"@;&F;+o;,;-T;.i*;/i2;0@R;1I"Zstatic VALUE
num_imaginary(VALUE num)
{
return rb_complex_new(INT2FIX(0), num);
};T; I"static VALUE;T;2To;
;F;;
;;;I"Numeric#+@;F;[ ;[[@i$;T;:+@;0;[ ;{ ;IC;"'Unary Plus---Returns the receiver.
;T;[o;
;I"
overload;F;0;;L;0; I" +num;T;IC;" ;T;[o;!
;I"return;F;I" ;T;0;[I"Numeric;T;"@;#[ ;$I"@return [Numeric];T;%0;"@;&F;'i ;(0;[ ;"@;#[ ;$I"LUnary Plus---Returns the receiver.
@overload +num
@return [Numeric];T;%0;"@;&F;+o;,;-T;.i;/i!;0@R;1I":static VALUE
num_uplus(VALUE num)
{
return num;
};T; I"static VALUE;T;2To;
;F;;
;;;I"Numeric#-@;F;[ ;[[@iB;T;;J;0;[ ;{ ;IC;"1Unary Minus---Returns the receiver, negated.
;T;[o;
;I"
overload;F;0;;K;0; I" -num;T;IC;" ;T;[o;!
;I"return;F;I" ;T;0;[I"Numeric;T;"@;#[ ;$I"@return [Numeric];T;%0;"@;&F;'i ;(0;[ ;"@;#[ ;$I"VUnary Minus---Returns the receiver, negated.
@overload -num
@return [Numeric];T;%0;"@;&F;+o;,;-T;.i;;/i?;0@R;1I"static VALUE
num_uminus(VALUE num)
{
VALUE zero;
zero = INT2FIX(0);
do_coerce(&zero, &num, TRUE);
return num_funcall1(zero, '-', num);
};T; I"static VALUE;T;2To;
;F;;
;;;I"Numeric#<=>;F;[[I"y;T0;[[@ij;T;;W;0;[ ;{ ;IC;"FReturns zero if +number+ equals +other+, otherwise returns +nil+.
;T;[o;
;I"
overload;F;0;;W;0; I"<=>(other);T;IC;" ;T;[o;!
;I"return;F;I" ;T;0;[I"0;TI"nil;T;"@;#[ ;$I"@return [ 0, nil];T;%0;"@;&F;'i ;(0;[[I"
other;T0;"@;#[ ;$I"qReturns zero if +number+ equals +other+, otherwise returns +nil+.
@overload <=>(other)
@return [ 0, nil];T;%0;"@;&F;+o;,;-T;.ic;/ig;0@R;1I"cstatic VALUE
num_cmp(VALUE x, VALUE y)
{
if (x == y) return INT2FIX(0);
return Qnil;
};T; I"static VALUE;T;2To;
;F;;
;;;I"Numeric#eql?;F;[[I"y;T0;[[@iW;T;;\;0;[ ;{ ;IC;"Returns +true+ if +num+ and +numeric+ are the same type and have equal
values. Contrast this with Numeric#==, which performs type conversions.
1 == 1.0 #=> true
1.eql?(1.0) #=> false
1.0.eql?(1.0) #=> true;T;[o;
;I"
overload;F;0;;\;0; I"eql?(numeric);T;IC;" ;T;[o;!
;I"return;F;I" ;T;0;[I"Boolean;T;"@.;#[ ;$I"@return [Boolean];T;%0;"@.;&F;'i ;(0;[[I"numeric;T0;"@.;#[ ;$I"Returns +true+ if +num+ and +numeric+ are the same type and have equal
values. Contrast this with Numeric#==, which performs type conversions.
1 == 1.0 #=> true
1.eql?(1.0) #=> false
1.0.eql?(1.0) #=> true
@overload eql?(numeric)
@return [Boolean];T;%0;"@.;&F;+o;,;-T;.iK;/iT;'i ;0@R;1I"static VALUE
num_eql(VALUE x, VALUE y)
{
if (TYPE(x) != TYPE(y)) return Qfalse;
if (RB_TYPE_P(x, T_BIGNUM)) {
return rb_big_eql(x, y);
}
return rb_equal(x, y);
};T; I"static VALUE;T;2To;
;F;;
;;;I"Numeric#fdiv;F;[[I"y;T0;[[@iT;T;;P;0;[ ;{ ;IC;"Returns float division.
;T;[o;
;I"
overload;F;0;;P;0; I"fdiv(numeric);T;IC;" ;T;[o;!
;I"return;F;I" ;T;0;[I"
Float;T;"@M;#[ ;$I"@return [Float];T;%0;"@M;&F;'i ;(0;[[I"numeric;T0;"@M;#[ ;$I"HReturns float division.
@overload fdiv(numeric)
@return [Float];T;%0;"@M;&F;+o;,;-T;.iM;/iQ;0@R;1I"_static VALUE
num_fdiv(VALUE x, VALUE y)
{
return rb_funcall(rb_Float(x), '/', 1, y);
};T; I"static VALUE;T;2To;
;F;;
;;;I"Numeric#div;F;[[I"y;T0;[[@if;T;:div;0;[ ;{ ;IC;"Uses +/+ to perform division, then converts the result to an integer.
Numeric does not define the +/+ operator; this is left to subclasses.
Equivalent to num.divmod(numeric)[0]
.
See Numeric#divmod.
;T;[o;
;I"
overload;F;0;;;0; I"div(numeric);T;IC;" ;T;[o;!
;I"return;F;I" ;T;0;[I"Integer;T;"@l;#[ ;$I"@return [Integer];T;%0;"@l;&F;'i ;(0;[[I"numeric;T0;"@l;#[ ;$I"Uses +/+ to perform division, then converts the result to an integer.
Numeric does not define the +/+ operator; this is left to subclasses.
Equivalent to num.divmod(numeric)[0]
.
See Numeric#divmod.
@overload div(numeric)
@return [Integer];T;%0;"@l;&F;+o;,;-T;.iZ;/ic;0@R;1I"static VALUE
num_div(VALUE x, VALUE y)
{
if (rb_equal(INT2FIX(0), y)) rb_num_zerodiv();
return rb_funcall(num_funcall1(x, '/', y), rb_intern("floor"), 0);
};T; I"static VALUE;T;2To;
;F;;
;;;I"Numeric#divmod;F;[[I"y;T0;[[@i;T;;S;0;[ ;{ ;IC;"6Returns an array containing the quotient and modulus obtained by dividing
+num+ by +numeric+.
If q, r = x.divmod(y)
, then
q = floor(x/y)
x = q*y + r
The quotient is rounded toward negative infinity, as shown in the
following table:
a | b | a.divmod(b) | a/b | a.modulo(b) | a.remainder(b)
------+-----+---------------+---------+-------------+---------------
13 | 4 | 3, 1 | 3 | 1 | 1
------+-----+---------------+---------+-------------+---------------
13 | -4 | -4, -3 | -4 | -3 | 1
------+-----+---------------+---------+-------------+---------------
-13 | 4 | -4, 3 | -4 | 3 | -1
------+-----+---------------+---------+-------------+---------------
-13 | -4 | 3, -1 | 3 | -1 | -1
------+-----+---------------+---------+-------------+---------------
11.5 | 4 | 2, 3.5 | 2.875 | 3.5 | 3.5
------+-----+---------------+---------+-------------+---------------
11.5 | -4 | -3, -0.5 | -2.875 | -0.5 | 3.5
------+-----+---------------+---------+-------------+---------------
-11.5 | 4 | -3, 0.5 | -2.875 | 0.5 | -3.5
------+-----+---------------+---------+-------------+---------------
-11.5 | -4 | 2, -3.5 | 2.875 | -3.5 | -3.5
Examples
11.divmod(3) #=> [3, 2]
11.divmod(-3) #=> [-4, -1]
11.divmod(3.5) #=> [3, 0.5]
(-11).divmod(3.5) #=> [-4, 3.0]
11.5.divmod(3.5) #=> [3, 1.0]
;T;[o;
;I"
overload;F;0;;S;0; I"divmod(numeric);T;IC;" ;T;[o;!
;I"return;F;I" ;T;0;[I"
Array;T;"@;#[ ;$I"@return [Array];T;%0;"@;&F;'i ;(0;[[I"numeric;T0;"@;#[ ;$I"dReturns an array containing the quotient and modulus obtained by dividing
+num+ by +numeric+.
If q, r = x.divmod(y)
, then
q = floor(x/y)
x = q*y + r
The quotient is rounded toward negative infinity, as shown in the
following table:
a | b | a.divmod(b) | a/b | a.modulo(b) | a.remainder(b)
------+-----+---------------+---------+-------------+---------------
13 | 4 | 3, 1 | 3 | 1 | 1
------+-----+---------------+---------+-------------+---------------
13 | -4 | -4, -3 | -4 | -3 | 1
------+-----+---------------+---------+-------------+---------------
-13 | 4 | -4, 3 | -4 | 3 | -1
------+-----+---------------+---------+-------------+---------------
-13 | -4 | 3, -1 | 3 | -1 | -1
------+-----+---------------+---------+-------------+---------------
11.5 | 4 | 2, 3.5 | 2.875 | 3.5 | 3.5
------+-----+---------------+---------+-------------+---------------
11.5 | -4 | -3, -0.5 | -2.875 | -0.5 | 3.5
------+-----+---------------+---------+-------------+---------------
-11.5 | 4 | -3, 0.5 | -2.875 | 0.5 | -3.5
------+-----+---------------+---------+-------------+---------------
-11.5 | -4 | 2, -3.5 | 2.875 | -3.5 | -3.5
Examples
11.divmod(3) #=> [3, 2]
11.divmod(-3) #=> [-4, -1]
11.divmod(3.5) #=> [3, 0.5]
(-11).divmod(3.5) #=> [-4, 3.0]
11.5.divmod(3.5) #=> [3, 1.0]
@overload divmod(numeric)
@return [Array];T;%0;"@;&F;+o;,;-T;.i;/i;0@R;1I"lstatic VALUE
num_divmod(VALUE x, VALUE y)
{
return rb_assoc_new(num_div(x, y), num_modulo(x, y));
};T; I"static VALUE;T;2To;
;F;;
;;;I"Numeric#%;F;[[I"y;T0;[[@ix;T;;Q;0;[ ;{ ;IC;"x.modulo(y)
means x-y*(x/y).floor
.
Equivalent to num.divmod(numeric)[1]
.
See Numeric#divmod.
;T;[o;
;I"
overload;F;0;;R;0; I"modulo(numeric);T;IC;" ;T;[ ;#[ ;$I" ;T;%0;"@;&F;'i ;(0;[[I"numeric;T0;"@;#[ ;$I"x.modulo(y)
means x-y*(x/y).floor
.
Equivalent to num.divmod(numeric)[1]
.
See Numeric#divmod.
@overload modulo(numeric);T;%0;"@;&F;+o;,;-T;.im;/it;0@R;1I"static VALUE
num_modulo(VALUE x, VALUE y)
{
VALUE q = num_funcall1(x, id_div, y);
return rb_funcall(x, '-', 1,
rb_funcall(y, '*', 1, q));
};T; I"static VALUE;T;2To;
;F;;
;;;I"Numeric#modulo;F;[[I"y;T0;[[@ix;T;;R;0;[ ;{ ;IC;"x.modulo(y)
means x-y*(x/y).floor
.
Equivalent to num.divmod(numeric)[1]
.
See Numeric#divmod.
;T;[o;
;I"
overload;F;0;;R;0; I"modulo(numeric);T;IC;" ;T;[ ;#[ ;$I" ;T;%0;"@;&F;'i ;(0;[[I"numeric;T0;"@;#[ ;$@;%0;"@;&F;+o;,;-T;.im;/it;0@R;1I"static VALUE
num_modulo(VALUE x, VALUE y)
{
VALUE q = num_funcall1(x, id_div, y);
return rb_funcall(x, '-', 1,
rb_funcall(y, '*', 1, q));
};T; I"static VALUE;T;2To;
;F;;
;;;I"Numeric#remainder;F;[[I"y;T0;[[@i;T;:remainder;0;[ ;{ ;IC;"\x.remainder(y)
means x-y*(x/y).truncate
.
See Numeric#divmod.
;T;[o;
;I"
overload;F;0;;{;0; I"remainder(numeric);T;IC;" ;T;[ ;#[ ;$I" ;T;%0;"@;&F;'i ;(0;[[I"numeric;T0;"@;#[ ;$I"{x.remainder(y)
means x-y*(x/y).truncate
.
See Numeric#divmod.
@overload remainder(numeric);T;%0;"@;&F;+o;,;-T;.i;/i;0@R;1I"3static VALUE
num_remainder(VALUE x, VALUE y)
{
VALUE z = num_funcall1(x, '%', y);
if ((!rb_equal(z, INT2FIX(0))) &&
((rb_num_negative_int_p(x) &&
rb_num_positive_int_p(y)) ||
(rb_num_positive_int_p(x) &&
rb_num_negative_int_p(y)))) {
return rb_funcall(z, '-', 1, y);
}
return z;
};T; I"static VALUE;T;2To;
;F;;
;;;I"Numeric#abs;F;[ ;[[@i;T;;_;0;[ ;{ ;IC;"Returns the absolute value of +num+.
12.abs #=> 12
(-34.56).abs #=> 34.56
-34.56.abs #=> 34.56
Numeric#magnitude is an alias for Numeric#abs.
;T;[o;
;I"
overload;F;0;;_;0; I"abs;T;IC;" ;T;[o;!
;I"return;F;I" ;T;0;[I"Numeric;T;"@;#[ ;$I"@return [Numeric];T;%0;"@;&F;'i ;(0;[ ;"@o;
;I"
overload;F;0;;`;0; I"magnitude;T;IC;" ;T;[o;!
;I"return;F;I" ;T;0;[I"Numeric;T;"@;#[ ;$I"@return [Numeric];T;%0;"@;&F;'i ;(0;[ ;"@;#[ ;$I"Returns the absolute value of +num+.
12.abs #=> 12
(-34.56).abs #=> 34.56
-34.56.abs #=> 34.56
Numeric#magnitude is an alias for Numeric#abs.
@overload abs
@return [Numeric]
@overload magnitude
@return [Numeric];T;%0;"@;&F;+o;,;-T;.i;/i;0@R;1I"static VALUE
num_abs(VALUE num)
{
if (rb_num_negative_int_p(num)) {
return num_funcall0(num, idUMinus);
}
return num;
};T; I"static VALUE;T;2To;
;F;;
;;;I"Numeric#magnitude;F;[ ;[[@i;T;;`;0;[ ;{ ;IC;"Returns the absolute value of +num+.
12.abs #=> 12
(-34.56).abs #=> 34.56
-34.56.abs #=> 34.56
Numeric#magnitude is an alias for Numeric#abs.
;T;[o;
;I"
overload;F;0;;_;0; I"abs;T;IC;" ;T;[o;!
;I"return;F;I" ;T;0;[I"Numeric;T;"@;#[ ;$I"@return [Numeric];T;%0;"@;&F;'i ;(0;[ ;"@o;
;I"
overload;F;0;;`;0; I"magnitude;T;IC;" ;T;[o;!
;I"return;F;I" ;T;0;[I"Numeric;T;"@;#[ ;$I"@return [Numeric];T;%0;"@;&F;'i ;(0;[ ;"@;#[ ;$@;%0;"@;&F;+o;,;-T;.i;/i;0@R;1I"static VALUE
num_abs(VALUE num)
{
if (rb_num_negative_int_p(num)) {
return num_funcall0(num, idUMinus);
}
return num;
};T; I"static VALUE;T;2To;
;F;;
;;;I"Numeric#to_int;F;[ ;[[@i_;T;;c;0;[ ;{ ;IC;"Invokes the child class's +to_i+ method to convert +num+ to an integer.
1.0.class #=> Float
1.0.to_int.class #=> Integer
1.0.to_i.class #=> Integer
;T;[o;
;I"
overload;F;0;;c;0; I"to_int;T;IC;" ;T;[o;!
;I"return;F;I" ;T;0;[I"Integer;T;"@F;#[ ;$I"@return [Integer];T;%0;"@F;&F;'i ;(0;[ ;"@F;#[ ;$I"Invokes the child class's +to_i+ method to convert +num+ to an integer.
1.0.class #=> Float
1.0.to_int.class #=> Integer
1.0.to_i.class #=> Integer
@overload to_int
@return [Integer];T;%0;"@F;&F;+o;,;-T;.iT;/i\;0@R;1I"Rstatic VALUE
num_to_int(VALUE num)
{
return num_funcall0(num, id_to_i);
};T; I"static VALUE;T;2To;
;F;;
;;;I"Numeric#real?;F;[ ;[[@i;T;:
real?;0;[ ;{ ;IC;"AReturns +true+ if +num+ is a real number (i.e. not Complex).;T;[o;
;I"
overload;F;0;;|;0; I"
real?;T;IC;" ;T;[o;!
;I"return;F;I" ;T;0;[I"Boolean;T;"@a;#[ ;$I"@return [Boolean];T;%0;"@a;&F;'i ;(0;[ ;"@a;#[ ;$I"gReturns +true+ if +num+ is a real number (i.e. not Complex).
@overload real?
@return [Boolean];T;%0;"@a;&F;+o;,;-T;.i;/i;'i ;0@R;1I"=static VALUE
num_real_p(VALUE num)
{
return Qtrue;
};T; I"static VALUE;T;2To;
;F;;
;;;I"Numeric#integer?;F;[ ;[[@i;T;:
integer?;0;[ ;{ ;IC;"eReturns +true+ if +num+ is an Integer.
1.0.integer? #=> false
1.integer? #=> true;T;[o;
;I"
overload;F;0;;};0; I"
integer?;T;IC;" ;T;[o;!
;I"return;F;I" ;T;0;[I"Boolean;T;"@|;#[ ;$I"@return [Boolean];T;%0;"@|;&F;'i ;(0;[ ;"@|;#[ ;$I"Returns +true+ if +num+ is an Integer.
1.0.integer? #=> false
1.integer? #=> true
@overload integer?
@return [Boolean];T;%0;"@|;&F;+o;,;-T;.i;/i;'i ;0@R;1I"=static VALUE
num_int_p(VALUE num)
{
return Qfalse;
};T; I"static VALUE;T;2To;
;F;;
;;;I"Numeric#zero?;F;[ ;[[@i;T;;a;0;[ ;{ ;IC;".Returns +true+ if +num+ has a zero value.;T;[o;
;I"
overload;F;0;;a;0; I"
zero?;T;IC;" ;T;[o;!
;I"return;F;I" ;T;0;[I"Boolean;T;"@;#[ ;$I"@return [Boolean];T;%0;"@;&F;'i ;(0;[ ;"@;#[ ;$I"TReturns +true+ if +num+ has a zero value.
@overload zero?
@return [Boolean];T;%0;"@;&F;+o;,;-T;.i;/i;'i ;0@R;1I"static VALUE
num_zero_p(VALUE num)
{
if (rb_equal(num, INT2FIX(0))) {
return Qtrue;
}
return Qfalse;
};T; I"static VALUE;T;2To;
;F;;
;;;I"Numeric#nonzero?;F;[ ;[[@i2;T;:
nonzero?;0;[ ;{ ;IC;"Returns +self+ if +num+ is not zero, +nil+ otherwise.
This behavior is useful when chaining comparisons:
a = %w( z Bb bB bb BB a aA Aa AA A )
b = a.sort {|a,b| (a.downcase <=> b.downcase).nonzero? || a <=> b }
b #=> ["A", "a", "AA", "Aa", "aA", "BB", "Bb", "bB", "bb", "z"];T;[o;
;I"
overload;F;0;;~;0; I"
nonzero?;T;IC;" ;T;[o;!
;I"return;F;I" ;T;0;[I" self;TI"nil;T;"@;#[ ;$I"@return [self, nil];T;%0;"@;&F;'i ;(0;[ ;"@;#[ ;$I"IReturns +self+ if +num+ is not zero, +nil+ otherwise.
This behavior is useful when chaining comparisons:
a = %w( z Bb bB bb BB a aA Aa AA A )
b = a.sort {|a,b| (a.downcase <=> b.downcase).nonzero? || a <=> b }
b #=> ["A", "a", "AA", "Aa", "aA", "BB", "Bb", "bB", "bb", "z"]
@overload nonzero?
@return [self, nil];T;%0;"@;&F;+o;,;-T;.i%;/i/;'i ;0@R;1I"static VALUE
num_nonzero_p(VALUE num)
{
if (RTEST(num_funcall0(num, rb_intern("zero?")))) {
return Qnil;
}
return num;
};T; I"static VALUE;T;2To;
;F;;
;;;I"Numeric#finite?;F;[ ;[[@iA;T;;j;0;[ ;{ ;IC;"KReturns +true+ if +num+ is a finite number, otherwise returns +false+.;T;[o;
;I"
overload;F;0;;j;0; I"finite?;T;IC;" ;T;[o;!
;I"return;F;I" ;T;0;[I"Boolean;T;"@;#[ ;$I"@return [Boolean];T;%0;"@;&F;'i ;(0;[ ;"@;#[ ;$I"sReturns +true+ if +num+ is a finite number, otherwise returns +false+.
@overload finite?
@return [Boolean];T;%0;"@;&F;+o;,;-T;.i;;/i?;'i ;0@R;1I"?static VALUE
num_finite_p(VALUE num)
{
return Qtrue;
};T; I"static VALUE;T;2To;
;F;;
;;;I"Numeric#infinite?;F;[ ;[[@iN;T;;i;0;[ ;{ ;IC;"yReturns +nil+, -1, or 1 depending on whether the value is
finite, -Infinity
, or +Infinity
.;T;[o;
;I"
overload;F;0;;i;0; I"infinite?;T;IC;" ;T;[o;!
;I"return;F;I" ;T;0;[I"-1;TI"1;TI"nil;T;"@;#[ ;$I"@return [ -1, 1, nil];T;%0;"@;&F;'i ;(0;[ ;"@;#[ ;$I"Returns +nil+, -1, or 1 depending on whether the value is
finite, -Infinity
, or +Infinity
.
@overload infinite?
@return [ -1, 1, nil];T;%0;"@;&F;+o;,;-T;.iG;/iL;'i ;0@R;1I"@static VALUE
num_infinite_p(VALUE num)
{
return Qnil;
};T; I"static VALUE;T;2To;
;F;;
;;;I"Numeric#floor;F;[[@0;[[@i ;T;;d;0;[ ;{ ;IC;"Returns the largest number less than or equal to +num+ with
a precision of +ndigits+ decimal digits (default: 0).
Numeric implements this by converting its value to a Float and
invoking Float#floor.
;T;[o;
;I"
overload;F;0;;d;0; I"floor([ndigits]);T;IC;" ;T;[o;!
;I"return;F;I" ;T;0;[I"Integer;TI"
Float;T;"@ ;#[ ;$I"@return [Integer, Float];T;%0;"@ ;&F;'i ;(0;[[I"[ndigits];T0;"@ ;#[ ;$I"Returns the largest number less than or equal to +num+ with
a precision of +ndigits+ decimal digits (default: 0).
Numeric implements this by converting its value to a Float and
invoking Float#floor.
@overload floor([ndigits])
@return [Integer, Float];T;%0;"@ ;&F;+o;,;-T;.i ;/i ;0@R;1I"rstatic VALUE
num_floor(int argc, VALUE *argv, VALUE num)
{
return flo_floor(argc, argv, rb_Float(num));
};T; I"static VALUE;T;2To;
;F;;
;;;I"Numeric#ceil;F;[[@0;[[@i ;T;;e;0;[ ;{ ;IC;"Returns the smallest number greater than or equal to +num+ with
a precision of +ndigits+ decimal digits (default: 0).
Numeric implements this by converting its value to a Float and
invoking Float#ceil.
;T;[o;
;I"
overload;F;0;;e;0; I"ceil([ndigits]);T;IC;" ;T;[o;!
;I"return;F;I" ;T;0;[I"Integer;TI"
Float;T;"@% ;#[ ;$I"@return [Integer, Float];T;%0;"@% ;&F;'i ;(0;[[I"[ndigits];T0;"@% ;#[ ;$I"Returns the smallest number greater than or equal to +num+ with
a precision of +ndigits+ decimal digits (default: 0).
Numeric implements this by converting its value to a Float and
invoking Float#ceil.
@overload ceil([ndigits])
@return [Integer, Float];T;%0;"@% ;&F;+o;,;-T;.i ;/i ;0@R;1I"pstatic VALUE
num_ceil(int argc, VALUE *argv, VALUE num)
{
return flo_ceil(argc, argv, rb_Float(num));
};T; I"static VALUE;T;2To;
;F;;
;;;I"Numeric#round;F;[[@0;[[@i ;T;;f;0;[ ;{ ;IC;"Returns +num+ rounded to the nearest value with
a precision of +ndigits+ decimal digits (default: 0).
Numeric implements this by converting its value to a Float and
invoking Float#round.
;T;[o;
;I"
overload;F;0;;f;0; I"round([ndigits]);T;IC;" ;T;[o;!
;I"return;F;I" ;T;0;[I"Integer;TI"
Float;T;"@D ;#[ ;$I"@return [Integer, Float];T;%0;"@D ;&F;'i ;(0;[[I"[ndigits];T0;"@D ;#[ ;$I"Returns +num+ rounded to the nearest value with
a precision of +ndigits+ decimal digits (default: 0).
Numeric implements this by converting its value to a Float and
invoking Float#round.
@overload round([ndigits])
@return [Integer, Float];T;%0;"@D ;&F;+o;,;-T;.i ;/i ;0@R;1I"rstatic VALUE
num_round(int argc, VALUE* argv, VALUE num)
{
return flo_round(argc, argv, rb_Float(num));
};T; I"static VALUE;T;2To;
;F;;
;;;I"Numeric#truncate;F;[[@0;[[@i ;T;;g;0;[ ;{ ;IC;"Returns +num+ truncated (toward zero) to
a precision of +ndigits+ decimal digits (default: 0).
Numeric implements this by converting its value to a Float and
invoking Float#truncate.
;T;[o;
;I"
overload;F;0;;g;0; I"truncate([ndigits]);T;IC;" ;T;[o;!
;I"return;F;I" ;T;0;[I"Integer;TI"
Float;T;"@c ;#[ ;$I"@return [Integer, Float];T;%0;"@c ;&F;'i ;(0;[[I"[ndigits];T0;"@c ;#[ ;$I"Returns +num+ truncated (toward zero) to
a precision of +ndigits+ decimal digits (default: 0).
Numeric implements this by converting its value to a Float and
invoking Float#truncate.
@overload truncate([ndigits])
@return [Integer, Float];T;%0;"@c ;&F;+o;,;-T;.i ;/i ;0@R;1I"xstatic VALUE
num_truncate(int argc, VALUE *argv, VALUE num)
{
return flo_truncate(argc, argv, rb_Float(num));
};T; I"static VALUE;T;2To;
;F;;
;;;I"Numeric#step;F;[[@0;[[@i
;T;: step;0;[ ;{ ;IC;"4Invokes the given block with the sequence of numbers starting at +num+,
incremented by +step+ (defaulted to +1+) on each call.
The loop finishes when the value to be passed to the block is greater than
+limit+ (if +step+ is positive) or less than +limit+ (if +step+ is
negative), where +limit+ is defaulted to infinity.
In the recommended keyword argument style, either or both of
+step+ and +limit+ (default infinity) can be omitted. In the
fixed position argument style, zero as a step
(i.e. num.step(limit, 0)
) is not allowed for historical
compatibility reasons.
If all the arguments are integers, the loop operates using an integer
counter.
If any of the arguments are floating point numbers, all are converted
to floats, and the loop is executed
floor(n + n*Float::EPSILON) + 1 times,
where n = (limit - num)/step.
Otherwise, the loop starts at +num+, uses either the
less-than (<
) or greater-than (>
) operator
to compare the counter against +limit+,
and increments itself using the +
operator.
If no block is given, an Enumerator is returned instead.
Especially, the enumerator is an Enumerator::ArithmeticSequence
if both +limit+ and +step+ are kind of Numeric or nil
.
For example:
p 1.step.take(4)
p 10.step(by: -1).take(4)
3.step(to: 5) {|i| print i, " " }
1.step(10, 2) {|i| print i, " " }
Math::E.step(to: Math::PI, by: 0.2) {|f| print f, " " }
Will produce:
[1, 2, 3, 4]
[10, 9, 8, 7]
3 4 5
1 3 5 7 9
2.718281828459045 2.9182818284590453 3.118281828459045
;T;[o;
;I"
overload;F;0;;;0; I"step(by: step, to: limit);T;IC;" ;T;[o;!
;I"
yield;F;I" ;T;0;[I"i;T;"@ o;!
;I"return;F;I" ;T;0;[I" self;T;"@ ;#[ ;$I"@yield [i]
@return [self];T;%0;"@ ;&F;'i ;(0;[[I"by:;TI" step;T[I"to:;TI"
limit;T;"@ o;
;I"
overload;F;0;;;0; I"step(by: step, to: limit);T;IC;" ;T;[ ;#[ ;$I" ;T;%0;"@ ;&F;'i ;(0;[[I"by:;TI" step;T[I"to:;TI"
limit;T;"@ o;
;I"
overload;F;0;;;0; I"step(by: step, to: limit);T;IC;" ;T;[ ;#[ ;$I" ;T;%0;"@ ;&F;'i ;(0;[[I"by:;TI" step;T[I"to:;TI"
limit;T;"@ o;
;I"
overload;F;0;;;0; I"step(limit=nil, step=1);T;IC;" ;T;[o;!
;I"
yield;F;I" ;T;0;[I"i;T;"@ o;!
;I"return;F;I" ;T;0;[I" self;T;"@ ;#[ ;$I"@yield [i]
@return [self];T;%0;"@ ;&F;'i ;(0;[[I"
limit;TI"nil;T[I" step;TI"1;T;"@ o;
;I"
overload;F;0;;;0; I"step(limit=nil, step=1);T;IC;" ;T;[ ;#[ ;$I" ;T;%0;"@ ;&F;'i ;(0;[[I"
limit;TI"nil;T[I" step;TI"1;T;"@ o;
;I"
overload;F;0;;;0; I"step(limit=nil, step=1);T;IC;" ;T;[ ;#[ ;$I" ;T;%0;"@ ;&F;'i ;(0;[[I"
limit;TI"nil;T[I" step;TI"1;T;"@ ;#[ ;$I"DInvokes the given block with the sequence of numbers starting at +num+,
incremented by +step+ (defaulted to +1+) on each call.
The loop finishes when the value to be passed to the block is greater than
+limit+ (if +step+ is positive) or less than +limit+ (if +step+ is
negative), where +limit+ is defaulted to infinity.
In the recommended keyword argument style, either or both of
+step+ and +limit+ (default infinity) can be omitted. In the
fixed position argument style, zero as a step
(i.e. num.step(limit, 0)
) is not allowed for historical
compatibility reasons.
If all the arguments are integers, the loop operates using an integer
counter.
If any of the arguments are floating point numbers, all are converted
to floats, and the loop is executed
floor(n + n*Float::EPSILON) + 1 times,
where n = (limit - num)/step.
Otherwise, the loop starts at +num+, uses either the
less-than (<
) or greater-than (>
) operator
to compare the counter against +limit+,
and increments itself using the +
operator.
If no block is given, an Enumerator is returned instead.
Especially, the enumerator is an Enumerator::ArithmeticSequence
if both +limit+ and +step+ are kind of Numeric or nil
.
For example:
p 1.step.take(4)
p 10.step(by: -1).take(4)
3.step(to: 5) {|i| print i, " " }
1.step(10, 2) {|i| print i, " " }
Math::E.step(to: Math::PI, by: 0.2) {|f| print f, " " }
Will produce:
[1, 2, 3, 4]
[10, 9, 8, 7]
3 4 5
1 3 5 7 9
2.718281828459045 2.9182818284590453 3.118281828459045
@overload step(by: step, to: limit)
@yield [i]
@return [self]
@overload step(by: step, to: limit)
@overload step(by: step, to: limit)
@overload step(limit=nil, step=1)
@yield [i]
@return [self]
@overload step(limit=nil, step=1)
@overload step(limit=nil, step=1);T;%0;"@ ;&F;+o;,;-T;.i
;/i
;0@R;1I"static VALUE
num_step(int argc, VALUE *argv, VALUE from)
{
VALUE to, step;
int desc, inf;
if (!rb_block_given_p()) {
VALUE by = Qundef;
num_step_extract_args(argc, argv, &to, &step, &by);
if (by != Qundef) {
step = by;
}
if (NIL_P(step)) {
step = INT2FIX(1);
}
else if (rb_equal(step, INT2FIX(0))) {
rb_raise(rb_eArgError, "step can't be 0");
}
if ((NIL_P(to) || rb_obj_is_kind_of(to, rb_cNumeric)) &&
rb_obj_is_kind_of(step, rb_cNumeric)) {
return rb_arith_seq_new(from, ID2SYM(rb_frame_this_func()), argc, argv,
num_step_size, from, to, step, FALSE);
}
return SIZED_ENUMERATOR(from, 2, ((VALUE [2]){to, step}), num_step_size);
}
desc = num_step_scan_args(argc, argv, &to, &step, TRUE, FALSE);
if (rb_equal(step, INT2FIX(0))) {
inf = 1;
}
else if (RB_TYPE_P(to, T_FLOAT)) {
double f = RFLOAT_VALUE(to);
inf = isinf(f) && (signbit(f) ? desc : !desc);
}
else inf = 0;
if (FIXNUM_P(from) && (inf || FIXNUM_P(to)) && FIXNUM_P(step)) {
long i = FIX2LONG(from);
long diff = FIX2LONG(step);
if (inf) {
for (;; i += diff)
rb_yield(LONG2FIX(i));
}
else {
long end = FIX2LONG(to);
if (desc) {
for (; i >= end; i += diff)
rb_yield(LONG2FIX(i));
}
else {
for (; i <= end; i += diff)
rb_yield(LONG2FIX(i));
}
}
}
else if (!ruby_float_step(from, to, step, FALSE, FALSE)) {
VALUE i = from;
if (inf) {
for (;; i = rb_funcall(i, '+', 1, step))
rb_yield(i);
}
else {
ID cmp = desc ? '<' : '>';
for (; !RTEST(rb_funcall(i, cmp, 1, to)); i = rb_funcall(i, '+', 1, step))
rb_yield(i);
}
}
return from;
};T; I"static VALUE;T;2To;
;F;;
;;;I"Numeric#positive?;F;[ ;[[@il;T;;m;0;[ ;{ ;IC;"/Returns +true+ if +num+ is greater than 0.;T;[o;
;I"
overload;F;0;;m;0; I"positive?;T;IC;" ;T;[o;!
;I"return;F;I" ;T;0;[I"Boolean;T;"@ ;#[ ;$I"@return [Boolean];T;%0;"@ ;&F;'i ;(0;[ ;"@ ;#[ ;$I"YReturns +true+ if +num+ is greater than 0.
@overload positive?
@return [Boolean];T;%0;"@ ;&F;+o;,;-T;.ie;/ii;'i ;0@R;1I"static VALUE
num_positive_p(VALUE num)
{
const ID mid = '>';
if (FIXNUM_P(num)) {
if (method_basic_p(rb_cInteger))
return (SIGNED_VALUE)num > (SIGNED_VALUE)INT2FIX(0) ? Qtrue : Qfalse;
}
else if (RB_TYPE_P(num, T_BIGNUM)) {
if (method_basic_p(rb_cInteger))
return BIGNUM_POSITIVE_P(num) && !rb_bigzero_p(num) ? Qtrue : Qfalse;
}
return rb_num_compare_with_zero(num, mid);
};T; I"static VALUE;T;2To;
;F;;
;;;I"Numeric#negative?;F;[ ;[[@i;T;;n;0;[ ;{ ;IC;",Returns +true+ if +num+ is less than 0.;T;[o;
;I"
overload;F;0;;n;0; I"negative?;T;IC;" ;T;[o;!
;I"return;F;I" ;T;0;[I"Boolean;T;"@
;#[ ;$I"@return [Boolean];T;%0;"@
;&F;'i ;(0;[ ;"@
;#[ ;$I"VReturns +true+ if +num+ is less than 0.
@overload negative?
@return [Boolean];T;%0;"@
;&F;+o;,;-T;.i|;/i;'i ;0@R;1I"gstatic VALUE
num_negative_p(VALUE num)
{
return rb_num_negative_int_p(num) ? Qtrue : Qfalse;
};T; I"static VALUE;T;2To;
;F;;
;;;I"Numeric#to_c;F;[ ;[[@i;T;: to_c;0;[ ;{ ;IC;"$Returns the value as a complex.
;T;[o;
;I"
overload;F;0;;;0; I" to_c;T;IC;" ;T;[ ;#[ ;$I" ;T;%0;"@/
;&F;'i ;(0;[ ;"@/
;#[ ;$I"5Returns the value as a complex.
@overload to_c;T;%0;"@/
;&F;+o;,;-T;.i;/i;0@R;1I"Pstatic VALUE
numeric_to_c(VALUE self)
{
return rb_complex_new1(self);
};T; I"static VALUE;T;2To;
;F;;
;;;I"Numeric#real;F;[ ;[[@io;T;: real;0;[ ;{ ;IC;"Returns self.
;T;[o;
;I"
overload;F;0;;;0; I" real;T;IC;" ;T;[o;!
;I"return;F;I" ;T;0;[I" self;T;"@E
;#[ ;$I"@return [self];T;%0;"@E
;&F;'i ;(0;[ ;"@E
;#[ ;$I"4Returns self.
@overload real
@return [self];T;%0;"@E
;&F;+o;,;-T;.ii;/im;0@R;1I"?static VALUE
numeric_real(VALUE self)
{
return self;
};T; I"static VALUE;T;2To;
;F;;
;;;I"Numeric#imaginary;F;[ ;[[@i|;T;:imaginary;0;[ ;{ ;IC;"Returns zero.
;T;[o;
;I"
overload;F;0;: imag;0; I" imag;T;IC;" ;T;[o;!
;I"return;F;I" ;T;0;[I"0;T;"@`
;#[ ;$I"@return [ 0];T;%0;"@`
;&F;'i ;(0;[ ;"@`
o;
;I"
overload;F;0;;;0; I"imaginary;T;IC;" ;T;[o;!
;I"return;F;I" ;T;0;[I"0;T;"@`
;#[ ;$I"@return [ 0];T;%0;"@`
;&F;'i ;(0;[ ;"@`
;#[ ;$I"UReturns zero.
@overload imag
@return [ 0]
@overload imaginary
@return [ 0];T;%0;"@`
;&F;+o;,;-T;.iu;/i{;0@R;1I"Estatic VALUE
numeric_imag(VALUE self)
{
return INT2FIX(0);
};T; I"static VALUE;T;2To;
;F;;
;;;I"Numeric#imag;F;[ ;[[@i|;T;;;0;[ ;{ ;IC;"Returns zero.
;T;[o;
;I"
overload;F;0;;;0; I" imag;T;IC;" ;T;[o;!
;I"return;F;I" ;T;0;[I"0;T;"@
;#[ ;$I"@return [ 0];T;%0;"@
;&F;'i ;(0;[ ;"@
o;
;I"
overload;F;0;;;0; I"imaginary;T;IC;" ;T;[o;!
;I"return;F;I" ;T;0;[I"0;T;"@
;#[ ;$I"@return [ 0];T;%0;"@
;&F;'i ;(0;[ ;"@
;#[ ;$@
;%0;"@
;&F;+o;,;-T;.iu;/i{;0@R;1I"Estatic VALUE
numeric_imag(VALUE self)
{
return INT2FIX(0);
};T; I"static VALUE;T;2To;
;F;;
;;;I"Numeric#abs2;F;[ ;[[@i;T;: abs2;0;[ ;{ ;IC;"Returns square of self.
;T;[o;
;I"
overload;F;0;;;0; I" abs2;T;IC;" ;T;[ ;#[ ;$I" ;T;%0;"@
;&F;'i ;(0;[ ;"@
;#[ ;$I"-Returns square of self.
@overload abs2;T;%0;"@
;&F;+o;,;-T;.i;/i;0@R;1I"Lstatic VALUE
numeric_abs2(VALUE self)
{
return f_mul(self, self);
};T; I"static VALUE;T;2To;
;F;;
;;;I"Numeric#arg;F;[ ;[[@i;T;;;0;[ ;{ ;IC;"6Returns 0 if the value is positive, pi otherwise.
;T;[o;
;I"
overload;F;0;;;0; I"arg;T;IC;" ;T;[o;!
;I"return;F;I" ;T;0;[I"0;TI"
Float;T;"@
;#[ ;$I"@return [ 0, Float];T;%0;"@
;&F;'i ;(0;[ ;"@
o;
;I"
overload;F;0;;);0; I"
angle;T;IC;" ;T;[o;!
;I"return;F;I" ;T;0;[I"0;TI"
Float;T;"@
;#[ ;$I"@return [ 0, Float];T;%0;"@
;&F;'i ;(0;[ ;"@
o;
;I"
overload;F;0;;*;0; I"
phase;T;IC;" ;T;[o;!
;I"return;F;I" ;T;0;[I"0;TI"
Float;T;"@
;#[ ;$I"@return [ 0, Float];T;%0;"@
;&F;'i ;(0;[ ;"@
;#[ ;$I"Returns 0 if the value is positive, pi otherwise.
@overload arg
@return [ 0, Float]
@overload angle
@return [ 0, Float]
@overload phase
@return [ 0, Float];T;%0;"@
;&F;+o;,;-T;.i;/i;0@R;1I"~static VALUE
numeric_arg(VALUE self)
{
if (f_positive_p(self))
return INT2FIX(0);
return DBL2NUM(M_PI);
};T; I"static VALUE;T;2To;
;F;;
;;;I"Numeric#angle;F;[ ;[[@i;T;;);0;[ ;{ ;IC;"6Returns 0 if the value is positive, pi otherwise.
;T;[o;
;I"
overload;F;0;;;0; I"arg;T;IC;" ;T;[o;!
;I"return;F;I" ;T;0;[I"0;TI"
Float;T;"@
;#[ ;$I"@return [ 0, Float];T;%0;"@
;&F;'i ;(0;[ ;"@
o;
;I"
overload;F;0;;);0; I"
angle;T;IC;" ;T;[o;!
;I"return;F;I" ;T;0;[I"0;TI"
Float;T;"@
;#[ ;$I"@return [ 0, Float];T;%0;"@
;&F;'i ;(0;[ ;"@
o;
;I"
overload;F;0;;*;0; I"
phase;T;IC;" ;T;[o;!
;I"return;F;I" ;T;0;[I"0;TI"
Float;T;"@
;#[ ;$I"@return [ 0, Float];T;%0;"@
;&F;'i ;(0;[ ;"@
;#[ ;$@
;%0;"@
;&F;+o;,;-T;.i;/i;0@R;1I"~static VALUE
numeric_arg(VALUE self)
{
if (f_positive_p(self))
return INT2FIX(0);
return DBL2NUM(M_PI);
};T; I"static VALUE;T;2To;
;F;;
;;;I"Numeric#phase;F;[ ;[[@i;T;;*;0;[ ;{ ;IC;"6Returns 0 if the value is positive, pi otherwise.
;T;[o;
;I"
overload;F;0;;;0; I"arg;T;IC;" ;T;[o;!
;I"return;F;I" ;T;0;[I"0;TI"
Float;T;"@4;#[ ;$I"@return [ 0, Float];T;%0;"@4;&F;'i ;(0;[ ;"@4o;
;I"
overload;F;0;;);0; I"
angle;T;IC;" ;T;[o;!
;I"return;F;I" ;T;0;[I"0;TI"
Float;T;"@4;#[ ;$I"@return [ 0, Float];T;%0;"@4;&F;'i ;(0;[ ;"@4o;
;I"
overload;F;0;;*;0; I"
phase;T;IC;" ;T;[o;!
;I"return;F;I" ;T;0;[I"0;TI"
Float;T;"@4;#[ ;$I"@return [ 0, Float];T;%0;"@4;&F;'i ;(0;[ ;"@4;#[ ;$@
;%0;"@4;&F;+o;,;-T;.i;/i;0@R;1I"~static VALUE
numeric_arg(VALUE self)
{
if (f_positive_p(self))
return INT2FIX(0);
return DBL2NUM(M_PI);
};T; I"static VALUE;T;2To;
;F;;
;;;I"Numeric#rectangular;F;[ ;[[@i;T;:rectangular;0;[ ;{ ;IC;" Returns an array; [num, 0].
;T;[o;
;I"
overload;F;0;: rect;0; I" rect;T;IC;" ;T;[o;!
;I"return;F;I" ;T;0;[I"
Array;T;"@k;#[ ;$I"@return [Array];T;%0;"@k;&F;'i ;(0;[ ;"@ko;
;I"
overload;F;0;;;0; I"rectangular;T;IC;" ;T;[o;!
;I"return;F;I" ;T;0;[I"
Array;T;"@k;#[ ;$I"@return [Array];T;%0;"@k;&F;'i ;(0;[ ;"@k;#[ ;$I"kReturns an array; [num, 0].
@overload rect
@return [Array]
@overload rectangular
@return [Array];T;%0;"@k;&F;+o;,;-T;.i;/i;0@R;1I"Ystatic VALUE
numeric_rect(VALUE self)
{
return rb_assoc_new(self, INT2FIX(0));
};T; I"static VALUE;T;2To;
;F;;
;;;I"Numeric#rect;F;[ ;[[@i;T;;;0;[ ;{ ;IC;" Returns an array; [num, 0].
;T;[o;
;I"
overload;F;0;;;0; I" rect;T;IC;" ;T;[o;!
;I"return;F;I" ;T;0;[I"
Array;T;"@;#[ ;$I"@return [Array];T;%0;"@;&F;'i ;(0;[ ;"@o;
;I"
overload;F;0;;;0; I"rectangular;T;IC;" ;T;[o;!
;I"return;F;I" ;T;0;[I"
Array;T;"@;#[ ;$I"@return [Array];T;%0;"@;&F;'i ;(0;[ ;"@;#[ ;$@;%0;"@;&F;+o;,;-T;.i;/i;0@R;1I"Ystatic VALUE
numeric_rect(VALUE self)
{
return rb_assoc_new(self, INT2FIX(0));
};T; I"static VALUE;T;2To;
;F;;
;;;I"Numeric#polar;F;[ ;[[@i;T;:
polar;0;[ ;{ ;IC;"*Returns an array; [num.abs, num.arg].
;T;[o;
;I"
overload;F;0;;;0; I"
polar;T;IC;" ;T;[o;!
;I"return;F;I" ;T;0;[I"
Array;T;"@;#[ ;$I"@return [Array];T;%0;"@;&F;'i ;(0;[ ;"@;#[ ;$I"NReturns an array; [num.abs, num.arg].
@overload polar
@return [Array];T;%0;"@;&F;+o;,;-T;.i;/i;0@R;1I"static VALUE
numeric_polar(VALUE self)
{
VALUE abs, arg;
if (RB_INTEGER_TYPE_P(self)) {
abs = rb_int_abs(self);
arg = numeric_arg(self);
}
else if (RB_FLOAT_TYPE_P(self)) {
abs = rb_float_abs(self);
arg = float_arg(self);
}
else if (RB_TYPE_P(self, T_RATIONAL)) {
abs = rb_rational_abs(self);
arg = numeric_arg(self);
}
else {
abs = f_abs(self);
arg = f_arg(self);
}
return rb_assoc_new(abs, arg);
};T; I"static VALUE;T;2To;
;F;;
;;;I"Numeric#conjugate;F;[ ;[[@i;T;:conjugate;0;[ ;{ ;IC;"Returns self.
;T;[o;
;I"
overload;F;0;: conj;0; I" conj;T;IC;" ;T;[o;!
;I"return;F;I" ;T;0;[I" self;T;"@;#[ ;$I"@return [self];T;%0;"@;&F;'i ;(0;[ ;"@o;
;I"
overload;F;0;;;0; I"conjugate;T;IC;" ;T;[o;!
;I"return;F;I" ;T;0;[I" self;T;"@;#[ ;$I"@return [self];T;%0;"@;&F;'i ;(0;[ ;"@;#[ ;$I"YReturns self.
@overload conj
@return [self]
@overload conjugate
@return [self];T;%0;"@;&F;+o;,;-T;.i;/i;0@R;1I"?static VALUE
numeric_conj(VALUE self)
{
return self;
};T; I"static VALUE;T;2To;
;F;;
;;;I"Numeric#conj;F;[ ;[[@i;T;;;0;[ ;{ ;IC;"Returns self.
;T;[o;
;I"
overload;F;0;;;0; I" conj;T;IC;" ;T;[o;!
;I"return;F;I" ;T;0;[I" self;T;"@;#[ ;$I"@return [self];T;%0;"@;&F;'i ;(0;[ ;"@o;
;I"
overload;F;0;;;0; I"conjugate;T;IC;" ;T;[o;!
;I"return;F;I" ;T;0;[I" self;T;"@;#[ ;$I"@return [self];T;%0;"@;&F;'i ;(0;[ ;"@;#[ ;$@;%0;"@;&F;+o;,;-T;.i;/i;0@R;1I"?static VALUE
numeric_conj(VALUE self)
{
return self;
};T; I"static VALUE;T;2To;
;F;;
;;;I"Numeric#numerator;F;[ ;[[@i;T;;3;0;[ ;{ ;IC;"Returns the numerator.
;T;[o;
;I"
overload;F;0;;3;0; I"numerator;T;IC;" ;T;[o;!
;I"return;F;I" ;T;0;[I"Integer;T;"@$;#[ ;$I"@return [Integer];T;%0;"@$;&F;'i ;(0;[ ;"@$;#[ ;$I"EReturns the numerator.
@overload numerator
@return [Integer];T;%0;"@$;&F;+o;,;-T;.i;/i;0@R;1I"Ystatic VALUE
numeric_numerator(VALUE self)
{
return f_numerator(f_to_r(self));
};T; I"static VALUE;T;2To;
;F;;
;;;I"Numeric#denominator;F;[ ;[[@i;T;;4;0;[ ;{ ;IC;"/Returns the denominator (always positive).
;T;[o;
;I"
overload;F;0;;4;0; I"denominator;T;IC;" ;T;[o;!
;I"return;F;I" ;T;0;[I"Integer;T;"@?;#[ ;$I"@return [Integer];T;%0;"@?;&F;'i ;(0;[ ;"@?;#[ ;$I"[Returns the denominator (always positive).
@overload denominator
@return [Integer];T;%0;"@?;&F;+o;,;-T;.i;/i;0@R;1I"]static VALUE
numeric_denominator(VALUE self)
{
return f_denominator(f_to_r(self));
};T; I"static VALUE;T;2To;
;F;;
;;;I"Numeric#quo;F;[[I"y;T0;[[@i;T;;O;0;[ ;{ ;IC;"OReturns the most exact division (rational for integers, float for floats).
;T;[o;
;I"
overload;F;0;;O;0; I"quo(int_or_rat);T;IC;" ;T;[ ;#[ ;$I" ;T;%0;"@Z;&F;'i ;(0;[[I"int_or_rat;T0;"@Zo;
;I"
overload;F;0;;O;0; I"
quo(flo);T;IC;" ;T;[ ;#[ ;$I" ;T;%0;"@Z;&F;'i ;(0;[[I"flo;T0;"@Z;#[ ;$I"~Returns the most exact division (rational for integers, float for floats).
@overload quo(int_or_rat)
@overload quo(flo);T;%0;"@Z;&F;+o;,;-T;.i;/i;0@R;1I"*VALUE
rb_numeric_quo(VALUE x, VALUE y)
{
if (RB_TYPE_P(x, T_COMPLEX)) {
return rb_complex_div(x, y);
}
if (RB_FLOAT_TYPE_P(y)) {
return rb_funcallv(x, idFdiv, 1, &y);
}
x = rb_convert_type(x, T_RATIONAL, "Rational", "to_r");
return rb_rational_div(x, y);
};T; I"
VALUE;T;2T;o@R;pIC;[ ;o@R;qIC;[o:YARD::CodeObjects::Proxy:@orignamespace0:@origname0:
@imethod0;:Comparable;0@: @objo:$YARD::CodeObjects::ModuleObject;IC;[o;
;F;;
;;;I"Comparable#==;F;[[I"y;T0;[[I"
compar.c;TiS;T;;U;0;[ ;{ ;IC;"Compares two objects based on the receiver's <=>
method, returning true if it returns 0. Also returns true if
_obj_ and _other_ are the same object.
;T;[o;
;I"
overload;F;0;;U;0; I"==(other);T;IC;" ;T;[o;!
;I"return;F;I" ;T;0;[I"Boolean;T;"@;#[ ;$I"@return [Boolean];T;%0;"@;&F;'i ;(0;[[I"
other;T0;"@;#[ ;$I"Compares two objects based on the receiver's <=>
method, returning true if it returns 0. Also returns true if
_obj_ and _other_ are the same object.
@overload ==(other)
@return [Boolean];T;%0;"@;&F;+o;,;-T;.iJ;/iP;0@;1I"static VALUE
cmp_equal(VALUE x, VALUE y)
{
VALUE c;
if (x == y) return Qtrue;
c = rb_exec_recursive_paired_outer(cmp_eq_recursive, x, y, y);
if (NIL_P(c)) return Qfalse;
if (rb_cmpint(c, x, y) == 0) return Qtrue;
return Qfalse;
};T; I"static VALUE;T;2To;
;F;;
;;;I"Comparable#>;F;[[I"y;T0;[[@in;T;;X;0;[ ;{ ;IC;"Compares two objects based on the receiver's <=>
method, returning true if it returns a value greater than 0.
;T;[o;
;I"
overload;F;0;;X;0; I"
>(other);T;IC;" ;T;[o;!
;I"return;F;I" ;T;0;[I"Boolean;T;"@;#[ ;$I"@return [Boolean];T;%0;"@;&F;'i ;(0;[[I"
other;T0;"@;#[ ;$I"Compares two objects based on the receiver's <=>
method, returning true if it returns a value greater than 0.
@overload >(other)
@return [Boolean];T;%0;"@;&F;+o;,;-T;.if;/ik;0@;1I"istatic VALUE
cmp_gt(VALUE x, VALUE y)
{
if (cmpint(x, y) > 0) return Qtrue;
return Qfalse;
};T; I"static VALUE;T;2To;
;F;;
;;;I"Comparable#>=;F;[[I"y;T0;[[@i};T;;Y;0;[ ;{ ;IC;"Compares two objects based on the receiver's <=>
method, returning true if it returns a value greater than or equal to 0.
;T;[o;
;I"
overload;F;0;;Y;0; I">=(other);T;IC;" ;T;[o;!
;I"return;F;I" ;T;0;[I"Boolean;T;"@;#[ ;$I"@return [Boolean];T;%0;"@;&F;'i ;(0;[[I"
other;T0;"@;#[ ;$I"Compares two objects based on the receiver's <=>
method, returning true if it returns a value greater than or equal to 0.
@overload >=(other)
@return [Boolean];T;%0;"@;&F;+o;,;-T;.iu;/iz;0@;1I"jstatic VALUE
cmp_ge(VALUE x, VALUE y)
{
if (cmpint(x, y) >= 0) return Qtrue;
return Qfalse;
};T; I"static VALUE;T;2To;
;F;;
;;;I"Comparable#<;F;[[I"y;T0;[[@i;T;;Z;0;[ ;{ ;IC;"|Compares two objects based on the receiver's <=>
method, returning true if it returns a value less than 0.
;T;[o;
;I"
overload;F;0;;Z;0; I"
<(other);T;IC;" ;T;[o;!
;I"return;F;I" ;T;0;[I"Boolean;T;"@;#[ ;$I"@return [Boolean];T;%0;"@;&F;'i ;(0;[[I"
other;T0;"@;#[ ;$I"Compares two objects based on the receiver's <=>
method, returning true if it returns a value less than 0.
@overload <(other)
@return [Boolean];T;%0;"@;&F;+o;,;-T;.i;/i;0@;1I"istatic VALUE
cmp_lt(VALUE x, VALUE y)
{
if (cmpint(x, y) < 0) return Qtrue;
return Qfalse;
};T; I"static VALUE;T;2To;
;F;;
;;;I"Comparable#<=;F;[[I"y;T0;[[@i;T;;[;0;[ ;{ ;IC;"Compares two objects based on the receiver's <=>
method, returning true if it returns a value less than or equal to 0.
;T;[o;
;I"
overload;F;0;;[;0; I"<=(other);T;IC;" ;T;[o;!
;I"return;F;I" ;T;0;[I"Boolean;T;"@
;#[ ;$I"@return [Boolean];T;%0;"@
;&F;'i ;(0;[[I"
other;T0;"@
;#[ ;$I"Compares two objects based on the receiver's <=>
method, returning true if it returns a value less than or equal to 0.
@overload <=(other)
@return [Boolean];T;%0;"@
;&F;+o;,;-T;.i;/i;0@;1I"jstatic VALUE
cmp_le(VALUE x, VALUE y)
{
if (cmpint(x, y) <= 0) return Qtrue;
return Qfalse;
};T; I"static VALUE;T;2To;
;F;;
;;;I"Comparable#between?;F;[[I"min;T0[I"max;T0;[[@i;T;:
between?;0;[ ;{ ;IC;"QReturns false
if _obj_ <=>
_min_ is less
than zero or if _obj_ <=>
_max_ is greater than zero,
true
otherwise.
3.between?(1, 5) #=> true
6.between?(1, 5) #=> false
'cat'.between?('ant', 'dog') #=> true
'gnu'.between?('ant', 'dog') #=> false;T;[o;
;I"
overload;F;0;;;0; I"between?(min, max);T;IC;" ;T;[o;!
;I"return;F;I" ;T;0;[I"Boolean;T;"@
;#[ ;$I"@return [Boolean];T;%0;"@
;&F;'i ;(0;[[I"min;T0[I"max;T0;"@
;#[ ;$I"Returns false
if _obj_ <=>
_min_ is less
than zero or if _obj_ <=>
_max_ is greater than zero,
true
otherwise.
3.between?(1, 5) #=> true
6.between?(1, 5) #=> false
'cat'.between?('ant', 'dog') #=> true
'gnu'.between?('ant', 'dog') #=> false
@overload between?(min, max)
@return [Boolean];T;%0;"@
;&F;+o;,;-T;.i;/i;'i ;0@;1I"static VALUE
cmp_between(VALUE x, VALUE min, VALUE max)
{
if (cmpint(x, min) < 0) return Qfalse;
if (cmpint(x, max) > 0) return Qfalse;
return Qtrue;
};T; I"static VALUE;T;2To;
;F;;
;;;I"Comparable#clamp;F;[[@0;[[@i;T;:
clamp;0;[ ;{ ;IC;"!In (min, max)
form, returns _min_ if _obj_
<=>
_min_ is less than zero, _max_ if _obj_
<=>
_max_ is greater than zero, and _obj_
otherwise.
12.clamp(0, 100) #=> 12
523.clamp(0, 100) #=> 100
-3.123.clamp(0, 100) #=> 0
'd'.clamp('a', 'f') #=> 'd'
'z'.clamp('a', 'f') #=> 'f'
In (range)
form, returns _range.begin_ if _obj_
<=>
_range.begin_ is less than zero, _range.end_
if _obj_ <=>
_range.end_ is greater than zero, and
_obj_ otherwise.
12.clamp(0..100) #=> 12
523.clamp(0..100) #=> 100
-3.123.clamp(0..100) #=> 0
'd'.clamp('a'..'f') #=> 'd'
'z'.clamp('a'..'f') #=> 'f'
If _range.begin_ is +nil+, it is considered smaller than _obj_,
and if _range.end_ is +nil+, it is considered greater than
_obj_.
-20.clamp(0..) #=> 0
523.clamp(..100) #=> 100
When _range.end_ is excluded and not +nil+, an exception is
raised.
100.clamp(0...100) # ArgumentError
;T;[o;
;I"
overload;F;0;;;0; I"clamp(min, max);T;IC;" ;T;[o;!
;I"return;F;I" ;T;0;[I"Object;T;"@B
;#[ ;$I"@return [Object];T;%0;"@B
;&F;'i ;(0;[[I"min;T0[I"max;T0;"@B
o;
;I"
overload;F;0;;;0; I"clamp(range);T;IC;" ;T;[o;!
;I"return;F;I" ;T;0;[I"Object;T;"@B
;#[ ;$I"@return [Object];T;%0;"@B
;&F;'i ;(0;[[I"
range;T0;"@B
;#[ ;$I"zIn (min, max)
form, returns _min_ if _obj_
<=>
_min_ is less than zero, _max_ if _obj_
<=>
_max_ is greater than zero, and _obj_
otherwise.
12.clamp(0, 100) #=> 12
523.clamp(0, 100) #=> 100
-3.123.clamp(0, 100) #=> 0
'd'.clamp('a', 'f') #=> 'd'
'z'.clamp('a', 'f') #=> 'f'
In (range)
form, returns _range.begin_ if _obj_
<=>
_range.begin_ is less than zero, _range.end_
if _obj_ <=>
_range.end_ is greater than zero, and
_obj_ otherwise.
12.clamp(0..100) #=> 12
523.clamp(0..100) #=> 100
-3.123.clamp(0..100) #=> 0
'd'.clamp('a'..'f') #=> 'd'
'z'.clamp('a'..'f') #=> 'f'
If _range.begin_ is +nil+, it is considered smaller than _obj_,
and if _range.end_ is +nil+, it is considered greater than
_obj_.
-20.clamp(0..) #=> 0
523.clamp(..100) #=> 100
When _range.end_ is excluded and not +nil+, an exception is
raised.
100.clamp(0...100) # ArgumentError
@overload clamp(min, max)
@return [Object]
@overload clamp(range)
@return [Object];T;%0;"@B
;&F;+o;,;-T;.i;/i;0@;1I"nstatic VALUE
cmp_clamp(int argc, VALUE *argv, VALUE x)
{
VALUE min, max;
int c, excl = 0;
if (rb_scan_args(argc, argv, "11", &min, &max) == 1) {
VALUE range = min;
if (!rb_range_values(range, &min, &max, &excl)) {
rb_raise(rb_eTypeError, "wrong argument type %s (expected Range)",
rb_builtin_class_name(range));
}
if (!NIL_P(max)) {
if (excl) rb_raise(rb_eArgError, "cannot clamp with an exclusive range");
}
}
if (!NIL_P(min) && !NIL_P(max) && cmpint(min, max) > 0) {
rb_raise(rb_eArgError, "min argument must be smaller than max argument");
}
if (!NIL_P(min)) {
c = cmpint(x, min);
if (c == 0) return x;
if (c < 0) return min;
}
if (!NIL_P(max)) {
c = cmpint(x, max);
if (c > 0) return max;
}
return x;
};T; I"static VALUE;T;2T;o@;pIC;[ ;o@;qIC;[ ;o@;rIC;s{;tIC;s{ ;uT;
IC;s{ ;uT;uT;v{ ;w[ ;[[@i);F;;;;H;;;[ ;{ ;IC;"The Comparable mixin is used by classes whose objects may be
ordered. The class must define the <=>
operator,
which compares the receiver against another object, returning a
value less than 0, returning 0, or returning a value greater than 0,
depending on whether the receiver is less than, equal to,
or greater than the other object. If the other object is not
comparable then the <=>
operator should return +nil+.
Comparable uses <=>
to implement the conventional
comparison operators (<
, <=
,
==
, >=
, and >
) and the
method between?
.
class SizeMatters
include Comparable
attr :str
def <=>(other)
str.size <=> other.str.size
end
def initialize(str)
@str = str
end
def inspect
@str
end
end
s1 = SizeMatters.new("Z")
s2 = SizeMatters.new("YY")
s3 = SizeMatters.new("XXX")
s4 = SizeMatters.new("WWWW")
s5 = SizeMatters.new("VVVVV")
s1 < s2 #=> true
s4.between?(s1, s3) #=> false
s4.between?(s3, s5) #=> true
[ s3, s2, s5, s4, s1 ].sort #=> [Z, YY, XXX, WWWW, VVVVV]
;T;[ ;#[ ;$I"The Comparable mixin is used by classes whose objects may be
ordered. The class must define the <=>
operator,
which compares the receiver against another object, returning a
value less than 0, returning 0, or returning a value greater than 0,
depending on whether the receiver is less than, equal to,
or greater than the other object. If the other object is not
comparable then the <=>
operator should return +nil+.
Comparable uses <=>
to implement the conventional
comparison operators (<
, <=
,
==
, >=
, and >
) and the
method between?
.
class SizeMatters
include Comparable
attr :str
def <=>(other)
str.size <=> other.str.size
end
def initialize(str)
@str = str
end
def inspect
@str
end
end
s1 = SizeMatters.new("Z")
s2 = SizeMatters.new("YY")
s3 = SizeMatters.new("XXX")
s4 = SizeMatters.new("WWWW")
s5 = SizeMatters.new("VVVVV")
s1 < s2 #=> true
s4.between?(s1, s3) #=> false
s4.between?(s3, s5) #=> true
[ s3, s2, s5, s4, s1 ].sort #=> [Z, YY, XXX, WWWW, VVVVV]
;T;%0;"@;&F;+o;,;-T;.i;/i#;0@;I"Comparable;F:
@type0;o@R;rIC;s{;tIC;s{ ;uT;
IC;s{ ;uT;uT;v{ ;w[ ;[[@i [@iu;T;:Numeric;;H;;;[ ;{ ;IC;"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
Integer are implemented as immediates, which means that each Integer is a single immutable
object which is always passed by value.
a = 1
1.object_id == a.object_id #=> true
There can only ever be one instance of the integer +1+, for example. Ruby ensures this
by preventing instantiation. If duplication is attempted, the same instance is returned.
Integer.new(1) #=> NoMethodError: undefined method `new' for Integer:Class
1.dup #=> 1
1.object_id == 1.dup.object_id #=> true
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
Array containing an object that has been coerced into an instance of the new class
and +self+ (see #coerce).
Inheriting classes should also implement arithmetic operator methods (+
,
-
, *
and /
) and the <=>
operator (see
Comparable). These methods may rely on +coerce+ to ensure interoperability with
instances of other numeric classes.
class Tally < Numeric
def initialize(string)
@string = string
end
def to_s
@string
end
def to_i
@string.size
end
def coerce(other)
[self.class.new('|' * other.to_i), self]
end
def <=>(other)
to_i <=> other.to_i
end
def +(other)
self.class.new('|' * (to_i + other.to_i))
end
def -(other)
self.class.new('|' * (to_i - other.to_i))
end
def *(other)
self.class.new('|' * (to_i * other.to_i))
end
def /(other)
self.class.new('|' * (to_i / other.to_i))
end
end
tally = Tally.new('||')
puts tally * 2 #=> "||||"
puts tally > 1 #=> true;T;[ ;#[ ;$I"
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
Integer are implemented as immediates, which means that each Integer is a single immutable
object which is always passed by value.
a = 1
1.object_id == a.object_id #=> true
There can only ever be one instance of the integer +1+, for example. Ruby ensures this
by preventing instantiation. If duplication is attempted, the same instance is returned.
Integer.new(1) #=> NoMethodError: undefined method `new' for Integer:Class
1.dup #=> 1
1.object_id == 1.dup.object_id #=> true
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
Array containing an object that has been coerced into an instance of the new class
and +self+ (see #coerce).
Inheriting classes should also implement arithmetic operator methods (+
,
-
, *
and /
) and the <=>
operator (see
Comparable). These methods may rely on +coerce+ to ensure interoperability with
instances of other numeric classes.
class Tally < Numeric
def initialize(string)
@string = string
end
def to_s
@string
end
def to_i
@string.size
end
def coerce(other)
[self.class.new('|' * other.to_i), self]
end
def <=>(other)
to_i <=> other.to_i
end
def +(other)
self.class.new('|' * (to_i + other.to_i))
end
def -(other)
self.class.new('|' * (to_i - other.to_i))
end
def *(other)
self.class.new('|' * (to_i * other.to_i))
end
def /(other)
self.class.new('|' * (to_i / other.to_i))
end
end
tally = Tally.new('||')
puts tally * 2 #=> "||||"
puts tally > 1 #=> true
;T;%0;"@R;&F;+o;,;-T;.i ;/ie;'i ;0@;I"Numeric;F;yo;;0;0;0;:Object;0@;o; ;IC;[;o;
;F;;
;;;I"Object#rb_fatal;F;[[I"msg;T0;[[I" ext/-test-/fatal/rb_fatal.c;Ti;T;:
rb_fatal;0;[ ;{ ;IC;" ;T;[ ;#[ ;$@z;%0;"@
;0@
;1I"static VALUE
ruby_fatal(VALUE obj, VALUE msg)
{
const char *cmsg = NULL;
(void)obj;
cmsg = RSTRING_PTR(msg);
rb_fatal("%s", cmsg);
return 0; /* never reached */
};T; I"static VALUE;T;2To;7;[[I"7ext/-test-/wait_for_single_fd/wait_for_single_fd.c;Ti[;F;:RB_WAITFD_IN;;9;;;[ ;{ ;IC;" ;T;[ ;#[ ;$@z;%0;"@
;0@
;I"Object::RB_WAITFD_IN;F;:I"INT2NUM(RB_WAITFD_IN);To;7;[[@
i\;F;:RB_WAITFD_OUT;;9;;;[ ;{ ;IC;" ;T;[ ;#[ ;$@z;%0;"@
;0@
;I"Object::RB_WAITFD_OUT;F;:I"INT2NUM(RB_WAITFD_OUT);To;7;[[@
i];F;:RB_WAITFD_PRI;;9;;;[ ;{ ;IC;" ;T;[ ;#[ ;$@z;%0;"@
;0@
;I"Object::RB_WAITFD_PRI;F;:I"INT2NUM(RB_WAITFD_PRI);To;7;[[I"
bignum.c;Ti
;F;:Bignum;;9;;;[ ;{ ;IC;"#An obsolete class, use Integer
;T;[ ;#[ ;$I"#An obsolete class, use Integer;T;%0;"@
;&F;+o;,;-T;.i ;/i ;0o;;0;0;0;;;0@;@
;0;I"Object::Bignum;T;:I"rb_cInteger;To;
;F;;
;;;I"Object#nil?;F;[ ;[[I"
object.c;Ti#;T;: nil?;0;[ ;{ ;IC;"Only the object nil responds true
to nil?
.
Object.new.nil? #=> false
nil.nil? #=> true;T;[o;
;I"
overload;F;0;;;0; I" nil?;T;IC;" ;T;[o;!
;I"return;F;I" ;T;0;[I"Boolean;T;"@
;#[ ;$I"@return [Boolean];T;%0;"@
;&F;'i ;(0;[ ;"@
;#[ ;$I"Only the object nil responds true
to nil?
.
Object.new.nil? #=> false
nil.nil? #=> true
@overload nil?
@return [Boolean];T;%0;"@
;&F;+o;,;-T;.i;/i;'i ;0@
;1I"HMJIT_FUNC_EXPORTED VALUE
rb_false(VALUE obj)
{
return Qfalse;
};T; I"MJIT_FUNC_EXPORTED VALUE;T;2To;
;F;;
;;;I"Object#===;F;[ ;[ ;F;;V;;H;[ ;{ ;IC;" ;T;[ ;#[ ;$@z;%0;"@
;0@
;2To;
;F;;
;;;I"Object#=~;F;[[I" obj2;T0;[[@
i4;T;:=~;0;[ ;{ ;IC;"pThis method is deprecated.
This is not only useless but also troublesome because it may hide a
type error.
;T;[o;
;I"
overload;F;0;;;0; I"=~(other);T;IC;" ;T;[o;!
;I"return;F;I" ;T;0;[I"nil;T;"@
;#[ ;$I"@return [nil];T;%0;"@
;&F;'i ;(0;[[I"
other;T0;"@
;#[ ;$I"This method is deprecated.
This is not only useless but also troublesome because it may hide a
type error.
@overload =~(other)
@return [nil];T;%0;"@
;&F;+o;,;-T;.i*;/i1;0@
;1I"7static VALUE
rb_obj_match(VALUE obj1, VALUE obj2)
{
if (rb_warning_category_enabled_p(RB_WARN_CATEGORY_DEPRECATED)) {
rb_category_warn(RB_WARN_CATEGORY_DEPRECATED, "deprecated Object#=~ is called on %"PRIsVALUE
"; it always returns nil", rb_obj_class(obj1));
}
return Qnil;
};T; I"static VALUE;T;2To;
;F;;
;;;I"Object#!~;F;[[I" obj2;T0;[[@
iF;T;:!~;0;[ ;{ ;IC;"\Returns true if two objects do not match (using the =~
method), otherwise false.
;T;[o;
;I"
overload;F;0;;;0; I"!~(other);T;IC;" ;T;[o;!
;I"return;F;I" ;T;0;[I"Boolean;T;"@;#[ ;$I"@return [Boolean];T;%0;"@;&F;'i ;(0;[[I"
other;T0;"@;#[ ;$I"Returns true if two objects do not match (using the =~
method), otherwise false.
@overload !~(other)
@return [Boolean];T;%0;"@;&F;+o;,;-T;.i>;/iC;0@
;1I"static VALUE
rb_obj_not_match(VALUE obj1, VALUE obj2)
{
VALUE result = rb_funcall(obj1, id_match, 1, obj2);
return RTEST(result) ? Qfalse : Qtrue;
};T; I"static VALUE;T;2To;
;F;;
;;;I"Object#eql?;F;[[I" obj2;T0;[[@
i;T;;\;0;[ ;{ ;IC;"Equality --- At the Object level, #== returns true
only if +obj+ and +other+ are the same object. Typically, this
method is overridden in descendant classes to provide
class-specific meaning.
Unlike #==, the #equal? method should never be overridden by
subclasses as it is used to determine object identity (that is,
a.equal?(b)
if and only if a
is the same
object as b
):
obj = "a"
other = obj.dup
obj == other #=> true
obj.equal? other #=> false
obj.equal? obj #=> true
The #eql? method returns true
if +obj+ and +other+
refer to the same hash key. This is used by Hash to test members
for equality. For any pair of objects where #eql? returns +true+,
the #hash value of both objects must be equal. So any subclass
that overrides #eql? should also override #hash appropriately.
For objects of class Object, #eql? is synonymous
with #==. Subclasses normally continue this tradition by aliasing
#eql? to their overridden #== method, but there are exceptions.
Numeric types, for example, perform type conversion across #==,
but not across #eql?, so:
1 == 1.0 #=> true
1.eql? 1.0 #=> false;T;[o;
;I"
overload;F;0;;U;0; I"==(other);T;IC;" ;T;[o;!
;I"return;F;I" ;T;0;[I"Boolean;T;"@4;#[ ;$I"@return [Boolean];T;%0;"@4;&F;'i ;(0;[[I"
other;T0;"@4o;
;I"
overload;F;0;:equal?;0; I"equal?(other);T;IC;" ;T;[o;!
;I"return;F;I" ;T;0;[I"Boolean;T;"@4;#[ ;$I"@return [Boolean];T;%0;"@4;&F;'i ;(0;[[I"
other;T0;"@4o;
;I"
overload;F;0;;\;0; I"eql?(other);T;IC;" ;T;[o;!
;I"return;F;I" ;T;0;[I"Boolean;T;"@4;#[ ;$I"@return [Boolean];T;%0;"@4;&F;'i ;(0;[[I"
other;T0;"@4;#[ ;$I"*Equality --- At the Object level, #== returns true
only if +obj+ and +other+ are the same object. Typically, this
method is overridden in descendant classes to provide
class-specific meaning.
Unlike #==, the #equal? method should never be overridden by
subclasses as it is used to determine object identity (that is,
a.equal?(b)
if and only if a
is the same
object as b
):
obj = "a"
other = obj.dup
obj == other #=> true
obj.equal? other #=> false
obj.equal? obj #=> true
The #eql? method returns true
if +obj+ and +other+
refer to the same hash key. This is used by Hash to test members
for equality. For any pair of objects where #eql? returns +true+,
the #hash value of both objects must be equal. So any subclass
that overrides #eql? should also override #hash appropriately.
For objects of class Object, #eql? is synonymous
with #==. Subclasses normally continue this tradition by aliasing
#eql? to their overridden #== method, but there are exceptions.
Numeric types, for example, perform type conversion across #==,
but not across #eql?, so:
1 == 1.0 #=> true
1.eql? 1.0 #=> false
@overload ==(other)
@return [Boolean]
@overload equal?(other)
@return [Boolean]
@overload eql?(other)
@return [Boolean];T;%0;"@4;&F;+o;,;-T;.i;/i;'i ;0@
;1I"}MJIT_FUNC_EXPORTED VALUE
rb_obj_equal(VALUE obj1, VALUE obj2)
{
if (obj1 == obj2) return Qtrue;
return Qfalse;
};T; I"MJIT_FUNC_EXPORTED VALUE;T;2To;
;F;;
;;;I"Object#hash;F;[ ;[[I"hash.c;TiH;T;;];0;[ ;{ ;IC;"Generates an Integer hash value for this object. This function must have the
property that a.eql?(b)
implies a.hash == b.hash
.
The hash value is used along with #eql? by the Hash class to determine if
two objects reference the same hash key. Any hash value that exceeds the
capacity of an Integer will be truncated before being used.
The hash value for an object may not be identical across invocations or
implementations of Ruby. If you need a stable identifier across Ruby
invocations and implementations you will need to generate one with a custom
method.
Certain core classes such as Integer use built-in hash calculations and
do not call the #hash method when used as a hash key.
;T;[o;
;I"
overload;F;0;;];0; I" hash;T;IC;" ;T;[o;!
;I"return;F;I" ;T;0;[I"Integer;T;"@q;#[ ;$I"@return [Integer];T;%0;"@q;&F;'i ;(0;[ ;"@q;#[ ;$I"Generates an Integer hash value for this object. This function must have the
property that a.eql?(b)
implies a.hash == b.hash
.
The hash value is used along with #eql? by the Hash class to determine if
two objects reference the same hash key. Any hash value that exceeds the
capacity of an Integer will be truncated before being used.
The hash value for an object may not be identical across invocations or
implementations of Ruby. If you need a stable identifier across Ruby
invocations and implementations you will need to generate one with a custom
method.
Certain core classes such as Integer use built-in hash calculations and
do not call the #hash method when used as a hash key.
@overload hash
@return [Integer];T;%0;"@q;&F;+o;,;-T;.i2;/iD;0@
;1I"iVALUE
rb_obj_hash(VALUE obj)
{
long hnum = any_hash(obj, objid_hash);
return ST2FIX(hnum);
};T; I"
VALUE;T;2To;
;F;;
;;;I"Object#<=>;F;[[I" obj2;T0;[[@
i`;T;;W;0;[ ;{ ;IC;"%Returns 0 if +obj+ and +other+ are the same object
or obj == other
, otherwise nil.
The #<=> is used by various methods to compare objects, for example
Enumerable#sort, Enumerable#max etc.
Your implementation of #<=> should return one of the following values: -1, 0,
1 or nil. -1 means self is smaller than other. 0 means self is equal to other.
1 means self is bigger than other. Nil means the two values could not be
compared.
When you define #<=>, you can include Comparable to gain the
methods #<=, #<, #==, #>=, #> and #between?.
;T;[o;
;I"
overload;F;0;;W;0; I"<=>(other);T;IC;" ;T;[o;!
;I"return;F;I" ;T;0;[I"0;TI"nil;T;"@;#[ ;$I"@return [0, nil];T;%0;"@;&F;'i ;(0;[[I"
other;T0;"@;#[ ;$I"OReturns 0 if +obj+ and +other+ are the same object
or obj == other
, otherwise nil.
The #<=> is used by various methods to compare objects, for example
Enumerable#sort, Enumerable#max etc.
Your implementation of #<=> should return one of the following values: -1, 0,
1 or nil. -1 means self is smaller than other. 0 means self is equal to other.
1 means self is bigger than other. Nil means the two values could not be
compared.
When you define #<=>, you can include Comparable to gain the
methods #<=, #<, #==, #>=, #> and #between?.
@overload <=>(other)
@return [0, nil];T;%0;"@;&F;+o;,;-T;.iN;/i^;0@
;1I"{static VALUE
rb_obj_cmp(VALUE obj1, VALUE obj2)
{
if (rb_equal(obj1, obj2))
return INT2FIX(0);
return Qnil;
};T; I"static VALUE;T;2To;
;F;;
;;;I"Object#singleton_class;F;[ ;[[@
i=;T;:singleton_class;0;[ ;{ ;IC;"Returns the singleton class of obj. This method creates
a new singleton class if obj does not have one.
If obj is nil
, true
, or
false
, it returns NilClass, TrueClass, or FalseClass,
respectively.
If obj is an Integer, a Float or a Symbol, it raises a TypeError.
Object.new.singleton_class #=> #>
String.singleton_class #=> #
nil.singleton_class #=> NilClass
;T;[o;
;I"
overload;F;0;;;0; I"singleton_class;T;IC;" ;T;[o;!
;I"return;F;I" ;T;0;[I"
Class;T;"@;#[ ;$I"@return [Class];T;%0;"@;&F;'i ;(0;[ ;"@;#[ ;$I"Returns the singleton class of obj. This method creates
a new singleton class if obj does not have one.
If obj is nil
, true
, or
false
, it returns NilClass, TrueClass, or FalseClass,
respectively.
If obj is an Integer, a Float or a Symbol, it raises a TypeError.
Object.new.singleton_class #=> #>
String.singleton_class #=> #
nil.singleton_class #=> NilClass
@overload singleton_class
@return [Class];T;%0;"@;&F;+o;,;-T;.i,;/i:;0@
;1I"[static VALUE
rb_obj_singleton_class(VALUE obj)
{
return rb_singleton_class(obj);
};T; I"static VALUE;T;2To;
;F;;
;;;I"Object#dup;F;[ ;[[@
i.;T;;|;0;[ ;{ ;IC;"Produces a shallow copy of obj---the instance variables of
obj are copied, but not the objects they reference.
This method may have class-specific behavior. If so, that
behavior will be documented under the #+initialize_copy+ method of
the class.
=== on dup vs clone
In general, #clone and #dup may have different semantics in
descendant classes. While #clone is used to duplicate an object,
including its internal state, #dup typically uses the class of the
descendant object to create the new instance.
When using #dup, any modules that the object has been extended with will not
be copied.
class Klass
attr_accessor :str
end
module Foo
def foo; 'foo'; end
end
s1 = Klass.new #=> #
s1.extend(Foo) #=> #
s1.foo #=> "foo"
s2 = s1.clone #=> #
s2.foo #=> "foo"
s3 = s1.dup #=> #
s3.foo #=> NoMethodError: undefined method `foo' for #
;T;[o;
;I"
overload;F;0;;|;0; I"dup;T;IC;" ;T;[o;!
;I"return;F;I" ;T;0;[I"Object;T;"@;#[ ;$I"@return [Object];T;%0;"@;&F;'i ;(0;[ ;"@;#[ ;$I"Produces a shallow copy of obj---the instance variables of
obj are copied, but not the objects they reference.
This method may have class-specific behavior. If so, that
behavior will be documented under the #+initialize_copy+ method of
the class.
=== on dup vs clone
In general, #clone and #dup may have different semantics in
descendant classes. While #clone is used to duplicate an object,
including its internal state, #dup typically uses the class of the
descendant object to create the new instance.
When using #dup, any modules that the object has been extended with will not
be copied.
class Klass
attr_accessor :str
end
module Foo
def foo; 'foo'; end
end
s1 = Klass.new #=> #
s1.extend(Foo) #=> #
s1.foo #=> "foo"
s2 = s1.clone #=> #
s2.foo #=> "foo"
s3 = s1.dup #=> #
s3.foo #=> NoMethodError: undefined method `foo' for #
@overload dup
@return [Object];T;%0;"@;&F;+o;,;-T;.i;/i*;0@
;1I"VALUE
rb_obj_dup(VALUE obj)
{
VALUE dup;
if (special_object_p(obj)) {
return obj;
}
dup = rb_obj_alloc(rb_obj_class(obj));
init_copy(dup, obj);
rb_funcall(dup, id_init_dup, 1, obj);
return dup;
};T; I"
VALUE;T;2To;
;F;;
;;;I"Object#itself;F;[ ;[[@
iH;T;:itself;0;[ ;{ ;IC;"mReturns the receiver.
string = "my string"
string.itself.object_id == string.object_id #=> true
;T;[o;
;I"
overload;F;0;;;0; I"itself;T;IC;" ;T;[o;!
;I"return;F;I" ;T;0;[I"Object;T;"@;#[ ;$I"@return [Object];T;%0;"@;&F;'i ;(0;[ ;"@;#[ ;$I"Returns the receiver.
string = "my string"
string.itself.object_id == string.object_id #=> true
@overload itself
@return [Object];T;%0;"@;&F;+o;,;-T;.i=;/iE;0@
;1I">static VALUE
rb_obj_itself(VALUE obj)
{
return obj;
};T; I"static VALUE;T;2To;
;F;;
;;;I"Object#initialize_copy;F;[[I" orig;T0;[[@
ib;T;:initialize_copy;0;[ ;{ ;IC;":nodoc:
;T;[ ;#[ ;$I":nodoc:
;T;%0;"@;&F;+o;,;-T;.iZ;/i\;0@
;1I"VALUE
rb_obj_init_copy(VALUE obj, VALUE orig)
{
if (obj == orig) return obj;
rb_check_frozen(obj);
if (TYPE(obj) != TYPE(orig) || rb_obj_class(obj) != rb_obj_class(orig)) {
rb_raise(rb_eTypeError, "initialize_copy should take same class object");
}
return obj;
};T; I"
VALUE;T;2To;
;F;;
;;;I"Object#initialize_dup;F;[[I" orig;T0;[[@
iv;T;:initialize_dup;0;[ ;{ ;IC;"!
:nodoc:
;T;[ ;#[ ;$I"!
:nodoc:
;T;%0;"@;&F;+o;,;-T;.im;/ip;0@
;1I"wVALUE
rb_obj_init_dup_clone(VALUE obj, VALUE orig)
{
rb_funcall(obj, id_init_copy, 1, orig);
return obj;
};T; I"
VALUE;T;2To;
;F;;
;;;I"Object#initialize_clone;F;[[@0;[[@
i;T;:initialize_clone;0;[ ;{ ;IC;"!
:nodoc:
;T;[ ;#[ ;$I"!
:nodoc:
;T;%0;"@;&F;+o;,;-T;.i};/i;0@
;1I"static VALUE
rb_obj_init_clone(int argc, VALUE *argv, VALUE obj)
{
VALUE orig, opts;
rb_scan_args(argc, argv, "1:", &orig, &opts);
/* Ignore a freeze keyword */
if (argc == 2) (void)freeze_opt(1, &opts);
rb_funcall(obj, id_init_copy, 1, orig);
return obj;
};T; I"static VALUE;T;2To;
;F;;
;;;I"Object#taint;F;[ ;[[@
i;T;:
taint;0;[ ;{ ;IC;"OReturns object. This method is deprecated and will be removed in Ruby 3.2.
;T;[o;
;I"
overload;F;0;;;0; I"
taint;T;IC;" ;T;[o;!
;I"return;F;I" ;T;0;[I"Object;T;"@-;#[ ;$I"@return [Object];T;%0;"@-;&F;'i ;(0;[ ;"@-;#[ ;$I"tReturns object. This method is deprecated and will be removed in Ruby 3.2.
@overload taint
@return [Object];T;%0;"@-;&F;+o;,;-T;.i;/i;0@
;1I"oVALUE
rb_obj_taint(VALUE obj)
{
rb_warn_deprecated_to_remove("Object#taint", "3.2");
return obj;
};T; I"
VALUE;T;2To;
;F;;
;;;I"Object#tainted?;F;[ ;[[@
i;T;:
tainted?;0;[ ;{ ;IC;"OReturns false. This method is deprecated and will be removed in Ruby 3.2.;T;[o;
;I"
overload;F;0;;;0; I"
tainted?;T;IC;" ;T;[o;!
;I"return;F;I" ;T;0;[I"
false;T;"@H;#[ ;$I"@return [false];T;%0;"@H;&F;'i ;(0;[ ;"@H;#[ ;$I"vReturns false. This method is deprecated and will be removed in Ruby 3.2.
@overload tainted?
@return [false];T;%0;"@H;&F;+o;,;-T;.i;/i;'i ;0@
;1I"wVALUE
rb_obj_tainted(VALUE obj)
{
rb_warn_deprecated_to_remove("Object#tainted?", "3.2");
return Qfalse;
};T; I"
VALUE;T;2To;
;F;;
;;;I"Object#untaint;F;[ ;[[@
i;T;:untaint;0;[ ;{ ;IC;"OReturns object. This method is deprecated and will be removed in Ruby 3.2.
;T;[o;
;I"
overload;F;0;;;0; I"untaint;T;IC;" ;T;[o;!
;I"return;F;I" ;T;0;[I"Object;T;"@c;#[ ;$I"@return [Object];T;%0;"@c;&F;'i ;(0;[ ;"@c;#[ ;$I"vReturns object. This method is deprecated and will be removed in Ruby 3.2.
@overload untaint
@return [Object];T;%0;"@c;&F;+o;,;-T;.i;/i;0@
;1I"sVALUE
rb_obj_untaint(VALUE obj)
{
rb_warn_deprecated_to_remove("Object#untaint", "3.2");
return obj;
};T; I"
VALUE;T;2To;
;F;;
;;;I"Object#untrust;F;[ ;[[@
i;T;:untrust;0;[ ;{ ;IC;"OReturns object. This method is deprecated and will be removed in Ruby 3.2.
;T;[o;
;I"
overload;F;0;;;0; I"untrust;T;IC;" ;T;[o;!
;I"return;F;I" ;T;0;[I"Object;T;"@~;#[ ;$I"@return [Object];T;%0;"@~;&F;'i ;(0;[ ;"@~;#[ ;$I"vReturns object. This method is deprecated and will be removed in Ruby 3.2.
@overload untrust
@return [Object];T;%0;"@~;&F;+o;,;-T;.i;/i;0@
;1I"sVALUE
rb_obj_untrust(VALUE obj)
{
rb_warn_deprecated_to_remove("Object#untrust", "3.2");
return obj;
};T; I"
VALUE;T;2To;
;F;;
;;;I"Object#untrusted?;F;[ ;[[@
i;T;:untrusted?;0;[ ;{ ;IC;"OReturns false. This method is deprecated and will be removed in Ruby 3.2.;T;[o;
;I"
overload;F;0;;;0; I"untrusted?;T;IC;" ;T;[o;!
;I"return;F;I" ;T;0;[I"
false;T;"@;#[ ;$I"@return [false];T;%0;"@;&F;'i ;(0;[ ;"@;#[ ;$I"xReturns false. This method is deprecated and will be removed in Ruby 3.2.
@overload untrusted?
@return [false];T;%0;"@;&F;+o;,;-T;.i;/i;'i ;0@
;1I"{VALUE
rb_obj_untrusted(VALUE obj)
{
rb_warn_deprecated_to_remove("Object#untrusted?", "3.2");
return Qfalse;
};T; I"
VALUE;T;2To;
;F;;
;;;I"Object#trust;F;[ ;[[@
i;T;:
trust;0;[ ;{ ;IC;"OReturns object. This method is deprecated and will be removed in Ruby 3.2.
;T;[o;
;I"
overload;F;0;;;0; I"
trust;T;IC;" ;T;[o;!
;I"return;F;I" ;T;0;[I"Object;T;"@;#[ ;$I"@return [Object];T;%0;"@;&F;'i ;(0;[ ;"@;#[ ;$I"tReturns object. This method is deprecated and will be removed in Ruby 3.2.
@overload trust
@return [Object];T;%0;"@;&F;+o;,;-T;.i;/i;0@
;1I"oVALUE
rb_obj_trust(VALUE obj)
{
rb_warn_deprecated_to_remove("Object#trust", "3.2");
return obj;
};T; I"
VALUE;T;2To;
;F;;
;;;I"Object#freeze;F;[ ;[[@
i;T;:freeze;0;[ ;{ ;IC;"Prevents further modifications to obj. A
RuntimeError will be raised if modification is attempted.
There is no way to unfreeze a frozen object. See also
Object#frozen?.
This method returns self.
a = [ "a", "b", "c" ]
a.freeze
a << "z"
produces:
prog.rb:3:in `<<': can't modify frozen Array (FrozenError)
from prog.rb:3
Objects of the following classes are always frozen: Integer,
Float, Symbol.
;T;[o;
;I"
overload;F;0;;;0; I"freeze;T;IC;" ;T;[o;!
;I"return;F;I" ;T;0;[I"Object;T;"@;#[ ;$I"@return [Object];T;%0;"@;&F;'i ;(0;[ ;"@;#[ ;$I"Prevents further modifications to obj. A
RuntimeError will be raised if modification is attempted.
There is no way to unfreeze a frozen object. See also
Object#frozen?.
This method returns self.
a = [ "a", "b", "c" ]
a.freeze
a << "z"
produces:
prog.rb:3:in `<<': can't modify frozen Array (FrozenError)
from prog.rb:3
Objects of the following classes are always frozen: Integer,
Float, Symbol.
@overload freeze
@return [Object];T;%0;"@;&F;+o;,;-T;.i;/i;0@
;1I"VALUE
rb_obj_freeze(VALUE obj)
{
if (!OBJ_FROZEN(obj)) {
OBJ_FREEZE(obj);
if (SPECIAL_CONST_P(obj)) {
rb_bug("special consts should be frozen.");
}
}
return obj;
};T; I"
VALUE;T;2To;
;F;;
;;;I"Object#to_s;F;[ ;[[@
i;T;;F;0;[ ;{ ;IC;"Returns a string representing obj. The default #to_s prints
the object's class and an encoding of the object id. As a special
case, the top-level object that is the initial execution context
of Ruby programs returns ``main''.
;T;[o;
;I"
overload;F;0;;F;0; I" to_s;T;IC;" ;T;[o;!
;I"return;F;I" ;T;0;[I"String;T;"@;#[ ;$I"@return [String];T;%0;"@;&F;'i ;(0;[ ;"@;#[ ;$I"Returns a string representing obj. The default #to_s prints
the object's class and an encoding of the object id. As a special
case, the top-level object that is the initial execution context
of Ruby programs returns ``main''.
@overload to_s
@return [String];T;%0;"@;&F;+o;,;-T;.i;/i;0@
;1I"VALUE
rb_any_to_s(VALUE obj)
{
VALUE str;
VALUE cname = rb_class_name(CLASS_OF(obj));
str = rb_sprintf("#<%"PRIsVALUE":%p>", cname, (void*)obj);
return str;
};T; I"
VALUE;T;2To;
;F;;
;;;I"Object#inspect;F;[ ;[[@
i ;T;;G;0;[ ;{ ;IC;"7Returns a string containing a human-readable representation of obj.
The default #inspect shows the object's class name, an encoding of
its memory address, and a list of the instance variables and their
values (by calling #inspect on each of them). User defined classes
should override this method to provide a better representation of
obj. When overriding this method, it should return a string
whose encoding is compatible with the default external encoding.
[ 1, 2, 3..4, 'five' ].inspect #=> "[1, 2, 3..4, \"five\"]"
Time.new.inspect #=> "2008-03-08 19:43:39 +0900"
class Foo
end
Foo.new.inspect #=> "#"
class Bar
def initialize
@bar = 1
end
end
Bar.new.inspect #=> "#"
;T;[o;
;I"
overload;F;0;;G;0; I"inspect;T;IC;" ;T;[o;!
;I"return;F;I" ;T;0;[I"String;T;"@;#[ ;$I"@return [String];T;%0;"@;&F;'i ;(0;[ ;"@;#[ ;$I"^Returns a string containing a human-readable representation of obj.
The default #inspect shows the object's class name, an encoding of
its memory address, and a list of the instance variables and their
values (by calling #inspect on each of them). User defined classes
should override this method to provide a better representation of
obj. When overriding this method, it should return a string
whose encoding is compatible with the default external encoding.
[ 1, 2, 3..4, 'five' ].inspect #=> "[1, 2, 3..4, \"five\"]"
Time.new.inspect #=> "2008-03-08 19:43:39 +0900"
class Foo
end
Foo.new.inspect #=> "#"
class Bar
def initialize
@bar = 1
end
end
Bar.new.inspect #=> "#"
@overload inspect
@return [String];T;%0;"@;&F;+o;,;-T;.i;/i;0@
;1I"static VALUE
rb_obj_inspect(VALUE obj)
{
if (rb_ivar_count(obj) > 0) {
VALUE str;
VALUE c = rb_class_name(CLASS_OF(obj));
str = rb_sprintf("-<%"PRIsVALUE":%p", c, (void*)obj);
return rb_exec_recursive(inspect_obj, obj, str);
}
else {
return rb_any_to_s(obj);
}
};T; I"static VALUE;T;2To;
;F;;
;;;I"Object#methods;F;[[@0;[[I"class.c;Ti
;T;:methods;0;[ ;{ ;IC;",Returns a list of the names of public and protected methods of
obj. This will include all the methods accessible in
obj's ancestors.
If the optional parameter is false
, it
returns an array of obj's public and protected singleton methods,
the array will not include methods in modules included in obj.
class Klass
def klass_method()
end
end
k = Klass.new
k.methods[0..9] #=> [:klass_method, :nil?, :===,
# :==~, :!, :eql?
# :hash, :<=>, :class, :singleton_class]
k.methods.length #=> 56
k.methods(false) #=> []
def k.singleton_method; end
k.methods(false) #=> [:singleton_method]
module M123; def m123; end end
k.extend M123
k.methods(false) #=> [:singleton_method]
;T;[o;
;I"
overload;F;0;;;0; I"methods(regular=true);T;IC;" ;T;[o;!
;I"return;F;I" ;T;0;[I"
Array;T;"@ ;#[ ;$I"@return [Array];T;%0;"@ ;&F;'i ;(0;[[I"regular;TI" true;T;"@ ;#[ ;$I"`Returns a list of the names of public and protected methods of
obj. This will include all the methods accessible in
obj's ancestors.
If the optional parameter is false
, it
returns an array of obj's public and protected singleton methods,
the array will not include methods in modules included in obj.
class Klass
def klass_method()
end
end
k = Klass.new
k.methods[0..9] #=> [:klass_method, :nil?, :===,
# :==~, :!, :eql?
# :hash, :<=>, :class, :singleton_class]
k.methods.length #=> 56
k.methods(false) #=> []
def k.singleton_method; end
k.methods(false) #=> [:singleton_method]
module M123; def m123; end end
k.extend M123
k.methods(false) #=> [:singleton_method]
@overload methods(regular=true)
@return [Array];T;%0;"@ ;&F;+o;,;-T;.i;/i
;0@
;1I"VALUE
rb_obj_methods(int argc, const VALUE *argv, VALUE obj)
{
rb_check_arity(argc, 0, 1);
if (argc > 0 && !RTEST(argv[0])) {
return rb_obj_singleton_methods(argc, argv, obj);
}
return class_instance_method_list(argc, argv, CLASS_OF(obj), 1, ins_methods_i);
};T; I"
VALUE;T;2To;
;F;;
;;;I"Object#singleton_methods;F;[[@0;[[@&ie;T;:singleton_methods;0;[ ;{ ;IC;"JReturns an array of the names of singleton methods for obj.
If the optional all parameter is true, the list will include
methods in modules included in obj.
Only public and protected singleton methods are returned.
module Other
def three() end
end
class Single
def Single.four() end
end
a = Single.new
def a.one()
end
class << a
include Other
def two()
end
end
Single.singleton_methods #=> [:four]
a.singleton_methods(false) #=> [:two, :one]
a.singleton_methods #=> [:two, :one, :three]
;T;[o;
;I"
overload;F;0;;;0; I" singleton_methods(all=true);T;IC;" ;T;[o;!
;I"return;F;I" ;T;0;[I"
Array;T;"@@;#[ ;$I"@return [Array];T;%0;"@@;&F;'i ;(0;[[I"all;TI" true;T;"@@;#[ ;$I"Returns an array of the names of singleton methods for obj.
If the optional all parameter is true, the list will include
methods in modules included in obj.
Only public and protected singleton methods are returned.
module Other
def three() end
end
class Single
def Single.four() end
end
a = Single.new
def a.one()
end
class << a
include Other
def two()
end
end
Single.singleton_methods #=> [:four]
a.singleton_methods(false) #=> [:two, :one]
a.singleton_methods #=> [:two, :one, :three]
@overload singleton_methods(all=true)
@return [Array];T;%0;"@@;&F;+o;,;-T;.iD;/ib;0@
;1I"(VALUE
rb_obj_singleton_methods(int argc, const VALUE *argv, VALUE obj)
{
VALUE ary, klass, origin;
struct method_entry_arg me_arg;
struct rb_id_table *mtbl;
int recur = TRUE;
if (rb_check_arity(argc, 0, 1)) recur = RTEST(argv[0]);
if (RB_TYPE_P(obj, T_CLASS) && FL_TEST(obj, FL_SINGLETON)) {
rb_singleton_class(obj);
}
klass = CLASS_OF(obj);
origin = RCLASS_ORIGIN(klass);
me_arg.list = st_init_numtable();
me_arg.recur = recur;
if (klass && FL_TEST(klass, FL_SINGLETON)) {
if ((mtbl = RCLASS_M_TBL(origin)) != 0) rb_id_table_foreach(mtbl, method_entry_i, &me_arg);
klass = RCLASS_SUPER(klass);
}
if (recur) {
while (klass && (FL_TEST(klass, FL_SINGLETON) || RB_TYPE_P(klass, T_ICLASS))) {
if (klass != origin && (mtbl = RCLASS_M_TBL(klass)) != 0) rb_id_table_foreach(mtbl, method_entry_i, &me_arg);
klass = RCLASS_SUPER(klass);
}
}
ary = rb_ary_new2(me_arg.list->num_entries);
st_foreach(me_arg.list, ins_methods_i, ary);
st_free_table(me_arg.list);
return ary;
};T; I"
VALUE;T;2To;
;F;;
;;;I"Object#protected_methods;F;[[@0;[[@&i ;T;:protected_methods;0;[ ;{ ;IC;"Returns the list of protected methods accessible to obj. If
the all parameter is set to false
, only those methods
in the receiver will be listed.
;T;[o;
;I"
overload;F;0;;;0; I" protected_methods(all=true);T;IC;" ;T;[o;!
;I"return;F;I" ;T;0;[I"
Array;T;"@_;#[ ;$I"@return [Array];T;%0;"@_;&F;'i ;(0;[[I"all;TI" true;T;"@_;#[ ;$I"Returns the list of protected methods accessible to obj. If
the all parameter is set to false
, only those methods
in the receiver will be listed.
@overload protected_methods(all=true)
@return [Array];T;%0;"@_;&F;+o;,;-T;.i;/i;0@
;1I"VALUE
rb_obj_protected_methods(int argc, const VALUE *argv, VALUE obj)
{
return class_instance_method_list(argc, argv, CLASS_OF(obj), 1, ins_methods_prot_i);
};T; I"
VALUE;T;2To;
;F;;
;;;I"Object#private_methods;F;[[@0;[[@&i/;T;:private_methods;0;[ ;{ ;IC;"Returns the list of private methods accessible to obj. If
the all parameter is set to false
, only those methods
in the receiver will be listed.
;T;[o;
;I"
overload;F;0;;;0; I"private_methods(all=true);T;IC;" ;T;[o;!
;I"return;F;I" ;T;0;[I"
Array;T;"@~;#[ ;$I"@return [Array];T;%0;"@~;&F;'i ;(0;[[I"all;TI" true;T;"@~;#[ ;$I"Returns the list of private methods accessible to obj. If
the all parameter is set to false
, only those methods
in the receiver will be listed.
@overload private_methods(all=true)
@return [Array];T;%0;"@~;&F;+o;,;-T;.i&;/i,;0@
;1I"VALUE
rb_obj_private_methods(int argc, const VALUE *argv, VALUE obj)
{
return class_instance_method_list(argc, argv, CLASS_OF(obj), 1, ins_methods_priv_i);
};T; I"
VALUE;T;2To;
;F;;
;;;I"Object#public_methods;F;[[@0;[[@&i>;T;:public_methods;0;[ ;{ ;IC;"Returns the list of public methods accessible to obj. If
the all parameter is set to false
, only those methods
in the receiver will be listed.
;T;[o;
;I"
overload;F;0;;;0; I"public_methods(all=true);T;IC;" ;T;[o;!
;I"return;F;I" ;T;0;[I"
Array;T;"@;#[ ;$I"@return [Array];T;%0;"@;&F;'i ;(0;[[I"all;TI" true;T;"@;#[ ;$I"Returns the list of public methods accessible to obj. If
the all parameter is set to false
, only those methods
in the receiver will be listed.
@overload public_methods(all=true)
@return [Array];T;%0;"@;&F;+o;,;-T;.i5;/i;;0@
;1I"VALUE
rb_obj_public_methods(int argc, const VALUE *argv, VALUE obj)
{
return class_instance_method_list(argc, argv, CLASS_OF(obj), 1, ins_methods_pub_i);
};T; I"
VALUE;T;2To;
;F;;
;;;I"Object#instance_variables;F;[ ;[[I"variable.c;Ti;T;:instance_variables;0;[ ;{ ;IC;"Returns an array of instance variable names for the receiver. Note
that simply defining an accessor does not create the corresponding
instance variable.
class Fred
attr_accessor :a1
def initialize
@iv = 3
end
end
Fred.new.instance_variables #=> [:@iv]
;T;[o;
;I"
overload;F;0;;;0; I"instance_variables;T;IC;" ;T;[o;!
;I"return;F;I" ;T;0;[I"
Array;T;"@;#[ ;$I"@return [Array];T;%0;"@;&F;'i ;(0;[ ;"@;#[ ;$I"NReturns an array of instance variable names for the receiver. Note
that simply defining an accessor does not create the corresponding
instance variable.
class Fred
attr_accessor :a1
def initialize
@iv = 3
end
end
Fred.new.instance_variables #=> [:@iv]
@overload instance_variables
@return [Array];T;%0;"@;&F;+o;,;-T;.i;/i;0@
;1I"VALUE
rb_obj_instance_variables(VALUE obj)
{
VALUE ary;
ary = rb_ary_new();
rb_ivar_foreach(obj, ivar_i, ary);
return ary;
};T; I"
VALUE;T;2To;
;F;;
;;;I"!Object#instance_variable_get;F;[[I"iv;T0;[[@
i6;T;:instance_variable_get;0;[ ;{ ;IC;"Returns the value of the given instance variable, or nil if the
instance variable is not set. The @
part of the
variable name should be included for regular instance
variables. Throws a NameError exception if the
supplied symbol is not valid as an instance variable name.
String arguments are converted to symbols.
class Fred
def initialize(p1, p2)
@a, @b = p1, p2
end
end
fred = Fred.new('cat', 99)
fred.instance_variable_get(:@a) #=> "cat"
fred.instance_variable_get("@b") #=> 99
;T;[o;
;I"
overload;F;0;;;0; I""instance_variable_get(symbol);T;IC;" ;T;[o;!
;I"return;F;I" ;T;0;[I"Object;T;"@;#[ ;$I"@return [Object];T;%0;"@;&F;'i ;(0;[[I"symbol;T0;"@o;
;I"
overload;F;0;;;0; I""instance_variable_get(string);T;IC;" ;T;[o;!
;I"return;F;I" ;T;0;[I"Object;T;"@;#[ ;$I"@return [Object];T;%0;"@;&F;'i ;(0;[[I"string;T0;"@;#[ ;$I"Returns the value of the given instance variable, or nil if the
instance variable is not set. The @
part of the
variable name should be included for regular instance
variables. Throws a NameError exception if the
supplied symbol is not valid as an instance variable name.
String arguments are converted to symbols.
class Fred
def initialize(p1, p2)
@a, @b = p1, p2
end
end
fred = Fred.new('cat', 99)
fred.instance_variable_get(:@a) #=> "cat"
fred.instance_variable_get("@b") #=> 99
@overload instance_variable_get(symbol)
@return [Object]
@overload instance_variable_get(string)
@return [Object];T;%0;"@;&F;+o;,;-T;.i ;/i4;0@
;1I"static VALUE
rb_obj_ivar_get(VALUE obj, VALUE iv)
{
ID id = id_for_var(obj, iv, instance);
if (!id) {
return Qnil;
}
return rb_ivar_get(obj, id);
};T; I"static VALUE;T;2To;
;F;;
;;;I"!Object#instance_variable_set;F;[[I"iv;T0[I"val;T0;[[@
iX;T;:instance_variable_set;0;[ ;{ ;IC;"Sets the instance variable named by symbol to the given
object. This may circumvent the encapsulation intended by
the author of the class, so it should be used with care.
The variable does not have to exist prior to this call.
If the instance variable name is passed as a string, that string
is converted to a symbol.
class Fred
def initialize(p1, p2)
@a, @b = p1, p2
end
end
fred = Fred.new('cat', 99)
fred.instance_variable_set(:@a, 'dog') #=> "dog"
fred.instance_variable_set(:@c, 'cat') #=> "cat"
fred.inspect #=> "#"
;T;[o;
;I"
overload;F;0;;;0; I"'instance_variable_set(symbol, obj);T;IC;" ;T;[o;!
;I"return;F;I" ;T;0;[I"Object;T;"@;#[ ;$I"@return [Object];T;%0;"@;&F;'i ;(0;[[I"symbol;T0[I"obj;T0;"@o;
;I"
overload;F;0;;;0; I"'instance_variable_set(string, obj);T;IC;" ;T;[o;!
;I"return;F;I" ;T;0;[I"Object;T;"@;#[ ;$I"@return [Object];T;%0;"@;&F;'i ;(0;[[I"string;T0[I"obj;T0;"@;#[ ;$I"Sets the instance variable named by symbol to the given
object. This may circumvent the encapsulation intended by
the author of the class, so it should be used with care.
The variable does not have to exist prior to this call.
If the instance variable name is passed as a string, that string
is converted to a symbol.
class Fred
def initialize(p1, p2)
@a, @b = p1, p2
end
end
fred = Fred.new('cat', 99)
fred.instance_variable_set(:@a, 'dog') #=> "dog"
fred.instance_variable_set(:@c, 'cat') #=> "cat"
fred.inspect #=> "#"
@overload instance_variable_set(symbol, obj)
@return [Object]
@overload instance_variable_set(string, obj)
@return [Object];T;%0;"@;&F;+o;,;-T;.iA;/iV;0@
;1I"static VALUE
rb_obj_ivar_set(VALUE obj, VALUE iv, VALUE val)
{
ID id = id_for_var(obj, iv, instance);
if (!id) id = rb_intern_str(iv);
return rb_ivar_set(obj, id, val);
};T; I"static VALUE;T;2To;
;F;;
;;;I"&Object#instance_variable_defined?;F;[[I"iv;T0;[[@
it;T;:instance_variable_defined?;0;[ ;{ ;IC;"Returns true
if the given instance variable is
defined in obj.
String arguments are converted to symbols.
class Fred
def initialize(p1, p2)
@a, @b = p1, p2
end
end
fred = Fred.new('cat', 99)
fred.instance_variable_defined?(:@a) #=> true
fred.instance_variable_defined?("@b") #=> true
fred.instance_variable_defined?("@c") #=> false;T;[o;
;I"
overload;F;0;;;0; I"'instance_variable_defined?(symbol);T;IC;" ;T;[o;!
;I"return;F;I" ;T;0;[I"Boolean;T;"@:;#[ ;$I"@return [Boolean];T;%0;"@:;&F;'i ;(0;[[I"symbol;T0;"@:o;
;I"
overload;F;0;;;0; I"'instance_variable_defined?(string);T;IC;" ;T;[o;!
;I"return;F;I" ;T;0;[I"Boolean;T;"@:;#[ ;$I"@return [Boolean];T;%0;"@:;&F;'i ;(0;[[I"string;T0;"@:;#[ ;$I"Returns true
if the given instance variable is
defined in obj.
String arguments are converted to symbols.
class Fred
def initialize(p1, p2)
@a, @b = p1, p2
end
end
fred = Fred.new('cat', 99)
fred.instance_variable_defined?(:@a) #=> true
fred.instance_variable_defined?("@b") #=> true
fred.instance_variable_defined?("@c") #=> false
@overload instance_variable_defined?(symbol)
@return [Boolean]
@overload instance_variable_defined?(string)
@return [Boolean];T;%0;"@:;&F;+o;,;-T;.i`;/ir;'i ;0@
;1I"static VALUE
rb_obj_ivar_defined(VALUE obj, VALUE iv)
{
ID id = id_for_var(obj, iv, instance);
if (!id) {
return Qfalse;
}
return rb_ivar_defined(obj, id);
};T; I"static VALUE;T;2To;
;F;;
;;;I"$Object#remove_instance_variable;F;[[I" name;T0;[[@i?;T;:remove_instance_variable;0;[ ;{ ;IC;"lRemoves the named instance variable from obj, returning that
variable's value.
String arguments are converted to symbols.
class Dummy
attr_reader :var
def initialize
@var = 99
end
def remove
remove_instance_variable(:@var)
end
end
d = Dummy.new
d.var #=> 99
d.remove #=> 99
d.var #=> nil
;T;[o;
;I"
overload;F;0;;;0; I"%remove_instance_variable(symbol);T;IC;" ;T;[o;!
;I"return;F;I" ;T;0;[I"Object;T;"@h;#[ ;$I"@return [Object];T;%0;"@h;&F;'i ;(0;[[I"symbol;T0;"@ho;
;I"
overload;F;0;;;0; I"%remove_instance_variable(string);T;IC;" ;T;[o;!
;I"return;F;I" ;T;0;[I"Object;T;"@h;#[ ;$I"@return [Object];T;%0;"@h;&F;'i ;(0;[[I"string;T0;"@h;#[ ;$I"Removes the named instance variable from obj, returning that
variable's value.
String arguments are converted to symbols.
class Dummy
attr_reader :var
def initialize
@var = 99
end
def remove
remove_instance_variable(:@var)
end
end
d = Dummy.new
d.var #=> 99
d.remove #=> 99
d.var #=> nil
@overload remove_instance_variable(symbol)
@return [Object]
@overload remove_instance_variable(string)
@return [Object];T;%0;"@h;&F;+o;,;-T;.i';/i=;0@
;1I"JVALUE
rb_obj_remove_instance_variable(VALUE obj, VALUE name)
{
VALUE val = Qnil;
const ID id = id_for_var(obj, name, an, instance);
st_data_t n, v;
struct st_table *iv_index_tbl;
uint32_t index;
rb_check_frozen(obj);
if (!id) {
goto not_defined;
}
switch (BUILTIN_TYPE(obj)) {
case T_OBJECT:
iv_index_tbl = ROBJECT_IV_INDEX_TBL(obj);
if (iv_index_tbl_lookup(iv_index_tbl, id, &index) &&
index < ROBJECT_NUMIV(obj) &&
(val = ROBJECT_IVPTR(obj)[index]) != Qundef) {
ROBJECT_IVPTR(obj)[index] = Qundef;
return val;
}
break;
case T_CLASS:
case T_MODULE:
IVAR_ACCESSOR_SHOULD_BE_MAIN_RACTOR(id);
n = id;
if (RCLASS_IV_TBL(obj) && st_delete(RCLASS_IV_TBL(obj), &n, &v)) {
return (VALUE)v;
}
break;
default:
if (FL_TEST(obj, FL_EXIVAR)) {
if (generic_ivar_remove(obj, id, &val)) {
return val;
}
}
break;
}
not_defined:
rb_name_err_raise("instance variable %1$s not defined",
obj, name);
UNREACHABLE_RETURN(Qnil);
};T; I"
VALUE;T;2To;
;F;;
;;;I"Object#instance_of?;F;[[I"c;T0;[[@
iA;T;:instance_of?;0;[ ;{ ;IC;"Returns true
if obj is an instance of the given
class. See also Object#kind_of?.
class A; end
class B < A; end
class C < B; end
b = B.new
b.instance_of? A #=> false
b.instance_of? B #=> true
b.instance_of? C #=> false;T;[o;
;I"
overload;F;0;;;0; I"instance_of?(class);T;IC;" ;T;[o;!
;I"return;F;I" ;T;0;[I"Boolean;T;"@;#[ ;$I"@return [Boolean];T;%0;"@;&F;'i ;(0;[ ;"@;#[ ;$I"CReturns true
if obj is an instance of the given
class. See also Object#kind_of?.
class A; end
class B < A; end
class C < B; end
b = B.new
b.instance_of? A #=> false
b.instance_of? B #=> true
b.instance_of? C #=> false
@overload instance_of?(class)
@return [Boolean];T;%0;"@;&F;+o;,;-T;.i);/i8;'i ;0@
;1I"VALUE
rb_obj_is_instance_of(VALUE obj, VALUE c)
{
c = class_or_module_required(c);
if (rb_obj_class(obj) == c) return Qtrue;
return Qfalse;
};T; I"
VALUE;T;2To;
;F;;
;;;I"Object#kind_of?;F;[[I"c;T0;[[@
im;T;:
kind_of?;0;[ ;{ ;IC;"Returns true
if class is the class of
obj, or if class is one of the superclasses of
obj or modules included in obj.
module M; end
class A
include M
end
class B < A; end
class C < B; end
b = B.new
b.is_a? A #=> true
b.is_a? B #=> true
b.is_a? C #=> false
b.is_a? M #=> true
b.kind_of? A #=> true
b.kind_of? B #=> true
b.kind_of? C #=> false
b.kind_of? M #=> true;T;[o;
;I"
overload;F;0;:
is_a?;0; I"is_a?(class);T;IC;" ;T;[o;!
;I"return;F;I" ;T;0;[I"Boolean;T;"@;#[ ;$I"@return [Boolean];T;%0;"@;&F;'i ;(0;[ ;"@o;
;I"
overload;F;0;;;0; I"kind_of?(class);T;IC;" ;T;[o;!
;I"return;F;I" ;T;0;[I"Boolean;T;"@;#[ ;$I"@return [Boolean];T;%0;"@;&F;'i ;(0;[ ;"@;#[ ;$I"gReturns true
if class is the class of
obj, or if class is one of the superclasses of
obj or modules included in obj.
module M; end
class A
include M
end
class B < A; end
class C < B; end
b = B.new
b.is_a? A #=> true
b.is_a? B #=> true
b.is_a? C #=> false
b.is_a? M #=> true
b.kind_of? A #=> true
b.kind_of? B #=> true
b.kind_of? C #=> false
b.kind_of? M #=> true
@overload is_a?(class)
@return [Boolean]
@overload kind_of?(class)
@return [Boolean];T;%0;"@;&F;+o;,;-T;.iJ;/ie;'i ;0@
;1I"VALUE
rb_obj_is_kind_of(VALUE obj, VALUE c)
{
VALUE cl = CLASS_OF(obj);
c = class_or_module_required(c);
return class_search_ancestor(cl, RCLASS_ORIGIN(c)) ? Qtrue : Qfalse;
};T; I"
VALUE;T;2To;
;F;;
;;;I"Object#is_a?;F;[[I"c;T0;[[@
im;T;;;0;[ ;{ ;IC;"Returns true
if class is the class of
obj, or if class is one of the superclasses of
obj or modules included in obj.
module M; end
class A
include M
end
class B < A; end
class C < B; end
b = B.new
b.is_a? A #=> true
b.is_a? B #=> true
b.is_a? C #=> false
b.is_a? M #=> true
b.kind_of? A #=> true
b.kind_of? B #=> true
b.kind_of? C #=> false
b.kind_of? M #=> true;T;[o;
;I"
overload;F;0;;;0; I"is_a?(class);T;IC;" ;T;[o;!
;I"return;F;I" ;T;0;[I"Boolean;T;"@;#[ ;$I"@return [Boolean];T;%0;"@;&F;'i ;(0;[ ;"@o;
;I"
overload;F;0;;;0; I"kind_of?(class);T;IC;" ;T;[o;!
;I"return;F;I" ;T;0;[I"Boolean;T;"@;#[ ;$I"@return [Boolean];T;%0;"@;&F;'i ;(0;[ ;"@;#[ ;$@;%0;"@;&F;+o;,;-T;.iJ;/ie;'i ;0@
;1I"VALUE
rb_obj_is_kind_of(VALUE obj, VALUE c)
{
VALUE cl = CLASS_OF(obj);
c = class_or_module_required(c);
return class_search_ancestor(cl, RCLASS_ORIGIN(c)) ? Qtrue : Qfalse;
};T; I"
VALUE;T;2To;
;F;;
;;;I"Object#object_id;F;[ ;[[I" gc.c;Ti;T;:object_id;0;[ ;{ ;IC;"4call-seq:
obj.__id__ -> integer
obj.object_id -> integer
Returns an integer identifier for +obj+.
The same number will be returned on all calls to +object_id+ for a given
object, and no two active objects will share an id.
Note: that some objects of builtin classes are reused for optimization.
This is the case for immediate values and frozen string literals.
BasicObject implements +__id__+, Kernel implements +object_id+.
Immediate values are not passed by reference but are passed by value:
+nil+, +true+, +false+, Fixnums, Symbols, and some Floats.
Object.new.object_id == Object.new.object_id # => false
(21 * 2).object_id == (21 * 2).object_id # => true
"hello".object_id == "hello".object_id # => false
"hi".freeze.object_id == "hi".freeze.object_id # => true
;T;[ ;#[ ;$I"6
call-seq:
obj.__id__ -> integer
obj.object_id -> integer
Returns an integer identifier for +obj+.
The same number will be returned on all calls to +object_id+ for a given
object, and no two active objects will share an id.
Note: that some objects of builtin classes are reused for optimization.
This is the case for immediate values and frozen string literals.
BasicObject implements +__id__+, Kernel implements +object_id+.
Immediate values are not passed by reference but are passed by value:
+nil+, +true+, +false+, Fixnums, Symbols, and some Floats.
Object.new.object_id == Object.new.object_id # => false
(21 * 2).object_id == (21 * 2).object_id # => true
"hello".object_id == "hello".object_id # => false
"hi".freeze.object_id == "hi".freeze.object_id # => true
;T;%0;"@;&F;+o;,;-T;.i;/i;0@
;1I"VALUE
rb_obj_id(VALUE obj)
{
/*
* 32-bit VALUE space
* MSB ------------------------ LSB
* false 00000000000000000000000000000000
* true 00000000000000000000000000000010
* nil 00000000000000000000000000000100
* undef 00000000000000000000000000000110
* symbol ssssssssssssssssssssssss00001110
* object oooooooooooooooooooooooooooooo00 = 0 (mod sizeof(RVALUE))
* fixnum fffffffffffffffffffffffffffffff1
*
* object_id space
* LSB
* false 00000000000000000000000000000000
* true 00000000000000000000000000000010
* nil 00000000000000000000000000000100
* undef 00000000000000000000000000000110
* symbol 000SSSSSSSSSSSSSSSSSSSSSSSSSSS0 S...S % A = 4 (S...S = s...s * A + 4)
* object oooooooooooooooooooooooooooooo0 o...o % A = 0
* fixnum fffffffffffffffffffffffffffffff1 bignum if required
*
* where A = sizeof(RVALUE)/4
*
* sizeof(RVALUE) is
* 20 if 32-bit, double is 4-byte aligned
* 24 if 32-bit, double is 8-byte aligned
* 40 if 64-bit
*/
return rb_find_object_id(obj, cached_object_id);
};T; I"
VALUE;T;2To;
;F;;
;;;I"Object#display;F;[[@0;[[I" io.c;Ti\;T;:display;0;[ ;{ ;IC;"Prints obj on the given port (default $>
).
Equivalent to:
def display(port=$>)
port.write self
nil
end
For example:
1.display
"cat".display
[ 4, 5, 6 ].display
puts
produces:
1cat[4, 5, 6]
;T;[o;
;I"
overload;F;0;;;0; I"display(port=$>);T;IC;" ;T;[o;!
;I"return;F;I" ;T;0;[I"nil;T;"@;#[ ;$I"@return [nil];T;%0;"@;&F;'i ;(0;[[I" port;TI"$>;T;"@;#[ ;$I"*Prints obj on the given port (default $>
).
Equivalent to:
def display(port=$>)
port.write self
nil
end
For example:
1.display
"cat".display
[ 4, 5, 6 ].display
puts
produces:
1cat[4, 5, 6]
@overload display(port=$>)
@return [nil];T;%0;"@;&F;+o;,;-T;.iD;/iY;0@
;1I"static VALUE
rb_obj_display(int argc, VALUE *argv, VALUE self)
{
VALUE out;
out = (!rb_check_arity(argc, 0, 1) ? rb_ractor_stdout() : argv[0]);
rb_io_write(out, self);
return Qnil;
};T; I"static VALUE;T;2To;
;F;;
;;;I"Object#method;F;[[I"vid;T0;[[I"proc.c;Ti;T;:method;0;[ ;{ ;IC;"Looks up the named method as a receiver in obj, returning a
Method object (or raising NameError). The Method object acts as a
closure in obj's object instance, so instance variables and
the value of self
remain available.
class Demo
def initialize(n)
@iv = n
end
def hello()
"Hello, @iv = #{@iv}"
end
end
k = Demo.new(99)
m = k.method(:hello)
m.call #=> "Hello, @iv = 99"
l = Demo.new('Fred')
m = l.method("hello")
m.call #=> "Hello, @iv = Fred"
Note that Method implements to_proc
method, which
means it can be used with iterators.
[ 1, 2, 3 ].each(&method(:puts)) # => prints 3 lines to stdout
out = File.open('test.txt', 'w')
[ 1, 2, 3 ].each(&out.method(:puts)) # => prints 3 lines to file
require 'date'
%w[2017-03-01 2017-03-02].collect(&Date.method(:parse))
#=> [#, #]
;T;[o;
;I"
overload;F;0;;;0; I"method(sym);T;IC;" ;T;[ ;#[ ;$I" ;T;%0;"@5;&F;'i ;(0;[[I"sym;T0;"@5;#[ ;$I"Looks up the named method as a receiver in obj, returning a
Method object (or raising NameError). The Method object acts as a
closure in obj's object instance, so instance variables and
the value of self
remain available.
class Demo
def initialize(n)
@iv = n
end
def hello()
"Hello, @iv = #{@iv}"
end
end
k = Demo.new(99)
m = k.method(:hello)
m.call #=> "Hello, @iv = 99"
l = Demo.new('Fred')
m = l.method("hello")
m.call #=> "Hello, @iv = Fred"
Note that Method implements to_proc
method, which
means it can be used with iterators.
[ 1, 2, 3 ].each(&method(:puts)) # => prints 3 lines to stdout
out = File.open('test.txt', 'w')
[ 1, 2, 3 ].each(&out.method(:puts)) # => prints 3 lines to file
require 'date'
%w[2017-03-01 2017-03-02].collect(&Date.method(:parse))
#=> [#, #]
@overload method(sym);T;%0;"@5;&F;+o;,;-T;.i;/i;0@
;1I"ZVALUE
rb_obj_method(VALUE obj, VALUE vid)
{
return obj_method(obj, vid, FALSE);
};T; I"
VALUE;T;2To;
;F;;
;;;I"Object#public_method;F;[[I"vid;T0;[[@<i;T;:public_method;0;[ ;{ ;IC;"6Similar to _method_, searches public method only.
;T;[o;
;I"
overload;F;0;;;0; I"public_method(sym);T;IC;" ;T;[ ;#[ ;$I" ;T;%0;"@P;&F;'i ;(0;[[I"sym;T0;"@P;#[ ;$I"USimilar to _method_, searches public method only.
@overload public_method(sym);T;%0;"@P;&F;+o;,;-T;.i;/i;0@
;1I"`VALUE
rb_obj_public_method(VALUE obj, VALUE vid)
{
return obj_method(obj, vid, TRUE);
};T; I"
VALUE;T;2To;
;F;;
;;;I"Object#singleton_method;F;[[I"vid;T0;[[@<i;T;:singleton_method;0;[ ;{ ;IC;"^Similar to _method_, searches singleton method only.
class Demo
def initialize(n)
@iv = n
end
def hello()
"Hello, @iv = #{@iv}"
end
end
k = Demo.new(99)
def k.hi
"Hi, @iv = #{@iv}"
end
m = k.singleton_method(:hi)
m.call #=> "Hi, @iv = 99"
m = k.singleton_method(:hello) #=> NameError
;T;[o;
;I"
overload;F;0;;;0; I"singleton_method(sym);T;IC;" ;T;[ ;#[ ;$I" ;T;%0;"@j;&F;'i ;(0;[[I"sym;T0;"@j;#[ ;$I"Similar to _method_, searches singleton method only.
class Demo
def initialize(n)
@iv = n
end
def hello()
"Hello, @iv = #{@iv}"
end
end
k = Demo.new(99)
def k.hi
"Hi, @iv = #{@iv}"
end
m = k.singleton_method(:hi)
m.call #=> "Hi, @iv = 99"
m = k.singleton_method(:hello) #=> NameError
@overload singleton_method(sym);T;%0;"@j;&F;+o;,;-T;.i;/i;0@
;1I"VALUE
rb_obj_singleton_method(VALUE obj, VALUE vid)
{
VALUE klass = rb_singleton_class_get(obj);
ID id = rb_check_id(&vid);
if (NIL_P(klass)) {
/* goto undef; */
}
else if (NIL_P(klass = RCLASS_ORIGIN(klass))) {
/* goto undef; */
}
else if (! id) {
VALUE m = mnew_missing_by_name(klass, obj, &vid, FALSE, rb_cMethod);
if (m) return m;
/* else goto undef; */
}
else {
const rb_method_entry_t *me = rb_method_entry_at(klass, id);
vid = ID2SYM(id);
if (UNDEFINED_METHOD_ENTRY_P(me)) {
/* goto undef; */
}
else if (UNDEFINED_REFINED_METHOD_P(me->def)) {
/* goto undef; */
}
else {
return mnew_from_me(me, klass, klass, obj, id, rb_cMethod, FALSE);
}
}
/* undef: */
rb_name_err_raise("undefined singleton method `%1$s' for `%2$s'",
obj, vid);
UNREACHABLE_RETURN(Qundef);
};T; I"
VALUE;T;2To;
;F;;
;;;I"#Object#define_singleton_method;F;[[@0;[[@<i;T;:define_singleton_method;0;[ ;{ ;IC;"Defines a singleton method in the receiver. The _method_
parameter can be a +Proc+, a +Method+ or an +UnboundMethod+ object.
If a block is specified, it is used as the method body.
If a block or a method has parameters, they're used as method parameters.
class A
class << self
def class_name
to_s
end
end
end
A.define_singleton_method(:who_am_i) do
"I am: #{class_name}"
end
A.who_am_i # ==> "I am: A"
guy = "Bob"
guy.define_singleton_method(:hello) { "#{self}: Hello there!" }
guy.hello #=> "Bob: Hello there!"
chris = "Chris"
chris.define_singleton_method(:greet) {|greeting| "#{greeting}, I'm Chris!" }
chris.greet("Hi") #=> "Hi, I'm Chris!"
;T;[o;
;I"
overload;F;0;;;0; I",define_singleton_method(symbol, method);T;IC;" ;T;[ ;#[ ;$I" ;T;%0;"@;&F;'i ;(0;[[I"symbol;T0[I"method;T0;"@o;
;I"
overload;F;0;;;0; I"$define_singleton_method(symbol);T;IC;" ;T;[o;!
;I"
yield;F;I"[];T;0;0;"@;#[ ;$I"@yield [];T;%0;"@;&F;'i ;(0;[[I"symbol;T0;"@;#[ ;$I">Defines a singleton method in the receiver. The _method_
parameter can be a +Proc+, a +Method+ or an +UnboundMethod+ object.
If a block is specified, it is used as the method body.
If a block or a method has parameters, they're used as method parameters.
class A
class << self
def class_name
to_s
end
end
end
A.define_singleton_method(:who_am_i) do
"I am: #{class_name}"
end
A.who_am_i # ==> "I am: A"
guy = "Bob"
guy.define_singleton_method(:hello) { "#{self}: Hello there!" }
guy.hello #=> "Bob: Hello there!"
chris = "Chris"
chris.define_singleton_method(:greet) {|greeting| "#{greeting}, I'm Chris!" }
chris.greet("Hi") #=> "Hi, I'm Chris!"
@overload define_singleton_method(symbol, method)
@overload define_singleton_method(symbol)
@yield [];T;%0;"@;&F;+o;,;-T;.i;/i;0@
;1I"static VALUE
rb_obj_define_method(int argc, VALUE *argv, VALUE obj)
{
VALUE klass = rb_singleton_class(obj);
return rb_mod_define_method(argc, argv, klass);
};T; I"static VALUE;T;2To;
;F;;
;;;I"Object#send;F;[[@0;[[I"vm_eval.c;Tin;T;: send;0;[ ;{ ;IC;"!Invokes the method identified by _symbol_, passing it any
arguments specified.
When the method is identified by a string, the string is converted
to a symbol.
BasicObject implements +__send__+, Kernel implements +send+.
__send__
is safer than +send+
when _obj_ has the same method name like Socket
.
See also public_send
.
class Klass
def hello(*args)
"Hello " + args.join(' ')
end
end
k = Klass.new
k.send :hello, "gentle", "readers" #=> "Hello gentle readers"
;T;[ o;
;I"
overload;F;0;;;0; I"send(symbol [, args...]);T;IC;" ;T;[o;!
;I"return;F;I" ;T;0;[I"Object;T;"@;#[ ;$I"@return [Object];T;%0;"@;&F;'i ;(0;[[I"symbol[, args...];T0;"@o;
;I"
overload;F;0;:
__send__;0; I"!__send__(symbol [, args...]);T;IC;" ;T;[o;!
;I"return;F;I" ;T;0;[I"Object;T;"@;#[ ;$I"@return [Object];T;%0;"@;&F;'i ;(0;[[I"symbol[, args...];T0;"@o;
;I"
overload;F;0;;;0; I"send(string [, args...]);T;IC;" ;T;[o;!
;I"return;F;I" ;T;0;[I"Object;T;"@;#[ ;$I"@return [Object];T;%0;"@;&F;'i ;(0;[[I"string[, args...];T0;"@o;
;I"
overload;F;0;;;0; I"!__send__(string [, args...]);T;IC;" ;T;[o;!
;I"return;F;I" ;T;0;[I"Object;T;"@;#[ ;$I"@return [Object];T;%0;"@;&F;'i ;(0;[[I"string[, args...];T0;"@;#[ ;$I" Invokes the method identified by _symbol_, passing it any
arguments specified.
When the method is identified by a string, the string is converted
to a symbol.
BasicObject implements +__send__+, Kernel implements +send+.
__send__
is safer than +send+
when _obj_ has the same method name like Socket
.
See also public_send
.
class Klass
def hello(*args)
"Hello " + args.join(' ')
end
end
k = Klass.new
k.send :hello, "gentle", "readers" #=> "Hello gentle readers"
@overload send(symbol [, args...])
@return [Object]
@overload __send__(symbol [, args...])
@return [Object]
@overload send(string [, args...])
@return [Object]
@overload __send__(string [, args...])
@return [Object];T;%0;"@;&F;+o;,;-T;.iT;/in;0@
;1I"vVALUE
rb_f_send(int argc, VALUE *argv, VALUE recv)
{
return send_internal_kw(argc, argv, recv, CALL_FCALL);
};T; I"
VALUE;T;2To;
;F;;
;;;I"Object#public_send;F;[[@0;[[@i;T;:public_send;0;[ ;{ ;IC;"Invokes the method identified by _symbol_, passing it any
arguments specified. Unlike send, public_send calls public
methods only.
When the method is identified by a string, the string is converted
to a symbol.
1.public_send(:puts, "hello") # causes NoMethodError
;T;[o;
;I"
overload;F;0;;;0; I"$public_send(symbol [, args...]);T;IC;" ;T;[o;!
;I"return;F;I" ;T;0;[I"Object;T;"@;#[ ;$I"@return [Object];T;%0;"@;&F;'i ;(0;[[I"symbol[, args...];T0;"@o;
;I"
overload;F;0;;;0; I"$public_send(string [, args...]);T;IC;" ;T;[o;!
;I"return;F;I" ;T;0;[I"Object;T;"@;#[ ;$I"@return [Object];T;%0;"@;&F;'i ;(0;[[I"string[, args...];T0;"@;#[ ;$I"Invokes the method identified by _symbol_, passing it any
arguments specified. Unlike send, public_send calls public
methods only.
When the method is identified by a string, the string is converted
to a symbol.
1.public_send(:puts, "hello") # causes NoMethodError
@overload public_send(symbol [, args...])
@return [Object]
@overload public_send(string [, args...])
@return [Object];T;%0;"@;&F;+o;,;-T;.it;/i;0@
;1I"static VALUE
rb_f_public_send(int argc, VALUE *argv, VALUE recv)
{
return send_internal_kw(argc, argv, recv, CALL_PUBLIC);
};T; I"static VALUE;T;2To;
;F;;
;;;I"Object#to_enum;F;[[@0;[[I"enumerator.c;Tip;T;:to_enum;0;[ ;{ ;IC;"{Creates a new Enumerator which will enumerate by calling +method+ on
+obj+, passing +args+ if any. What was _yielded_ by method becomes
values of enumerator.
If a block is given, it will be used to calculate the size of
the enumerator without the need to iterate it (see Enumerator#size).
=== Examples
str = "xyz"
enum = str.enum_for(:each_byte)
enum.each { |b| puts b }
# => 120
# => 121
# => 122
# protect an array from being modified by some_method
a = [1, 2, 3]
some_method(a.to_enum)
# String#split in block form is more memory-effective:
very_large_string.split("|") { |chunk| return chunk if chunk.include?('DATE') }
# This could be rewritten more idiomatically with to_enum:
very_large_string.to_enum(:split, "|").lazy.grep(/DATE/).first
It is typical to call to_enum when defining methods for
a generic Enumerable, in case no block is passed.
Here is such an example, with parameter passing and a sizing block:
module Enumerable
# a generic method to repeat the values of any enumerable
def repeat(n)
raise ArgumentError, "#{n} is negative!" if n < 0
unless block_given?
return to_enum(__method__, n) do # __method__ is :repeat here
sz = size # Call size and multiply by n...
sz * n if sz # but return nil if size itself is nil
end
end
each do |*val|
n.times { yield *val }
end
end
end
%i[hello world].repeat(2) { |w| puts w }
# => Prints 'hello', 'hello', 'world', 'world'
enum = (1..14).repeat(3)
# => returns an Enumerator when called without a block
enum.first(4) # => [1, 1, 1, 2]
enum.size # => 42
;T;[ o;
;I"
overload;F;0;;;0; I"#to_enum(method = :each, *args);T;IC;" ;T;[o;!
;I"return;F;I" ;T;0;[I"Enumerator;T;"@%;#[ ;$I"@return [Enumerator];T;%0;"@%;&F;'i ;(0;[[I"method;TI"
:each;T[I"
*args;T0;"@%o;
;I"
overload;F;0;:
enum_for;0; I"$enum_for(method = :each, *args);T;IC;" ;T;[o;!
;I"return;F;I" ;T;0;[I"Enumerator;T;"@%;#[ ;$I"@return [Enumerator];T;%0;"@%;&F;'i ;(0;[[I"method;TI"
:each;T[I"
*args;T0;"@%o;
;I"
overload;F;0;;;0; I"#to_enum(method = :each, *args);T;IC;" ;T;[o;!
;I"
yield;F;I" ;T;0;[I"
*args;T;"@%o;!
;I"return;F;I" ;T;0;[I"Enumerator;T;"@%;#[ ;$I"(@yield [*args]
@return [Enumerator];T;%0;"@%;&F;'i ;(0;[[I"method;TI"
:each;T[I"
*args;T0;"@%o;
;I"
overload;F;0;;;0; I"$enum_for(method = :each, *args);T;IC;" ;T;[o;!
;I"
yield;F;I" ;T;0;[I"
*args;T;"@%o;!
;I"return;F;I" ;T;0;[I"Enumerator;T;"@%;#[ ;$I"(@yield [*args]
@return [Enumerator];T;%0;"@%;&F;'i ;(0;[[I"method;TI"
:each;T[I"
*args;T0;"@%;#[ ;$I"Creates a new Enumerator which will enumerate by calling +method+ on
+obj+, passing +args+ if any. What was _yielded_ by method becomes
values of enumerator.
If a block is given, it will be used to calculate the size of
the enumerator without the need to iterate it (see Enumerator#size).
=== Examples
str = "xyz"
enum = str.enum_for(:each_byte)
enum.each { |b| puts b }
# => 120
# => 121
# => 122
# protect an array from being modified by some_method
a = [1, 2, 3]
some_method(a.to_enum)
# String#split in block form is more memory-effective:
very_large_string.split("|") { |chunk| return chunk if chunk.include?('DATE') }
# This could be rewritten more idiomatically with to_enum:
very_large_string.to_enum(:split, "|").lazy.grep(/DATE/).first
It is typical to call to_enum when defining methods for
a generic Enumerable, in case no block is passed.
Here is such an example, with parameter passing and a sizing block:
module Enumerable
# a generic method to repeat the values of any enumerable
def repeat(n)
raise ArgumentError, "#{n} is negative!" if n < 0
unless block_given?
return to_enum(__method__, n) do # __method__ is :repeat here
sz = size # Call size and multiply by n...
sz * n if sz # but return nil if size itself is nil
end
end
each do |*val|
n.times { yield *val }
end
end
end
%i[hello world].repeat(2) { |w| puts w }
# => Prints 'hello', 'hello', 'world', 'world'
enum = (1..14).repeat(3)
# => returns an Enumerator when called without a block
enum.first(4) # => [1, 1, 1, 2]
enum.size # => 42
@overload to_enum(method = :each, *args)
@return [Enumerator]
@overload enum_for(method = :each, *args)
@return [Enumerator]
@overload to_enum(method = :each, *args)
@yield [*args]
@return [Enumerator]
@overload enum_for(method = :each, *args)
@yield [*args]
@return [Enumerator];T;%0;"@%;&F;+o;,;-T;.i3;/is;0@
;1I"Qstatic VALUE
obj_to_enum(int argc, VALUE *argv, VALUE obj)
{
VALUE enumerator, meth = sym_each;
if (argc > 0) {
--argc;
meth = *argv++;
}
enumerator = rb_enumeratorize_with_size(obj, meth, argc, argv, 0);
if (rb_block_given_p()) {
enumerator_ptr(enumerator)->size = rb_block_proc();
}
return enumerator;
};T; I"static VALUE;T;2To;
;F;;
;;;I"Object#enum_for;F;[[@0;[[@+ip;T;;;0;[ ;{ ;IC;"{Creates a new Enumerator which will enumerate by calling +method+ on
+obj+, passing +args+ if any. What was _yielded_ by method becomes
values of enumerator.
If a block is given, it will be used to calculate the size of
the enumerator without the need to iterate it (see Enumerator#size).
=== Examples
str = "xyz"
enum = str.enum_for(:each_byte)
enum.each { |b| puts b }
# => 120
# => 121
# => 122
# protect an array from being modified by some_method
a = [1, 2, 3]
some_method(a.to_enum)
# String#split in block form is more memory-effective:
very_large_string.split("|") { |chunk| return chunk if chunk.include?('DATE') }
# This could be rewritten more idiomatically with to_enum:
very_large_string.to_enum(:split, "|").lazy.grep(/DATE/).first
It is typical to call to_enum when defining methods for
a generic Enumerable, in case no block is passed.
Here is such an example, with parameter passing and a sizing block:
module Enumerable
# a generic method to repeat the values of any enumerable
def repeat(n)
raise ArgumentError, "#{n} is negative!" if n < 0
unless block_given?
return to_enum(__method__, n) do # __method__ is :repeat here
sz = size # Call size and multiply by n...
sz * n if sz # but return nil if size itself is nil
end
end
each do |*val|
n.times { yield *val }
end
end
end
%i[hello world].repeat(2) { |w| puts w }
# => Prints 'hello', 'hello', 'world', 'world'
enum = (1..14).repeat(3)
# => returns an Enumerator when called without a block
enum.first(4) # => [1, 1, 1, 2]
enum.size # => 42
;T;[ o;
;I"
overload;F;0;;;0; I"#to_enum(method = :each, *args);T;IC;" ;T;[o;!
;I"return;F;I" ;T;0;[I"Enumerator;T;"@;#[ ;$I"@return [Enumerator];T;%0;"@;&F;'i ;(0;[[I"method;TI"
:each;T[I"
*args;T0;"@o;
;I"
overload;F;0;;;0; I"$enum_for(method = :each, *args);T;IC;" ;T;[o;!
;I"return;F;I" ;T;0;[I"Enumerator;T;"@;#[ ;$I"@return [Enumerator];T;%0;"@;&F;'i ;(0;[[I"method;TI"
:each;T[I"
*args;T0;"@o;
;I"
overload;F;0;;;0; I"#to_enum(method = :each, *args);T;IC;" ;T;[o;!
;I"
yield;F;I" ;T;0;[I"
*args;T;"@o;!
;I"return;F;I" ;T;0;[I"Enumerator;T;"@;#[ ;$I"(@yield [*args]
@return [Enumerator];T;%0;"@;&F;'i ;(0;[[I"method;TI"
:each;T[I"
*args;T0;"@o;
;I"
overload;F;0;;;0; I"$enum_for(method = :each, *args);T;IC;" ;T;[o;!
;I"
yield;F;I" ;T;0;[I"
*args;T;"@o;!
;I"return;F;I" ;T;0;[I"Enumerator;T;"@;#[ ;$I"(@yield [*args]
@return [Enumerator];T;%0;"@;&F;'i ;(0;[[I"method;TI"
:each;T[I"
*args;T0;"@;#[ ;$@;%0;"@;&F;+o;,;-T;.i3;/is;0@
;1I"Qstatic VALUE
obj_to_enum(int argc, VALUE *argv, VALUE obj)
{
VALUE enumerator, meth = sym_each;
if (argc > 0) {
--argc;
meth = *argv++;
}
enumerator = rb_enumeratorize_with_size(obj, meth, argc, argv, 0);
if (rb_block_given_p()) {
enumerator_ptr(enumerator)->size = rb_block_proc();
}
return enumerator;
};T; I"static VALUE;T;2To;
;F;;
;;;I"Object#respond_to?;F;[[@0;[[I"vm_method.c;Ti ;T;:respond_to?;0;[ ;{ ;IC;"Returns +true+ if _obj_ responds to the given method. Private and
protected methods are included in the search only if the optional
second parameter evaluates to +true+.
If the method is not implemented,
as Process.fork on Windows, File.lchmod on GNU/Linux, etc.,
false is returned.
If the method is not defined, respond_to_missing?
method is called and the result is returned.
When the method name parameter is given as a string, the string is
converted to a symbol.;T;[o;
;I"
overload;F;0;;;0; I"+respond_to?(symbol, include_all=false);T;IC;" ;T;[o;!
;I"return;F;I" ;T;0;[I"Boolean;T;"@;#[ ;$I"@return [Boolean];T;%0;"@;&F;'i ;(0;[[I"symbol;T0[I"include_all;TI"
false;T;"@o;
;I"
overload;F;0;;;0; I"+respond_to?(string, include_all=false);T;IC;" ;T;[o;!
;I"return;F;I" ;T;0;[I"Boolean;T;"@;#[ ;$I"@return [Boolean];T;%0;"@;&F;'i ;(0;[[I"string;T0[I"include_all;TI"
false;T;"@;#[ ;$I"pReturns +true+ if _obj_ responds to the given method. Private and
protected methods are included in the search only if the optional
second parameter evaluates to +true+.
If the method is not implemented,
as Process.fork on Windows, File.lchmod on GNU/Linux, etc.,
false is returned.
If the method is not defined, respond_to_missing?
method is called and the result is returned.
When the method name parameter is given as a string, the string is
converted to a symbol.
@overload respond_to?(symbol, include_all=false)
@return [Boolean]
@overload respond_to?(string, include_all=false)
@return [Boolean];T;%0;"@;&F;+o;,;-T;.i ;/i ;'i ;0@
;1I"static VALUE
obj_respond_to(int argc, VALUE *argv, VALUE obj)
{
VALUE mid, priv;
ID id;
rb_execution_context_t *ec = GET_EC();
rb_scan_args(argc, argv, "11", &mid, &priv);
if (!(id = rb_check_id(&mid))) {
VALUE ret = basic_obj_respond_to_missing(ec, CLASS_OF(obj), obj,
rb_to_symbol(mid), priv);
if (ret == Qundef) ret = Qfalse;
return ret;
}
if (basic_obj_respond_to(ec, CLASS_OF(obj), obj, id, !RTEST(priv)))
return Qtrue;
return Qfalse;
};T; I"static VALUE;T;2To;
;F;;
;;;I"Object#respond_to_missing?;F;[[I"mid;T0[I" priv;T0;[[@i
;T;:respond_to_missing?;0;[ ;{ ;IC;"DO NOT USE THIS DIRECTLY.
Hook method to return whether the _obj_ can respond to _id_ method
or not.
When the method name parameter is given as a string, the string is
converted to a symbol.
See #respond_to?, and the example of BasicObject.;T;[o;
;I"
overload;F;0;;;0; I"-respond_to_missing?(symbol, include_all);T;IC;" ;T;[o;!
;I"return;F;I" ;T;0;[I"Boolean;T;"@;#[ ;$I"@return [Boolean];T;%0;"@;&F;'i ;(0;[[I"symbol;T0[I"include_all;T0;"@o;
;I"
overload;F;0;;;0; I"-respond_to_missing?(string, include_all);T;IC;" ;T;[o;!
;I"return;F;I" ;T;0;[I"Boolean;T;"@;#[ ;$I"@return [Boolean];T;%0;"@;&F;'i ;(0;[[I"string;T0[I"include_all;T0;"@;#[ ;$I"DO NOT USE THIS DIRECTLY.
Hook method to return whether the _obj_ can respond to _id_ method
or not.
When the method name parameter is given as a string, the string is
converted to a symbol.
See #respond_to?, and the example of BasicObject.
@overload respond_to_missing?(symbol, include_all)
@return [Boolean]
@overload respond_to_missing?(string, include_all)
@return [Boolean];T;%0;"@;&F;+o;,;-T;.i
;/i
;'i ;0@
;1I"astatic VALUE
obj_respond_to_missing(VALUE obj, VALUE mid, VALUE priv)
{
return Qfalse;
};T; I"static VALUE;T;2To;
;F;;
;;;I"Object#extend;F;[[@0;[[I"eval.c;Ti;T;:extend;0;[ ;{ ;IC;"sAdds to _obj_ the instance methods from each module given as a
parameter.
module Mod
def hello
"Hello from Mod.\n"
end
end
class Klass
def hello
"Hello from Klass.\n"
end
end
k = Klass.new
k.hello #=> "Hello from Klass.\n"
k.extend(Mod) #=> #
k.hello #=> "Hello from Mod.\n"
;T;[o;
;I"
overload;F;0;;;0; I"extend(module, ...);T;IC;" ;T;[o;!
;I"return;F;I" ;T;0;[I"Object;T;"@O;#[ ;$I"@return [Object];T;%0;"@O;&F;'i ;(0;[ ;"@O;#[ ;$I"Adds to _obj_ the instance methods from each module given as a
parameter.
module Mod
def hello
"Hello from Mod.\n"
end
end
class Klass
def hello
"Hello from Klass.\n"
end
end
k = Klass.new
k.hello #=> "Hello from Klass.\n"
k.extend(Mod) #=> #
k.hello #=> "Hello from Mod.\n"
@overload extend(module, ...)
@return [Object];T;%0;"@O;&F;+o;,;-T;.i;/i;0@
;1I"static VALUE
rb_obj_extend(int argc, VALUE *argv, VALUE obj)
{
int i;
ID id_extend_object, id_extended;
CONST_ID(id_extend_object, "extend_object");
CONST_ID(id_extended, "extended");
rb_check_arity(argc, 1, UNLIMITED_ARGUMENTS);
for (i = 0; i < argc; i++)
Check_Type(argv[i], T_MODULE);
while (argc--) {
rb_funcall(argv[argc], id_extend_object, 1, obj);
rb_funcall(argv[argc], id_extended, 1, obj);
}
return obj;
};T; I"static VALUE;T;2To;7;[[@i;F;:Fixnum;;9;;;[ ;{ ;IC;"#An obsolete class, use Integer
;T;[ ;#[ ;$I"#An obsolete class, use Integer;T;%0;"@l;&F;+o;,;-T;.i;/i;0@
;I"Object::Fixnum;F;:I"rb_cInteger;T;o@
;pIC;[ ;o@
;qIC;[o;;IC;[Do;
;F;;
;;;I"Kernel#at_exit;F;[ ;[[I"eval_jump.c;Ti*;T;:at_exit;0;[ ;{ ;IC;"Converts _block_ to a +Proc+ object (and therefore
binds it at the point of call) and registers it for execution when
the program exits. If multiple handlers are registered, they are
executed in reverse order of registration.
def do_at_exit(str1)
at_exit { print str1 }
end
at_exit { puts "cruel world" }
do_at_exit("goodbye ")
exit
produces:
goodbye cruel world
;T;[o;
;I"
overload;F;0;;;0; I"at_exit;T;IC;" ;T;[o;!
;I"
yield;F;I"[];T;0;0;"@|o;!
;I"return;F;I" ;T;0;[I" Proc;T;"@|;#[ ;$I"@yield []
@return [Proc];T;%0;"@|;&F;'i ;(0;[ ;"@|;#[ ;$I"Converts _block_ to a +Proc+ object (and therefore
binds it at the point of call) and registers it for execution when
the program exits. If multiple handlers are registered, they are
executed in reverse order of registration.
def do_at_exit(str1)
at_exit { print str1 }
end
at_exit { puts "cruel world" }
do_at_exit("goodbye ")
exit
produces:
goodbye cruel world
@overload at_exit
@yield []
@return [Proc];T;%0;"@|;&F;+o;,;-T;.i;/i(;0@z;1I"static VALUE
rb_f_at_exit(VALUE _)
{
VALUE proc;
if (!rb_block_given_p()) {
rb_raise(rb_eArgError, "called without a block");
}
proc = rb_block_proc();
rb_set_end_proc(rb_call_end_proc, proc);
return proc;
};T; I"static VALUE;T;2To;
;F;;
;;;I"Kernel#srand;F;[[@0;[[I"
random.c;TiW;T;:
srand;0;[ ;{ ;IC;"hSeeds the system pseudo-random number generator, with +number+.
The previous seed value is returned.
If +number+ is omitted, seeds the generator using a source of entropy
provided by the operating system, if available (/dev/urandom on Unix systems
or the RSA cryptographic provider on Windows), which is then combined with
the time, the process id, and a sequence number.
srand may be used to ensure repeatable sequences of pseudo-random numbers
between different runs of the program. By setting the seed to a known value,
programs can be made deterministic during testing.
srand 1234 # => 268519324636777531569100071560086917274
[ rand, rand ] # => [0.1915194503788923, 0.6221087710398319]
[ rand(10), rand(1000) ] # => [4, 664]
srand 1234 # => 1234
[ rand, rand ] # => [0.1915194503788923, 0.6221087710398319]
;T;[o;
;I"
overload;F;0;;;0; I"$srand(number = Random.new_seed);T;IC;" ;T;[ ;#[ ;$I" ;T;%0;"@;&F;'i ;(0;[[I"number;TI"Random.new_seed;T;"@;#[ ;$I"Seeds the system pseudo-random number generator, with +number+.
The previous seed value is returned.
If +number+ is omitted, seeds the generator using a source of entropy
provided by the operating system, if available (/dev/urandom on Unix systems
or the RSA cryptographic provider on Windows), which is then combined with
the time, the process id, and a sequence number.
srand may be used to ensure repeatable sequences of pseudo-random numbers
between different runs of the program. By setting the seed to a known value,
programs can be made deterministic during testing.
srand 1234 # => 268519324636777531569100071560086917274
[ rand, rand ] # => [0.1915194503788923, 0.6221087710398319]
[ rand(10), rand(1000) ] # => [4, 664]
srand 1234 # => 1234
[ rand, rand ] # => [0.1915194503788923, 0.6221087710398319]
@overload srand(number = Random.new_seed);T;%0;"@;&F;+o;,;-T;.i@;/iS;0@z;1I"ystatic VALUE
rb_f_srand(int argc, VALUE *argv, VALUE obj)
{
VALUE seed, old;
rb_random_mt_t *r = rand_mt_start(default_rand());
if (rb_check_arity(argc, 0, 1) == 0) {
seed = random_seed(obj);
}
else {
seed = rb_to_int(argv[0]);
}
old = r->base.seed;
rand_init(&random_mt_if, &r->base, seed);
r->base.seed = seed;
return old;
};T; I"static VALUE;T;2To;
;F;;
;;;I"Kernel#rand;F;[[@0;[[@i;T;: rand;0;[ ;{ ;IC;"0If called without an argument, or if max.to_i.abs == 0, rand
returns a pseudo-random floating point number between 0.0 and 1.0,
including 0.0 and excluding 1.0.
rand #=> 0.2725926052826416
When +max.abs+ is greater than or equal to 1, +rand+ returns a pseudo-random
integer greater than or equal to 0 and less than +max.to_i.abs+.
rand(100) #=> 12
When +max+ is a Range, +rand+ returns a random number where
range.member?(number) == true.
Negative or floating point values for +max+ are allowed, but may give
surprising results.
rand(-100) # => 87
rand(-0.5) # => 0.8130921818028143
rand(1.9) # equivalent to rand(1), which is always 0
Kernel.srand may be used to ensure that sequences of random numbers are
reproducible between different runs of a program.
See also Random.rand.
;T;[o;
;I"
overload;F;0;;;0; I"rand(max=0);T;IC;" ;T;[o;!
;I"return;F;I" ;T;0;[I"Numeric;T;"@;#[ ;$I"@return [Numeric];T;%0;"@;&F;'i ;(0;[[I"max;TI"0;T;"@;#[ ;$I"\If called without an argument, or if max.to_i.abs == 0, rand
returns a pseudo-random floating point number between 0.0 and 1.0,
including 0.0 and excluding 1.0.
rand #=> 0.2725926052826416
When +max.abs+ is greater than or equal to 1, +rand+ returns a pseudo-random
integer greater than or equal to 0 and less than +max.to_i.abs+.
rand(100) #=> 12
When +max+ is a Range, +rand+ returns a random number where
range.member?(number) == true.
Negative or floating point values for +max+ are allowed, but may give
surprising results.
rand(-100) # => 87
rand(-0.5) # => 0.8130921818028143
rand(1.9) # equivalent to rand(1), which is always 0
Kernel.srand may be used to ensure that sequences of random numbers are
reproducible between different runs of a program.
See also Random.rand.
@overload rand(max=0)
@return [Numeric];T;%0;"@;&F;+o;,;-T;.i;/i;0@z;1I"static VALUE
rb_f_rand(int argc, VALUE *argv, VALUE obj)
{
VALUE vmax;
rb_random_t *rnd = rand_start(default_rand());
if (rb_check_arity(argc, 0, 1) && !NIL_P(vmax = argv[0])) {
VALUE v = rand_range(obj, rnd, vmax);
if (v != Qfalse) return v;
vmax = rb_to_int(vmax);
if (vmax != INT2FIX(0)) {
v = rand_int(obj, rnd, vmax, 0);
if (!NIL_P(v)) return v;
}
}
return DBL2NUM(random_real(obj, rnd, TRUE));
};T; I"static VALUE;T;2To;
;F;;
;;;I"Kernel#Complex;F;[[@0;[[@i%;T;:Complex;0;[ ;{ ;IC;"DReturns x+i*y;
Complex(1, 2) #=> (1+2i)
Complex('1+2i') #=> (1+2i)
Complex(nil) #=> TypeError
Complex(1, nil) #=> TypeError
Complex(1, nil, exception: false) #=> nil
Complex('1+2', exception: false) #=> nil
Syntax of string form:
string form = extra spaces , complex , extra spaces ;
complex = real part | [ sign ] , imaginary part
| real part , sign , imaginary part
| rational , "@" , rational ;
real part = rational ;
imaginary part = imaginary unit | unsigned rational , imaginary unit ;
rational = [ sign ] , unsigned rational ;
unsigned rational = numerator | numerator , "/" , denominator ;
numerator = integer part | fractional part | integer part , fractional part ;
denominator = digits ;
integer part = digits ;
fractional part = "." , digits , [ ( "e" | "E" ) , [ sign ] , digits ] ;
imaginary unit = "i" | "I" | "j" | "J" ;
sign = "-" | "+" ;
digits = digit , { digit | "_" , digit };
digit = "0" | "1" | "2" | "3" | "4" | "5" | "6" | "7" | "8" | "9" ;
extra spaces = ? \s* ? ;
See String#to_c.
;T;[o;
;I"
overload;F;0;;;0; I"%Complex(x[, y], exception: true);T;IC;" ;T;[o;!
;I"return;F;I" ;T;0;[I"Numeric;TI"nil;T;"@;#[ ;$I"@return [Numeric, nil];T;%0;"@;&F;'i ;(0;[[I"x[, y];T0[I"exception:;TI" true;T;"@;#[ ;$I"Returns x+i*y;
Complex(1, 2) #=> (1+2i)
Complex('1+2i') #=> (1+2i)
Complex(nil) #=> TypeError
Complex(1, nil) #=> TypeError
Complex(1, nil, exception: false) #=> nil
Complex('1+2', exception: false) #=> nil
Syntax of string form:
string form = extra spaces , complex , extra spaces ;
complex = real part | [ sign ] , imaginary part
| real part , sign , imaginary part
| rational , "@" , rational ;
real part = rational ;
imaginary part = imaginary unit | unsigned rational , imaginary unit ;
rational = [ sign ] , unsigned rational ;
unsigned rational = numerator | numerator , "/" , denominator ;
numerator = integer part | fractional part | integer part , fractional part ;
denominator = digits ;
integer part = digits ;
fractional part = "." , digits , [ ( "e" | "E" ) , [ sign ] , digits ] ;
imaginary unit = "i" | "I" | "j" | "J" ;
sign = "-" | "+" ;
digits = digit , { digit | "_" , digit };
digit = "0" | "1" | "2" | "3" | "4" | "5" | "6" | "7" | "8" | "9" ;
extra spaces = ? \s* ? ;
See String#to_c.
@overload Complex(x[, y], exception: true)
@return [Numeric, nil];T;%0;"@;&F;+o;,;-T;.i;/i#;0@z;1I"static VALUE
nucomp_f_complex(int argc, VALUE *argv, VALUE klass)
{
VALUE a1, a2, opts = Qnil;
int raise = TRUE;
if (rb_scan_args(argc, argv, "11:", &a1, &a2, &opts) == 1) {
a2 = Qundef;
}
if (!NIL_P(opts)) {
raise = rb_opts_exception_p(opts, raise);
}
if (argc > 0 && CLASS_OF(a1) == rb_cComplex && a2 == Qundef) {
return a1;
}
return nucomp_convert(rb_cComplex, a1, a2, raise);
};T; I"static VALUE;T;2To;
;F;;
;;;I"Kernel#trap;F;[[@0;[[I"
signal.c;Tik;T;: trap;0;[ ;{ ;IC;"Specifies the handling of signals. The first parameter is a signal
name (a string such as ``SIGALRM'', ``SIGUSR1'', and so on) or a
signal number. The characters ``SIG'' may be omitted from the
signal name. The command or block specifies code to be run when the
signal is raised.
If the command is the string ``IGNORE'' or ``SIG_IGN'', the signal
will be ignored.
If the command is ``DEFAULT'' or ``SIG_DFL'', the Ruby's default handler
will be invoked.
If the command is ``EXIT'', the script will be terminated by the signal.
If the command is ``SYSTEM_DEFAULT'', the operating system's default
handler will be invoked.
Otherwise, the given command or block will be run.
The special signal name ``EXIT'' or signal number zero will be
invoked just prior to program termination.
trap returns the previous handler for the given signal.
Signal.trap(0, proc { puts "Terminating: #{$$}" })
Signal.trap("CLD") { puts "Child died" }
fork && Process.wait
produces:
Terminating: 27461
Child died
Terminating: 27460
;T;[o;
;I"
overload;F;0;;;0; I"trap( signal, command );T;IC;" ;T;[o;!
;I"return;F;I" ;T;0;[I"Object;T;"@;#[ ;$I"@return [Object];T;%0;"@;&F;'i ;(0;[[I"signal;T0[I"command;T0;"@o;
;I"
overload;F;0;;;0; I"trap( signal );T;IC;" ;T;[o;!
;I"
yield;F;I" ;T;0;[I" ;T;"@o;!
;I"return;F;I" ;T;0;[I"Object;T;"@;#[ ;$I" @yield [ ]
@return [Object];T;%0;"@;&F;'i ;(0;[[I"signal;T0;"@;#[ ;$I"xSpecifies the handling of signals. The first parameter is a signal
name (a string such as ``SIGALRM'', ``SIGUSR1'', and so on) or a
signal number. The characters ``SIG'' may be omitted from the
signal name. The command or block specifies code to be run when the
signal is raised.
If the command is the string ``IGNORE'' or ``SIG_IGN'', the signal
will be ignored.
If the command is ``DEFAULT'' or ``SIG_DFL'', the Ruby's default handler
will be invoked.
If the command is ``EXIT'', the script will be terminated by the signal.
If the command is ``SYSTEM_DEFAULT'', the operating system's default
handler will be invoked.
Otherwise, the given command or block will be run.
The special signal name ``EXIT'' or signal number zero will be
invoked just prior to program termination.
trap returns the previous handler for the given signal.
Signal.trap(0, proc { puts "Terminating: #{$$}" })
Signal.trap("CLD") { puts "Child died" }
fork && Process.wait
produces:
Terminating: 27461
Child died
Terminating: 27460
@overload trap( signal, command )
@return [Object]
@overload trap( signal )
@yield [ ]
@return [Object];T;%0;"@;&F;+o;,;-T;.iL;/ik;0@z;1I"static VALUE
sig_trap(int argc, VALUE *argv, VALUE _)
{
int sig;
sighandler_t func;
VALUE cmd;
rb_check_arity(argc, 1, 2);
sig = trap_signm(argv[0]);
if (reserved_signal_p(sig)) {
const char *name = signo2signm(sig);
if (name)
rb_raise(rb_eArgError, "can't trap reserved signal: SIG%s", name);
else
rb_raise(rb_eArgError, "can't trap reserved signal: %d", sig);
}
if (argc == 1) {
cmd = rb_block_proc();
func = sighandler;
}
else {
cmd = argv[1];
func = trap_handler(&cmd, sig);
}
if (rb_obj_is_proc(cmd) &&
!rb_ractor_main_p() && !rb_ractor_shareable_p(cmd)) {
cmd = rb_proc_isolate(cmd);
}
return trap(sig, func, cmd);
};T; I"static VALUE;T;2To;
;F;;
;;;I"Kernel#set_trace_func;F;[[I"
trace;T0;[[I"vm_trace.c;Ti;T;:set_trace_func;0;[ ;{ ;IC;"+Establishes _proc_ as the handler for tracing, or disables
tracing if the parameter is +nil+.
*Note:* this method is obsolete, please use TracePoint instead.
_proc_ takes up to six parameters:
* an event name
* a filename
* a line number
* an object id
* a binding
* the name of a class
_proc_ is invoked whenever an event occurs.
Events are:
+c-call+:: call a C-language routine
+c-return+:: return from a C-language routine
+call+:: call a Ruby method
+class+:: start a class or module definition
+end+:: finish a class or module definition
+line+:: execute code on a new line
+raise+:: raise an exception
+return+:: return from a Ruby method
Tracing is disabled within the context of _proc_.
class Test
def test
a = 1
b = 2
end
end
set_trace_func proc { |event, file, line, id, binding, classname|
printf "%8s %s:%-2d %10s %8s\n", event, file, line, id, classname
}
t = Test.new
t.test
line prog.rb:11 false
c-call prog.rb:11 new Class
c-call prog.rb:11 initialize Object
c-return prog.rb:11 initialize Object
c-return prog.rb:11 new Class
line prog.rb:12 false
call prog.rb:2 test Test
line prog.rb:3 test Test
line prog.rb:4 test Test
return prog.rb:4 test Test
;T;[o;
;I"
overload;F;0;;;0; I"set_trace_func(proc);T;IC;" ;T;[o;!
;I"return;F;I" ;T;0;[I" Proc;T;"@,;#[ ;$I"@return [Proc];T;%0;"@,;&F;'i ;(0;[[I" proc;T0;"@,o;
;I"
overload;F;0;;;0; I"set_trace_func(nil);T;IC;" ;T;[o;!
;I"return;F;I" ;T;0;[I"nil;T;"@,;#[ ;$I"@return [nil];T;%0;"@,;&F;'i ;(0;[[I"nil;T0;"@,;#[ ;$I"Establishes _proc_ as the handler for tracing, or disables
tracing if the parameter is +nil+.
*Note:* this method is obsolete, please use TracePoint instead.
_proc_ takes up to six parameters:
* an event name
* a filename
* a line number
* an object id
* a binding
* the name of a class
_proc_ is invoked whenever an event occurs.
Events are:
+c-call+:: call a C-language routine
+c-return+:: return from a C-language routine
+call+:: call a Ruby method
+class+:: start a class or module definition
+end+:: finish a class or module definition
+line+:: execute code on a new line
+raise+:: raise an exception
+return+:: return from a Ruby method
Tracing is disabled within the context of _proc_.
class Test
def test
a = 1
b = 2
end
end
set_trace_func proc { |event, file, line, id, binding, classname|
printf "%8s %s:%-2d %10s %8s\n", event, file, line, id, classname
}
t = Test.new
t.test
line prog.rb:11 false
c-call prog.rb:11 new Class
c-call prog.rb:11 initialize Object
c-return prog.rb:11 initialize Object
c-return prog.rb:11 new Class
line prog.rb:12 false
call prog.rb:2 test Test
line prog.rb:3 test Test
line prog.rb:4 test Test
return prog.rb:4 test Test
@overload set_trace_func(proc)
@return [Proc]
@overload set_trace_func(nil)
@return [nil];T;%0;"@,;&F;+o;,;-T;.i;/i
;0@z;1I"Cstatic VALUE
set_trace_func(VALUE obj, VALUE trace)
{
rb_remove_event_hook(call_trace_func);
if (NIL_P(trace)) {
return Qnil;
}
if (!rb_obj_is_proc(trace)) {
rb_raise(rb_eTypeError, "trace_func needs to be Proc");
}
rb_add_event_hook(call_trace_func, RUBY_EVENT_ALL, trace);
return trace;
};T; I"static VALUE;T;2To;
;F;;
;;;I"Kernel#Rational;F;[[@0;[[@i$;T;:
Rational;0;[ ;{ ;IC;"Returns +x/y+ or +arg+ as a Rational.
Rational(2, 3) #=> (2/3)
Rational(5) #=> (5/1)
Rational(0.5) #=> (1/2)
Rational(0.3) #=> (5404319552844595/18014398509481984)
Rational("2/3") #=> (2/3)
Rational("0.3") #=> (3/10)
Rational("10 cents") #=> ArgumentError
Rational(nil) #=> TypeError
Rational(1, nil) #=> TypeError
Rational("10 cents", exception: false) #=> nil
Syntax of the string form:
string form = extra spaces , rational , extra spaces ;
rational = [ sign ] , unsigned rational ;
unsigned rational = numerator | numerator , "/" , denominator ;
numerator = integer part | fractional part | integer part , fractional part ;
denominator = digits ;
integer part = digits ;
fractional part = "." , digits , [ ( "e" | "E" ) , [ sign ] , digits ] ;
sign = "-" | "+" ;
digits = digit , { digit | "_" , digit } ;
digit = "0" | "1" | "2" | "3" | "4" | "5" | "6" | "7" | "8" | "9" ;
extra spaces = ? \s* ? ;
See also String#to_r.
;T;[o;
;I"
overload;F;0;;;0; I"$Rational(x, y, exception: true);T;IC;" ;T;[o;!
;I"return;F;I" ;T;0;[I"nil;T;"@[;#[ ;$I"@return [nil];T;%0;"@[;&F;'i ;(0;[[I"x;T0[I"y;T0[I"exception:;TI" true;T;"@[o;
;I"
overload;F;0;;;0; I"#Rational(arg, exception: true);T;IC;" ;T;[o;!
;I"return;F;I" ;T;0;[I"nil;T;"@[;#[ ;$I"@return [nil];T;%0;"@[;&F;'i ;(0;[[I"arg;T0[I"exception:;TI" true;T;"@[;#[ ;$I"jReturns +x/y+ or +arg+ as a Rational.
Rational(2, 3) #=> (2/3)
Rational(5) #=> (5/1)
Rational(0.5) #=> (1/2)
Rational(0.3) #=> (5404319552844595/18014398509481984)
Rational("2/3") #=> (2/3)
Rational("0.3") #=> (3/10)
Rational("10 cents") #=> ArgumentError
Rational(nil) #=> TypeError
Rational(1, nil) #=> TypeError
Rational("10 cents", exception: false) #=> nil
Syntax of the string form:
string form = extra spaces , rational , extra spaces ;
rational = [ sign ] , unsigned rational ;
unsigned rational = numerator | numerator , "/" , denominator ;
numerator = integer part | fractional part | integer part , fractional part ;
denominator = digits ;
integer part = digits ;
fractional part = "." , digits , [ ( "e" | "E" ) , [ sign ] , digits ] ;
sign = "-" | "+" ;
digits = digit , { digit | "_" , digit } ;
digit = "0" | "1" | "2" | "3" | "4" | "5" | "6" | "7" | "8" | "9" ;
extra spaces = ? \s* ? ;
See also String#to_r.
@overload Rational(x, y, exception: true)
@return [nil]
@overload Rational(arg, exception: true)
@return [nil];T;%0;"@[;&F;+o;,;-T;.i;/i#;0@z;1I"^static VALUE
nurat_f_rational(int argc, VALUE *argv, VALUE klass)
{
VALUE a1, a2, opts = Qnil;
int raise = TRUE;
if (rb_scan_args(argc, argv, "11:", &a1, &a2, &opts) == 1) {
a2 = Qundef;
}
if (!NIL_P(opts)) {
raise = rb_opts_exception_p(opts, raise);
}
return nurat_convert(rb_cRational, a1, a2, raise);
};T; I"static VALUE;T;2To;
;F;;
;;;I"Kernel#sprintf;F;[[I"*v;T0[I"_;T0;[[@
i;T;:sprintf;0;[ ;{ ;IC;"F,Returns the string resulting from applying format_string to
any additional arguments. Within the format string, any characters
other than format sequences are copied to the result.
The syntax of a format sequence is as follows.
%[flags][width][.precision]type
A format
sequence consists of a percent sign, followed by optional flags,
width, and precision indicators, then terminated with a field type
character. The field type controls how the corresponding
sprintf
argument is to be interpreted, while the flags
modify that interpretation.
The field type characters are:
Field | Integer Format
------+--------------------------------------------------------------
b | Convert argument as a binary number.
| Negative numbers will be displayed as a two's complement
| prefixed with `..1'.
B | Equivalent to `b', but uses an uppercase 0B for prefix
| in the alternative format by #.
d | Convert argument as a decimal number.
i | Identical to `d'.
o | Convert argument as an octal number.
| Negative numbers will be displayed as a two's complement
| prefixed with `..7'.
u | Identical to `d'.
x | Convert argument as a hexadecimal number.
| Negative numbers will be displayed as a two's complement
| prefixed with `..f' (representing an infinite string of
| leading 'ff's).
X | Equivalent to `x', but uses uppercase letters.
Field | Float Format
------+--------------------------------------------------------------
e | Convert floating point argument into exponential notation
| with one digit before the decimal point as [-]d.dddddde[+-]dd.
| The precision specifies the number of digits after the decimal
| point (defaulting to six).
E | Equivalent to `e', but uses an uppercase E to indicate
| the exponent.
f | Convert floating point argument as [-]ddd.dddddd,
| where the precision specifies the number of digits after
| the decimal point.
g | Convert a floating point number using exponential form
| if the exponent is less than -4 or greater than or
| equal to the precision, or in dd.dddd form otherwise.
| The precision specifies the number of significant digits.
G | Equivalent to `g', but use an uppercase `E' in exponent form.
a | Convert floating point argument as [-]0xh.hhhhp[+-]dd,
| which is consisted from optional sign, "0x", fraction part
| as hexadecimal, "p", and exponential part as decimal.
A | Equivalent to `a', but use uppercase `X' and `P'.
Field | Other Format
------+--------------------------------------------------------------
c | Argument is the numeric code for a single character or
| a single character string itself.
p | The valuing of argument.inspect.
s | Argument is a string to be substituted. If the format
| sequence contains a precision, at most that many characters
| will be copied.
% | A percent sign itself will be displayed. No argument taken.
The flags modifies the behavior of the formats.
The flag characters are:
Flag | Applies to | Meaning
---------+---------------+-----------------------------------------
space | bBdiouxX | Leave a space at the start of
| aAeEfgG | non-negative numbers.
| (numeric fmt) | For `o', `x', `X', `b' and `B', use
| | a minus sign with absolute value for
| | negative values.
---------+---------------+-----------------------------------------
(digit)$ | all | Specifies the absolute argument number
| | for this field. Absolute and relative
| | argument numbers cannot be mixed in a
| | sprintf string.
---------+---------------+-----------------------------------------
# | bBoxX | Use an alternative format.
| aAeEfgG | For the conversions `o', increase the precision
| | until the first digit will be `0' if
| | it is not formatted as complements.
| | For the conversions `x', `X', `b' and `B'
| | on non-zero, prefix the result with ``0x'',
| | ``0X'', ``0b'' and ``0B'', respectively.
| | For `a', `A', `e', `E', `f', `g', and 'G',
| | force a decimal point to be added,
| | even if no digits follow.
| | For `g' and 'G', do not remove trailing zeros.
---------+---------------+-----------------------------------------
+ | bBdiouxX | Add a leading plus sign to non-negative
| aAeEfgG | numbers.
| (numeric fmt) | For `o', `x', `X', `b' and `B', use
| | a minus sign with absolute value for
| | negative values.
---------+---------------+-----------------------------------------
- | all | Left-justify the result of this conversion.
---------+---------------+-----------------------------------------
0 (zero) | bBdiouxX | Pad with zeros, not spaces.
| aAeEfgG | For `o', `x', `X', `b' and `B', radix-1
| (numeric fmt) | is used for negative numbers formatted as
| | complements.
---------+---------------+-----------------------------------------
* | all | Use the next argument as the field width.
| | If negative, left-justify the result. If the
| | asterisk is followed by a number and a dollar
| | sign, use the indicated argument as the width.
Examples of flags:
# `+' and space flag specifies the sign of non-negative numbers.
sprintf("%d", 123) #=> "123"
sprintf("%+d", 123) #=> "+123"
sprintf("% d", 123) #=> " 123"
# `#' flag for `o' increases number of digits to show `0'.
# `+' and space flag changes format of negative numbers.
sprintf("%o", 123) #=> "173"
sprintf("%#o", 123) #=> "0173"
sprintf("%+o", -123) #=> "-173"
sprintf("%o", -123) #=> "..7605"
sprintf("%#o", -123) #=> "..7605"
# `#' flag for `x' add a prefix `0x' for non-zero numbers.
# `+' and space flag disables complements for negative numbers.
sprintf("%x", 123) #=> "7b"
sprintf("%#x", 123) #=> "0x7b"
sprintf("%+x", -123) #=> "-7b"
sprintf("%x", -123) #=> "..f85"
sprintf("%#x", -123) #=> "0x..f85"
sprintf("%#x", 0) #=> "0"
# `#' for `X' uses the prefix `0X'.
sprintf("%X", 123) #=> "7B"
sprintf("%#X", 123) #=> "0X7B"
# `#' flag for `b' add a prefix `0b' for non-zero numbers.
# `+' and space flag disables complements for negative numbers.
sprintf("%b", 123) #=> "1111011"
sprintf("%#b", 123) #=> "0b1111011"
sprintf("%+b", -123) #=> "-1111011"
sprintf("%b", -123) #=> "..10000101"
sprintf("%#b", -123) #=> "0b..10000101"
sprintf("%#b", 0) #=> "0"
# `#' for `B' uses the prefix `0B'.
sprintf("%B", 123) #=> "1111011"
sprintf("%#B", 123) #=> "0B1111011"
# `#' for `e' forces to show the decimal point.
sprintf("%.0e", 1) #=> "1e+00"
sprintf("%#.0e", 1) #=> "1.e+00"
# `#' for `f' forces to show the decimal point.
sprintf("%.0f", 1234) #=> "1234"
sprintf("%#.0f", 1234) #=> "1234."
# `#' for `g' forces to show the decimal point.
# It also disables stripping lowest zeros.
sprintf("%g", 123.4) #=> "123.4"
sprintf("%#g", 123.4) #=> "123.400"
sprintf("%g", 123456) #=> "123456"
sprintf("%#g", 123456) #=> "123456."
The field width is an optional integer, followed optionally by a
period and a precision. The width specifies the minimum number of
characters that will be written to the result for this field.
Examples of width:
# padding is done by spaces, width=20
# 0 or radix-1. <------------------>
sprintf("%20d", 123) #=> " 123"
sprintf("%+20d", 123) #=> " +123"
sprintf("%020d", 123) #=> "00000000000000000123"
sprintf("%+020d", 123) #=> "+0000000000000000123"
sprintf("% 020d", 123) #=> " 0000000000000000123"
sprintf("%-20d", 123) #=> "123 "
sprintf("%-+20d", 123) #=> "+123 "
sprintf("%- 20d", 123) #=> " 123 "
sprintf("%020x", -123) #=> "..ffffffffffffffff85"
For
numeric fields, the precision controls the number of decimal places
displayed. For string fields, the precision determines the maximum
number of characters to be copied from the string. (Thus, the format
sequence %10.10s
will always contribute exactly ten
characters to the result.)
Examples of precisions:
# precision for `d', 'o', 'x' and 'b' is
# minimum number of digits <------>
sprintf("%20.8d", 123) #=> " 00000123"
sprintf("%20.8o", 123) #=> " 00000173"
sprintf("%20.8x", 123) #=> " 0000007b"
sprintf("%20.8b", 123) #=> " 01111011"
sprintf("%20.8d", -123) #=> " -00000123"
sprintf("%20.8o", -123) #=> " ..777605"
sprintf("%20.8x", -123) #=> " ..ffff85"
sprintf("%20.8b", -11) #=> " ..110101"
# "0x" and "0b" for `#x' and `#b' is not counted for
# precision but "0" for `#o' is counted. <------>
sprintf("%#20.8d", 123) #=> " 00000123"
sprintf("%#20.8o", 123) #=> " 00000173"
sprintf("%#20.8x", 123) #=> " 0x0000007b"
sprintf("%#20.8b", 123) #=> " 0b01111011"
sprintf("%#20.8d", -123) #=> " -00000123"
sprintf("%#20.8o", -123) #=> " ..777605"
sprintf("%#20.8x", -123) #=> " 0x..ffff85"
sprintf("%#20.8b", -11) #=> " 0b..110101"
# precision for `e' is number of
# digits after the decimal point <------>
sprintf("%20.8e", 1234.56789) #=> " 1.23456789e+03"
# precision for `f' is number of
# digits after the decimal point <------>
sprintf("%20.8f", 1234.56789) #=> " 1234.56789000"
# precision for `g' is number of
# significant digits <------->
sprintf("%20.8g", 1234.56789) #=> " 1234.5679"
# <------->
sprintf("%20.8g", 123456789) #=> " 1.2345679e+08"
# precision for `s' is
# maximum number of characters <------>
sprintf("%20.8s", "string test") #=> " string t"
Examples:
sprintf("%d %04x", 123, 123) #=> "123 007b"
sprintf("%08b '%4s'", 123, 123) #=> "01111011 ' 123'"
sprintf("%1$*2$s %2$d %1$s", "hello", 8) #=> " hello 8 hello"
sprintf("%1$*2$s %2$d", "hello", -8) #=> "hello -8"
sprintf("%+g:% g:%-g", 1.23, 1.23, 1.23) #=> "+1.23: 1.23:1.23"
sprintf("%u", -123) #=> "-123"
For more complex formatting, Ruby supports a reference by name.
%s style uses format style, but %{name} style doesn't.
Examples:
sprintf("%d : %f", { :foo => 1, :bar => 2 })
#=> 1 : 2.000000
sprintf("%{foo}f", { :foo => 1 })
# => "1f"
;T;[o;
;I"
overload;F;0;:format;0; I",format(format_string [, arguments...] );T;IC;" ;T;[o;!
;I"return;F;I" ;T;0;[I"String;T;"@;#[ ;$I"@return [String];T;%0;"@;&F;'i ;(0;[[I""format_string[, arguments...];T0;"@o;
;I"
overload;F;0;;;0; I"-sprintf(format_string [, arguments...] );T;IC;" ;T;[o;!
;I"return;F;I" ;T;0;[I"String;T;"@;#[ ;$I"@return [String];T;%0;"@;&F;'i ;(0;[[I""format_string[, arguments...];T0;"@;#[ ;$I",Returns the string resulting from applying format_string to
any additional arguments. Within the format string, any characters
other than format sequences are copied to the result.
The syntax of a format sequence is as follows.
%[flags][width][.precision]type
A format
sequence consists of a percent sign, followed by optional flags,
width, and precision indicators, then terminated with a field type
character. The field type controls how the corresponding
sprintf
argument is to be interpreted, while the flags
modify that interpretation.
The field type characters are:
Field | Integer Format
------+--------------------------------------------------------------
b | Convert argument as a binary number.
| Negative numbers will be displayed as a two's complement
| prefixed with `..1'.
B | Equivalent to `b', but uses an uppercase 0B for prefix
| in the alternative format by #.
d | Convert argument as a decimal number.
i | Identical to `d'.
o | Convert argument as an octal number.
| Negative numbers will be displayed as a two's complement
| prefixed with `..7'.
u | Identical to `d'.
x | Convert argument as a hexadecimal number.
| Negative numbers will be displayed as a two's complement
| prefixed with `..f' (representing an infinite string of
| leading 'ff's).
X | Equivalent to `x', but uses uppercase letters.
Field | Float Format
------+--------------------------------------------------------------
e | Convert floating point argument into exponential notation
| with one digit before the decimal point as [-]d.dddddde[+-]dd.
| The precision specifies the number of digits after the decimal
| point (defaulting to six).
E | Equivalent to `e', but uses an uppercase E to indicate
| the exponent.
f | Convert floating point argument as [-]ddd.dddddd,
| where the precision specifies the number of digits after
| the decimal point.
g | Convert a floating point number using exponential form
| if the exponent is less than -4 or greater than or
| equal to the precision, or in dd.dddd form otherwise.
| The precision specifies the number of significant digits.
G | Equivalent to `g', but use an uppercase `E' in exponent form.
a | Convert floating point argument as [-]0xh.hhhhp[+-]dd,
| which is consisted from optional sign, "0x", fraction part
| as hexadecimal, "p", and exponential part as decimal.
A | Equivalent to `a', but use uppercase `X' and `P'.
Field | Other Format
------+--------------------------------------------------------------
c | Argument is the numeric code for a single character or
| a single character string itself.
p | The valuing of argument.inspect.
s | Argument is a string to be substituted. If the format
| sequence contains a precision, at most that many characters
| will be copied.
% | A percent sign itself will be displayed. No argument taken.
The flags modifies the behavior of the formats.
The flag characters are:
Flag | Applies to | Meaning
---------+---------------+-----------------------------------------
space | bBdiouxX | Leave a space at the start of
| aAeEfgG | non-negative numbers.
| (numeric fmt) | For `o', `x', `X', `b' and `B', use
| | a minus sign with absolute value for
| | negative values.
---------+---------------+-----------------------------------------
(digit)$ | all | Specifies the absolute argument number
| | for this field. Absolute and relative
| | argument numbers cannot be mixed in a
| | sprintf string.
---------+---------------+-----------------------------------------
# | bBoxX | Use an alternative format.
| aAeEfgG | For the conversions `o', increase the precision
| | until the first digit will be `0' if
| | it is not formatted as complements.
| | For the conversions `x', `X', `b' and `B'
| | on non-zero, prefix the result with ``0x'',
| | ``0X'', ``0b'' and ``0B'', respectively.
| | For `a', `A', `e', `E', `f', `g', and 'G',
| | force a decimal point to be added,
| | even if no digits follow.
| | For `g' and 'G', do not remove trailing zeros.
---------+---------------+-----------------------------------------
+ | bBdiouxX | Add a leading plus sign to non-negative
| aAeEfgG | numbers.
| (numeric fmt) | For `o', `x', `X', `b' and `B', use
| | a minus sign with absolute value for
| | negative values.
---------+---------------+-----------------------------------------
- | all | Left-justify the result of this conversion.
---------+---------------+-----------------------------------------
0 (zero) | bBdiouxX | Pad with zeros, not spaces.
| aAeEfgG | For `o', `x', `X', `b' and `B', radix-1
| (numeric fmt) | is used for negative numbers formatted as
| | complements.
---------+---------------+-----------------------------------------
* | all | Use the next argument as the field width.
| | If negative, left-justify the result. If the
| | asterisk is followed by a number and a dollar
| | sign, use the indicated argument as the width.
Examples of flags:
# `+' and space flag specifies the sign of non-negative numbers.
sprintf("%d", 123) #=> "123"
sprintf("%+d", 123) #=> "+123"
sprintf("% d", 123) #=> " 123"
# `#' flag for `o' increases number of digits to show `0'.
# `+' and space flag changes format of negative numbers.
sprintf("%o", 123) #=> "173"
sprintf("%#o", 123) #=> "0173"
sprintf("%+o", -123) #=> "-173"
sprintf("%o", -123) #=> "..7605"
sprintf("%#o", -123) #=> "..7605"
# `#' flag for `x' add a prefix `0x' for non-zero numbers.
# `+' and space flag disables complements for negative numbers.
sprintf("%x", 123) #=> "7b"
sprintf("%#x", 123) #=> "0x7b"
sprintf("%+x", -123) #=> "-7b"
sprintf("%x", -123) #=> "..f85"
sprintf("%#x", -123) #=> "0x..f85"
sprintf("%#x", 0) #=> "0"
# `#' for `X' uses the prefix `0X'.
sprintf("%X", 123) #=> "7B"
sprintf("%#X", 123) #=> "0X7B"
# `#' flag for `b' add a prefix `0b' for non-zero numbers.
# `+' and space flag disables complements for negative numbers.
sprintf("%b", 123) #=> "1111011"
sprintf("%#b", 123) #=> "0b1111011"
sprintf("%+b", -123) #=> "-1111011"
sprintf("%b", -123) #=> "..10000101"
sprintf("%#b", -123) #=> "0b..10000101"
sprintf("%#b", 0) #=> "0"
# `#' for `B' uses the prefix `0B'.
sprintf("%B", 123) #=> "1111011"
sprintf("%#B", 123) #=> "0B1111011"
# `#' for `e' forces to show the decimal point.
sprintf("%.0e", 1) #=> "1e+00"
sprintf("%#.0e", 1) #=> "1.e+00"
# `#' for `f' forces to show the decimal point.
sprintf("%.0f", 1234) #=> "1234"
sprintf("%#.0f", 1234) #=> "1234."
# `#' for `g' forces to show the decimal point.
# It also disables stripping lowest zeros.
sprintf("%g", 123.4) #=> "123.4"
sprintf("%#g", 123.4) #=> "123.400"
sprintf("%g", 123456) #=> "123456"
sprintf("%#g", 123456) #=> "123456."
The field width is an optional integer, followed optionally by a
period and a precision. The width specifies the minimum number of
characters that will be written to the result for this field.
Examples of width:
# padding is done by spaces, width=20
# 0 or radix-1. <------------------>
sprintf("%20d", 123) #=> " 123"
sprintf("%+20d", 123) #=> " +123"
sprintf("%020d", 123) #=> "00000000000000000123"
sprintf("%+020d", 123) #=> "+0000000000000000123"
sprintf("% 020d", 123) #=> " 0000000000000000123"
sprintf("%-20d", 123) #=> "123 "
sprintf("%-+20d", 123) #=> "+123 "
sprintf("%- 20d", 123) #=> " 123 "
sprintf("%020x", -123) #=> "..ffffffffffffffff85"
For
numeric fields, the precision controls the number of decimal places
displayed. For string fields, the precision determines the maximum
number of characters to be copied from the string. (Thus, the format
sequence %10.10s
will always contribute exactly ten
characters to the result.)
Examples of precisions:
# precision for `d', 'o', 'x' and 'b' is
# minimum number of digits <------>
sprintf("%20.8d", 123) #=> " 00000123"
sprintf("%20.8o", 123) #=> " 00000173"
sprintf("%20.8x", 123) #=> " 0000007b"
sprintf("%20.8b", 123) #=> " 01111011"
sprintf("%20.8d", -123) #=> " -00000123"
sprintf("%20.8o", -123) #=> " ..777605"
sprintf("%20.8x", -123) #=> " ..ffff85"
sprintf("%20.8b", -11) #=> " ..110101"
# "0x" and "0b" for `#x' and `#b' is not counted for
# precision but "0" for `#o' is counted. <------>
sprintf("%#20.8d", 123) #=> " 00000123"
sprintf("%#20.8o", 123) #=> " 00000173"
sprintf("%#20.8x", 123) #=> " 0x0000007b"
sprintf("%#20.8b", 123) #=> " 0b01111011"
sprintf("%#20.8d", -123) #=> " -00000123"
sprintf("%#20.8o", -123) #=> " ..777605"
sprintf("%#20.8x", -123) #=> " 0x..ffff85"
sprintf("%#20.8b", -11) #=> " 0b..110101"
# precision for `e' is number of
# digits after the decimal point <------>
sprintf("%20.8e", 1234.56789) #=> " 1.23456789e+03"
# precision for `f' is number of
# digits after the decimal point <------>
sprintf("%20.8f", 1234.56789) #=> " 1234.56789000"
# precision for `g' is number of
# significant digits <------->
sprintf("%20.8g", 1234.56789) #=> " 1234.5679"
# <------->
sprintf("%20.8g", 123456789) #=> " 1.2345679e+08"
# precision for `s' is
# maximum number of characters <------>
sprintf("%20.8s", "string test") #=> " string t"
Examples:
sprintf("%d %04x", 123, 123) #=> "123 007b"
sprintf("%08b '%4s'", 123, 123) #=> "01111011 ' 123'"
sprintf("%1$*2$s %2$d %1$s", "hello", 8) #=> " hello 8 hello"
sprintf("%1$*2$s %2$d", "hello", -8) #=> "hello -8"
sprintf("%+g:% g:%-g", 1.23, 1.23, 1.23) #=> "+1.23: 1.23:1.23"
sprintf("%u", -123) #=> "-123"
For more complex formatting, Ruby supports a reference by name.
%s style uses format style, but %{name} style doesn't.
Examples:
sprintf("%d : %f", { :foo => 1, :bar => 2 })
#=> 1 : 2.000000
sprintf("%{foo}f", { :foo => 1 })
# => "1f"
@overload format(format_string [, arguments...] )
@return [String]
@overload sprintf(format_string [, arguments...] )
@return [String];T;%0;"@;&F;+o;,;-T;.i;/i;0@z;1I"^static VALUE
f_sprintf(int c, const VALUE *v, VALUE _)
{
return rb_f_sprintf(c, v);
};T; I"static VALUE;T;2To;
;F;;
;;;I"Kernel#format;F;[[I"*v;T0[I"_;T0;[[@
i;T;;;0;[ ;{ ;IC;"F,Returns the string resulting from applying format_string to
any additional arguments. Within the format string, any characters
other than format sequences are copied to the result.
The syntax of a format sequence is as follows.
%[flags][width][.precision]type
A format
sequence consists of a percent sign, followed by optional flags,
width, and precision indicators, then terminated with a field type
character. The field type controls how the corresponding
sprintf
argument is to be interpreted, while the flags
modify that interpretation.
The field type characters are:
Field | Integer Format
------+--------------------------------------------------------------
b | Convert argument as a binary number.
| Negative numbers will be displayed as a two's complement
| prefixed with `..1'.
B | Equivalent to `b', but uses an uppercase 0B for prefix
| in the alternative format by #.
d | Convert argument as a decimal number.
i | Identical to `d'.
o | Convert argument as an octal number.
| Negative numbers will be displayed as a two's complement
| prefixed with `..7'.
u | Identical to `d'.
x | Convert argument as a hexadecimal number.
| Negative numbers will be displayed as a two's complement
| prefixed with `..f' (representing an infinite string of
| leading 'ff's).
X | Equivalent to `x', but uses uppercase letters.
Field | Float Format
------+--------------------------------------------------------------
e | Convert floating point argument into exponential notation
| with one digit before the decimal point as [-]d.dddddde[+-]dd.
| The precision specifies the number of digits after the decimal
| point (defaulting to six).
E | Equivalent to `e', but uses an uppercase E to indicate
| the exponent.
f | Convert floating point argument as [-]ddd.dddddd,
| where the precision specifies the number of digits after
| the decimal point.
g | Convert a floating point number using exponential form
| if the exponent is less than -4 or greater than or
| equal to the precision, or in dd.dddd form otherwise.
| The precision specifies the number of significant digits.
G | Equivalent to `g', but use an uppercase `E' in exponent form.
a | Convert floating point argument as [-]0xh.hhhhp[+-]dd,
| which is consisted from optional sign, "0x", fraction part
| as hexadecimal, "p", and exponential part as decimal.
A | Equivalent to `a', but use uppercase `X' and `P'.
Field | Other Format
------+--------------------------------------------------------------
c | Argument is the numeric code for a single character or
| a single character string itself.
p | The valuing of argument.inspect.
s | Argument is a string to be substituted. If the format
| sequence contains a precision, at most that many characters
| will be copied.
% | A percent sign itself will be displayed. No argument taken.
The flags modifies the behavior of the formats.
The flag characters are:
Flag | Applies to | Meaning
---------+---------------+-----------------------------------------
space | bBdiouxX | Leave a space at the start of
| aAeEfgG | non-negative numbers.
| (numeric fmt) | For `o', `x', `X', `b' and `B', use
| | a minus sign with absolute value for
| | negative values.
---------+---------------+-----------------------------------------
(digit)$ | all | Specifies the absolute argument number
| | for this field. Absolute and relative
| | argument numbers cannot be mixed in a
| | sprintf string.
---------+---------------+-----------------------------------------
# | bBoxX | Use an alternative format.
| aAeEfgG | For the conversions `o', increase the precision
| | until the first digit will be `0' if
| | it is not formatted as complements.
| | For the conversions `x', `X', `b' and `B'
| | on non-zero, prefix the result with ``0x'',
| | ``0X'', ``0b'' and ``0B'', respectively.
| | For `a', `A', `e', `E', `f', `g', and 'G',
| | force a decimal point to be added,
| | even if no digits follow.
| | For `g' and 'G', do not remove trailing zeros.
---------+---------------+-----------------------------------------
+ | bBdiouxX | Add a leading plus sign to non-negative
| aAeEfgG | numbers.
| (numeric fmt) | For `o', `x', `X', `b' and `B', use
| | a minus sign with absolute value for
| | negative values.
---------+---------------+-----------------------------------------
- | all | Left-justify the result of this conversion.
---------+---------------+-----------------------------------------
0 (zero) | bBdiouxX | Pad with zeros, not spaces.
| aAeEfgG | For `o', `x', `X', `b' and `B', radix-1
| (numeric fmt) | is used for negative numbers formatted as
| | complements.
---------+---------------+-----------------------------------------
* | all | Use the next argument as the field width.
| | If negative, left-justify the result. If the
| | asterisk is followed by a number and a dollar
| | sign, use the indicated argument as the width.
Examples of flags:
# `+' and space flag specifies the sign of non-negative numbers.
sprintf("%d", 123) #=> "123"
sprintf("%+d", 123) #=> "+123"
sprintf("% d", 123) #=> " 123"
# `#' flag for `o' increases number of digits to show `0'.
# `+' and space flag changes format of negative numbers.
sprintf("%o", 123) #=> "173"
sprintf("%#o", 123) #=> "0173"
sprintf("%+o", -123) #=> "-173"
sprintf("%o", -123) #=> "..7605"
sprintf("%#o", -123) #=> "..7605"
# `#' flag for `x' add a prefix `0x' for non-zero numbers.
# `+' and space flag disables complements for negative numbers.
sprintf("%x", 123) #=> "7b"
sprintf("%#x", 123) #=> "0x7b"
sprintf("%+x", -123) #=> "-7b"
sprintf("%x", -123) #=> "..f85"
sprintf("%#x", -123) #=> "0x..f85"
sprintf("%#x", 0) #=> "0"
# `#' for `X' uses the prefix `0X'.
sprintf("%X", 123) #=> "7B"
sprintf("%#X", 123) #=> "0X7B"
# `#' flag for `b' add a prefix `0b' for non-zero numbers.
# `+' and space flag disables complements for negative numbers.
sprintf("%b", 123) #=> "1111011"
sprintf("%#b", 123) #=> "0b1111011"
sprintf("%+b", -123) #=> "-1111011"
sprintf("%b", -123) #=> "..10000101"
sprintf("%#b", -123) #=> "0b..10000101"
sprintf("%#b", 0) #=> "0"
# `#' for `B' uses the prefix `0B'.
sprintf("%B", 123) #=> "1111011"
sprintf("%#B", 123) #=> "0B1111011"
# `#' for `e' forces to show the decimal point.
sprintf("%.0e", 1) #=> "1e+00"
sprintf("%#.0e", 1) #=> "1.e+00"
# `#' for `f' forces to show the decimal point.
sprintf("%.0f", 1234) #=> "1234"
sprintf("%#.0f", 1234) #=> "1234."
# `#' for `g' forces to show the decimal point.
# It also disables stripping lowest zeros.
sprintf("%g", 123.4) #=> "123.4"
sprintf("%#g", 123.4) #=> "123.400"
sprintf("%g", 123456) #=> "123456"
sprintf("%#g", 123456) #=> "123456."
The field width is an optional integer, followed optionally by a
period and a precision. The width specifies the minimum number of
characters that will be written to the result for this field.
Examples of width:
# padding is done by spaces, width=20
# 0 or radix-1. <------------------>
sprintf("%20d", 123) #=> " 123"
sprintf("%+20d", 123) #=> " +123"
sprintf("%020d", 123) #=> "00000000000000000123"
sprintf("%+020d", 123) #=> "+0000000000000000123"
sprintf("% 020d", 123) #=> " 0000000000000000123"
sprintf("%-20d", 123) #=> "123 "
sprintf("%-+20d", 123) #=> "+123 "
sprintf("%- 20d", 123) #=> " 123 "
sprintf("%020x", -123) #=> "..ffffffffffffffff85"
For
numeric fields, the precision controls the number of decimal places
displayed. For string fields, the precision determines the maximum
number of characters to be copied from the string. (Thus, the format
sequence %10.10s
will always contribute exactly ten
characters to the result.)
Examples of precisions:
# precision for `d', 'o', 'x' and 'b' is
# minimum number of digits <------>
sprintf("%20.8d", 123) #=> " 00000123"
sprintf("%20.8o", 123) #=> " 00000173"
sprintf("%20.8x", 123) #=> " 0000007b"
sprintf("%20.8b", 123) #=> " 01111011"
sprintf("%20.8d", -123) #=> " -00000123"
sprintf("%20.8o", -123) #=> " ..777605"
sprintf("%20.8x", -123) #=> " ..ffff85"
sprintf("%20.8b", -11) #=> " ..110101"
# "0x" and "0b" for `#x' and `#b' is not counted for
# precision but "0" for `#o' is counted. <------>
sprintf("%#20.8d", 123) #=> " 00000123"
sprintf("%#20.8o", 123) #=> " 00000173"
sprintf("%#20.8x", 123) #=> " 0x0000007b"
sprintf("%#20.8b", 123) #=> " 0b01111011"
sprintf("%#20.8d", -123) #=> " -00000123"
sprintf("%#20.8o", -123) #=> " ..777605"
sprintf("%#20.8x", -123) #=> " 0x..ffff85"
sprintf("%#20.8b", -11) #=> " 0b..110101"
# precision for `e' is number of
# digits after the decimal point <------>
sprintf("%20.8e", 1234.56789) #=> " 1.23456789e+03"
# precision for `f' is number of
# digits after the decimal point <------>
sprintf("%20.8f", 1234.56789) #=> " 1234.56789000"
# precision for `g' is number of
# significant digits <------->
sprintf("%20.8g", 1234.56789) #=> " 1234.5679"
# <------->
sprintf("%20.8g", 123456789) #=> " 1.2345679e+08"
# precision for `s' is
# maximum number of characters <------>
sprintf("%20.8s", "string test") #=> " string t"
Examples:
sprintf("%d %04x", 123, 123) #=> "123 007b"
sprintf("%08b '%4s'", 123, 123) #=> "01111011 ' 123'"
sprintf("%1$*2$s %2$d %1$s", "hello", 8) #=> " hello 8 hello"
sprintf("%1$*2$s %2$d", "hello", -8) #=> "hello -8"
sprintf("%+g:% g:%-g", 1.23, 1.23, 1.23) #=> "+1.23: 1.23:1.23"
sprintf("%u", -123) #=> "-123"
For more complex formatting, Ruby supports a reference by name.
%s style uses format style, but %{name} style doesn't.
Examples:
sprintf("%d : %f", { :foo => 1, :bar => 2 })
#=> 1 : 2.000000
sprintf("%{foo}f", { :foo => 1 })
# => "1f"
;T;[o;
;I"
overload;F;0;;;0; I",format(format_string [, arguments...] );T;IC;" ;T;[o;!
;I"return;F;I" ;T;0;[I"String;T;"@;#[ ;$I"@return [String];T;%0;"@;&F;'i ;(0;[[I""format_string[, arguments...];T0;"@o;
;I"
overload;F;0;;;0; I"-sprintf(format_string [, arguments...] );T;IC;" ;T;[o;!
;I"return;F;I" ;T;0;[I"String;T;"@;#[ ;$I"@return [String];T;%0;"@;&F;'i ;(0;[[I""format_string[, arguments...];T0;"@;#[ ;$@;%0;"@;&F;+o;,;-T;.i;/i;0@z;1I"^static VALUE
f_sprintf(int c, const VALUE *v, VALUE _)
{
return rb_f_sprintf(c, v);
};T; I"static VALUE;T;2To;
;F;;
;;;I"Kernel#Integer;F;[[@0;[[@
iZ
;T;:Integer;0;[ ;{ ;IC;"=Converts arg to an Integer.
Numeric types are converted directly (with floating point numbers
being truncated). base (0, or between 2 and 36) is a base for
integer string representation. If arg is a String,
when base is omitted or equals zero, radix indicators
(0
, 0b
, and 0x
) are honored.
In any case, strings should consist only of one or more digits, except
for that a sign, one underscore between two digits, and leading/trailing
spaces are optional. This behavior is different from that of
String#to_i. Non string values will be converted by first
trying to_int
, then to_i
.
Passing nil
raises a TypeError, while passing a String that
does not conform with numeric representation raises an ArgumentError.
This behavior can be altered by passing exception: false
,
in this case a not convertible value will return nil
.
Integer(123.999) #=> 123
Integer("0x1a") #=> 26
Integer(Time.new) #=> 1204973019
Integer("0930", 10) #=> 930
Integer("111", 2) #=> 7
Integer(" +1_0 ") #=> 10
Integer(nil) #=> TypeError: can't convert nil into Integer
Integer("x") #=> ArgumentError: invalid value for Integer(): "x"
Integer("x", exception: false) #=> nil
;T;[o;
;I"
overload;F;0;;;0; I"*Integer(arg, base=0, exception: true);T;IC;" ;T;[o;!
;I"return;F;I" ;T;0;[I"Integer;TI"nil;T;"@;#[ ;$I"@return [Integer, nil];T;%0;"@;&F;'i ;(0;[[I"arg;T0[I" base;TI"0;T[I"exception:;TI" true;T;"@;#[ ;$I"Converts arg to an Integer.
Numeric types are converted directly (with floating point numbers
being truncated). base (0, or between 2 and 36) is a base for
integer string representation. If arg is a String,
when base is omitted or equals zero, radix indicators
(0
, 0b
, and 0x
) are honored.
In any case, strings should consist only of one or more digits, except
for that a sign, one underscore between two digits, and leading/trailing
spaces are optional. This behavior is different from that of
String#to_i. Non string values will be converted by first
trying to_int
, then to_i
.
Passing nil
raises a TypeError, while passing a String that
does not conform with numeric representation raises an ArgumentError.
This behavior can be altered by passing exception: false
,
in this case a not convertible value will return nil
.
Integer(123.999) #=> 123
Integer("0x1a") #=> 26
Integer(Time.new) #=> 1204973019
Integer("0930", 10) #=> 930
Integer("111", 2) #=> 7
Integer(" +1_0 ") #=> 10
Integer(nil) #=> TypeError: can't convert nil into Integer
Integer("x") #=> ArgumentError: invalid value for Integer(): "x"
Integer("x", exception: false) #=> nil
@overload Integer(arg, base=0, exception: true)
@return [Integer, nil];T;%0;"@;&F;+o;,;-T;.i8
;/iW
;0@z;1I"static VALUE
rb_f_integer(int argc, VALUE *argv, VALUE obj)
{
VALUE arg = Qnil, opts = Qnil;
int base = 0;
if (argc > 1) {
int narg = 1;
VALUE vbase = rb_check_to_int(argv[1]);
if (!NIL_P(vbase)) {
base = NUM2INT(vbase);
narg = 2;
}
if (argc > narg) {
VALUE hash = rb_check_hash_type(argv[argc-1]);
if (!NIL_P(hash)) {
opts = rb_extract_keywords(&hash);
if (!hash) --argc;
}
}
}
rb_check_arity(argc, 1, 2);
arg = argv[0];
return rb_convert_to_integer(arg, base, opts_exception_p(opts));
};T; I"static VALUE;T;2To;
;F;;
;;;I"Kernel#String;F;[[I"arg;T0;[[@
iG;T;:String;0;[ ;{ ;IC;"Returns arg as a String.
First tries to call its to_str
method, then its to_s
method.
String(self) #=> "main"
String(self.class) #=> "Object"
String(123456) #=> "123456"
;T;[o;
;I"
overload;F;0;;;0; I"String(arg);T;IC;" ;T;[o;!
;I"return;F;I" ;T;0;[I"String;T;"@;#[ ;$I"@return [String];T;%0;"@;&F;'i ;(0;[[I"arg;T0;"@;#[ ;$I"
Returns arg as a String.
First tries to call its to_str
method, then its to_s
method.
String(self) #=> "main"
String(self.class) #=> "Object"
String(123456) #=> "123456"
@overload String(arg)
@return [String];T;%0;"@;&F;+o;,;-T;.i:;/iD;0@z;1I"Rstatic VALUE
rb_f_string(VALUE obj, VALUE arg)
{
return rb_String(arg);
};T; I"static VALUE;T;2To;
;F;;
;;;I"Kernel#Array;F;[[I"arg;T0;[[@
ir;T;:
Array;0;[ ;{ ;IC;"Returns +arg+ as an Array.
First tries to call to_ary
on +arg+, then to_a
.
If +arg+ does not respond to to_ary
or to_a
,
returns an Array of length 1 containing +arg+.
If to_ary
or to_a
returns something other than
an Array, raises a TypeError.
Array(["a", "b"]) #=> ["a", "b"]
Array(1..5) #=> [1, 2, 3, 4, 5]
Array(key: :value) #=> [[:key, :value]]
Array(nil) #=> []
Array(1) #=> [1]
;T;[o;
;I"
overload;F;0;;;0; I"Array(arg);T;IC;" ;T;[o;!
;I"return;F;I" ;T;0;[I"
Array;T;"@3;#[ ;$I"@return [Array];T;%0;"@3;&F;'i ;(0;[[I"arg;T0;"@3;#[ ;$I""Returns +arg+ as an Array.
First tries to call to_ary
on +arg+, then to_a
.
If +arg+ does not respond to to_ary
or to_a
,
returns an Array of length 1 containing +arg+.
If to_ary
or to_a
returns something other than
an Array, raises a TypeError.
Array(["a", "b"]) #=> ["a", "b"]
Array(1..5) #=> [1, 2, 3, 4, 5]
Array(key: :value) #=> [[:key, :value]]
Array(nil) #=> []
Array(1) #=> [1]
@overload Array(arg)
@return [Array];T;%0;"@3;&F;+o;,;-T;.i^;/io;0@z;1I"Pstatic VALUE
rb_f_array(VALUE obj, VALUE arg)
{
return rb_Array(arg);
};T; I"static VALUE;T;2To;
;F;;
;;;I"Kernel#Hash;F;[[I"arg;T0;[[@
i;T;: Hash;0;[ ;{ ;IC;"Converts arg to a Hash by calling
arg.to_hash
. Returns an empty Hash when
arg is nil or [].
Hash([]) #=> {}
Hash(nil) #=> {}
Hash(key: :value) #=> {:key => :value}
Hash([1, 2, 3]) #=> TypeError
;T;[o;
;I"
overload;F;0;;;0; I"Hash(arg);T;IC;" ;T;[o;!
;I"return;F;I" ;T;0;[I" Hash;T;"@R;#[ ;$I"@return [Hash];T;%0;"@R;&F;'i ;(0;[[I"arg;T0;"@R;#[ ;$I"<Converts arg to a Hash by calling
arg.to_hash
. Returns an empty Hash when
arg is nil or [].
Hash([]) #=> {}
Hash(nil) #=> {}
Hash(key: :value) #=> {:key => :value}
Hash([1, 2, 3]) #=> TypeError
@overload Hash(arg)
@return [Hash];T;%0;"@R;&F;+o;,;-T;.i;/i;0@z;1I"Nstatic VALUE
rb_f_hash(VALUE obj, VALUE arg)
{
return rb_Hash(arg);
};T; I"static VALUE;T;2To;
;F;;
;;;I"Kernel#syscall;F;[[@0;[[@i';T;:syscall;0;[ ;{ ;IC;"Calls the operating system function identified by _num_ and
returns the result of the function or raises SystemCallError if
it failed.
Arguments for the function can follow _num_. They must be either
+String+ objects or +Integer+ objects. A +String+ object is passed
as a pointer to the byte sequence. An +Integer+ object is passed
as an integer whose bit size is same as a pointer.
Up to nine parameters may be passed.
The function identified by _num_ is system
dependent. On some Unix systems, the numbers may be obtained from a
header file called syscall.h
.
syscall 4, 1, "hello\n", 6 # '4' is write(2) on our box
produces:
hello
Calling +syscall+ on a platform which does not have any way to
an arbitrary system function just fails with NotImplementedError.
*Note:*
+syscall+ is essentially unsafe and unportable.
Feel free to shoot your foot.
The DL (Fiddle) library is preferred for safer and a bit
more portable programming.
;T;[o;
;I"
overload;F;0;;;0; I"syscall(num [, args...]);T;IC;" ;T;[o;!
;I"return;F;I" ;T;0;[I"Integer;T;"@q;#[ ;$I"@return [Integer];T;%0;"@q;&F;'i ;(0;[[I"num[, args...];T0;"@q;#[ ;$I"Calls the operating system function identified by _num_ and
returns the result of the function or raises SystemCallError if
it failed.
Arguments for the function can follow _num_. They must be either
+String+ objects or +Integer+ objects. A +String+ object is passed
as a pointer to the byte sequence. An +Integer+ object is passed
as an integer whose bit size is same as a pointer.
Up to nine parameters may be passed.
The function identified by _num_ is system
dependent. On some Unix systems, the numbers may be obtained from a
header file called syscall.h
.
syscall 4, 1, "hello\n", 6 # '4' is write(2) on our box
produces:
hello
Calling +syscall+ on a platform which does not have any way to
an arbitrary system function just fails with NotImplementedError.
*Note:*
+syscall+ is essentially unsafe and unportable.
Feel free to shoot your foot.
The DL (Fiddle) library is preferred for safer and a bit
more portable programming.
@overload syscall(num [, args...])
@return [Integer];T;%0;"@q;&F;+o;,;-T;.i';/i';0@z;1I"e static VALUE
rb_f_syscall(int argc, VALUE *argv, VALUE _)
{
VALUE arg[8];
#if SIZEOF_VOIDP == 8 && defined(HAVE___SYSCALL) && SIZEOF_INT != 8 /* mainly *BSD */
# define SYSCALL __syscall
# define NUM2SYSCALLID(x) NUM2LONG(x)
# define RETVAL2NUM(x) LONG2NUM(x)
# if SIZEOF_LONG == 8
long num, retval = -1;
# elif SIZEOF_LONG_LONG == 8
long long num, retval = -1;
# else
# error ---->> it is asserted that __syscall takes the first argument and returns retval in 64bit signed integer. <<----
# endif
#elif defined(__linux__)
# define SYSCALL syscall
# define NUM2SYSCALLID(x) NUM2LONG(x)
# define RETVAL2NUM(x) LONG2NUM(x)
/*
* Linux man page says, syscall(2) function prototype is below.
*
* int syscall(int number, ...);
*
* But, it's incorrect. Actual one takes and returned long. (see unistd.h)
*/
long num, retval = -1;
#else
# define SYSCALL syscall
# define NUM2SYSCALLID(x) NUM2INT(x)
# define RETVAL2NUM(x) INT2NUM(x)
int num, retval = -1;
#endif
int i;
if (RTEST(ruby_verbose)) {
rb_category_warning(RB_WARN_CATEGORY_DEPRECATED,
"We plan to remove a syscall function at future release. DL(Fiddle) provides safer alternative.");
}
if (argc == 0)
rb_raise(rb_eArgError, "too few arguments for syscall");
if (argc > numberof(arg))
rb_raise(rb_eArgError, "too many arguments for syscall");
num = NUM2SYSCALLID(argv[0]); ++argv;
for (i = argc - 1; i--; ) {
VALUE v = rb_check_string_type(argv[i]);
if (!NIL_P(v)) {
SafeStringValue(v);
rb_str_modify(v);
arg[i] = (VALUE)StringValueCStr(v);
}
else {
arg[i] = (VALUE)NUM2LONG(argv[i]);
}
}
switch (argc) {
case 1:
retval = SYSCALL(num);
break;
case 2:
retval = SYSCALL(num, arg[0]);
break;
case 3:
retval = SYSCALL(num, arg[0],arg[1]);
break;
case 4:
retval = SYSCALL(num, arg[0],arg[1],arg[2]);
break;
case 5:
retval = SYSCALL(num, arg[0],arg[1],arg[2],arg[3]);
break;
case 6:
retval = SYSCALL(num, arg[0],arg[1],arg[2],arg[3],arg[4]);
break;
case 7:
retval = SYSCALL(num, arg[0],arg[1],arg[2],arg[3],arg[4],arg[5]);
break;
case 8:
retval = SYSCALL(num, arg[0],arg[1],arg[2],arg[3],arg[4],arg[5],arg[6]);
break;
}
if (retval == -1)
rb_sys_fail(0);
return RETVAL2NUM(retval);
#undef SYSCALL
#undef NUM2SYSCALLID
#undef RETVAL2NUM
};T; I"static VALUE;T;2To;
;F;;
;;;I"Kernel#open;F;[[@0;[[@i;T;: open;0;[ ;{ ;IC;" Creates an IO object connected to the given stream, file, or subprocess.
If +path+ does not start with a pipe character (|
), treat it
as the name of a file to open using the specified mode (defaulting to
"r").
The +mode+ is either a string or an integer. If it is an integer, it
must be bitwise-or of open(2) flags, such as File::RDWR or File::EXCL. If
it is a string, it is either "fmode", "fmode:ext_enc", or
"fmode:ext_enc:int_enc".
See the documentation of IO.new for full documentation of the +mode+ string
directives.
If a file is being created, its initial permissions may be set using the
+perm+ parameter. See File.new and the open(2) and chmod(2) man pages for
a description of permissions.
If a block is specified, it will be invoked with the IO object as a
parameter, and the IO will be automatically closed when the block
terminates. The call returns the value of the block.
If +path+ starts with a pipe character ("|"
), a subprocess is
created, connected to the caller by a pair of pipes. The returned IO
object may be used to write to the standard input and read from the
standard output of this subprocess.
If the command following the pipe is a single minus sign
("|-"
), Ruby forks, and this subprocess is connected to the
parent. If the command is not "-"
, the subprocess runs the
command.
When the subprocess is Ruby (opened via "|-"
), the +open+
call returns +nil+. If a block is associated with the open call, that
block will run twice --- once in the parent and once in the child.
The block parameter will be an IO object in the parent and +nil+ in the
child. The parent's +IO+ object will be connected to the child's $stdin
and $stdout. The subprocess will be terminated at the end of the block.
=== Examples
Reading from "testfile":
open("testfile") do |f|
print f.gets
end
Produces:
This is line one
Open a subprocess and read its output:
cmd = open("|date")
print cmd.gets
cmd.close
Produces:
Wed Apr 9 08:56:31 CDT 2003
Open a subprocess running the same Ruby program:
f = open("|-", "w+")
if f.nil?
puts "in Child"
exit
else
puts "Got: #{f.gets}"
end
Produces:
Got: in Child
Open a subprocess using a block to receive the IO object:
open "|-" do |f|
if f then
# parent process
puts "Got: #{f.gets}"
else
# child process
puts "in Child"
end
end
Produces:
Got: in Child
;T;[o;
;I"
overload;F;0;;;0; I")open(path [, mode [, perm]] [, opt]);T;IC;" ;T;[o;!
;I"return;F;I" ;T;0;[I"IO;TI"nil;T;"@;#[ ;$I"@return [IO, nil];T;%0;"@;&F;'i ;(0;[[I"!path[, mode [, perm]][, opt];T0;"@o;
;I"
overload;F;0;;;0; I")open(path [, mode [, perm]] [, opt]);T;IC;" ;T;[o;!
;I"
yield;F;I" ;T;0;[I"io;T;"@o;!
;I"return;F;I" ;T;0;[I"Object;T;"@;#[ ;$I"!@yield [io]
@return [Object];T;%0;"@;&F;'i ;(0;[[I"!path[, mode [, perm]][, opt];T0;"@;#[ ;$I"\
Creates an IO object connected to the given stream, file, or subprocess.
If +path+ does not start with a pipe character (|
), treat it
as the name of a file to open using the specified mode (defaulting to
"r").
The +mode+ is either a string or an integer. If it is an integer, it
must be bitwise-or of open(2) flags, such as File::RDWR or File::EXCL. If
it is a string, it is either "fmode", "fmode:ext_enc", or
"fmode:ext_enc:int_enc".
See the documentation of IO.new for full documentation of the +mode+ string
directives.
If a file is being created, its initial permissions may be set using the
+perm+ parameter. See File.new and the open(2) and chmod(2) man pages for
a description of permissions.
If a block is specified, it will be invoked with the IO object as a
parameter, and the IO will be automatically closed when the block
terminates. The call returns the value of the block.
If +path+ starts with a pipe character ("|"
), a subprocess is
created, connected to the caller by a pair of pipes. The returned IO
object may be used to write to the standard input and read from the
standard output of this subprocess.
If the command following the pipe is a single minus sign
("|-"
), Ruby forks, and this subprocess is connected to the
parent. If the command is not "-"
, the subprocess runs the
command.
When the subprocess is Ruby (opened via "|-"
), the +open+
call returns +nil+. If a block is associated with the open call, that
block will run twice --- once in the parent and once in the child.
The block parameter will be an IO object in the parent and +nil+ in the
child. The parent's +IO+ object will be connected to the child's $stdin
and $stdout. The subprocess will be terminated at the end of the block.
=== Examples
Reading from "testfile":
open("testfile") do |f|
print f.gets
end
Produces:
This is line one
Open a subprocess and read its output:
cmd = open("|date")
print cmd.gets
cmd.close
Produces:
Wed Apr 9 08:56:31 CDT 2003
Open a subprocess running the same Ruby program:
f = open("|-", "w+")
if f.nil?
puts "in Child"
exit
else
puts "Got: #{f.gets}"
end
Produces:
Got: in Child
Open a subprocess using a block to receive the IO object:
open "|-" do |f|
if f then
# parent process
puts "Got: #{f.gets}"
else
# child process
puts "in Child"
end
end
Produces:
Got: in Child
@overload open(path [, mode [, perm]] [, opt])
@return [IO, nil]
@overload open(path [, mode [, perm]] [, opt])
@yield [io]
@return [Object];T;%0;"@;&F;+o;,;-T;.i5;/i;0@z;1I"static VALUE
rb_f_open(int argc, VALUE *argv, VALUE _)
{
ID to_open = 0;
int redirect = FALSE;
if (argc >= 1) {
CONST_ID(to_open, "to_open");
if (rb_respond_to(argv[0], to_open)) {
redirect = TRUE;
}
else {
VALUE tmp = argv[0];
FilePathValue(tmp);
if (NIL_P(tmp)) {
redirect = TRUE;
}
else {
VALUE cmd = check_pipe_command(tmp);
if (!NIL_P(cmd)) {
argv[0] = cmd;
return rb_io_s_popen(argc, argv, rb_cIO);
}
}
}
}
if (redirect) {
VALUE io = rb_funcallv_kw(argv[0], to_open, argc-1, argv+1, RB_PASS_CALLED_KEYWORDS);
if (rb_block_given_p()) {
return rb_ensure(rb_yield, io, io_close, io);
}
return io;
}
return rb_io_s_open(argc, argv, rb_cFile);
};T; I"static VALUE;T;2To;
;F;;
;;;I"Kernel#printf;F;[[@0;[[@i;T;:printf;0;[ ;{ ;IC;"iEquivalent to:
io.write(sprintf(string, obj, ...))
or
$stdout.write(sprintf(string, obj, ...))
;T;[o;
;I"
overload;F;0;;;0; I"$printf(io, string [, obj ... ]);T;IC;" ;T;[o;!
;I"return;F;I" ;T;0;[I"nil;T;"@;#[ ;$I"@return [nil];T;%0;"@;&F;'i ;(0;[[I"io;T0[I"string[, obj ... ];T0;"@o;
;I"
overload;F;0;;;0; I" printf(string [, obj ... ]);T;IC;" ;T;[o;!
;I"return;F;I" ;T;0;[I"nil;T;"@;#[ ;$I"@return [nil];T;%0;"@;&F;'i ;(0;[[I"string[, obj ... ];T0;"@;#[ ;$I"Equivalent to:
io.write(sprintf(string, obj, ...))
or
$stdout.write(sprintf(string, obj, ...))
@overload printf(io, string [, obj ... ])
@return [nil]
@overload printf(string [, obj ... ])
@return [nil];T;%0;"@;&F;+o;,;-T;.i;/i;0@z;1I"*static VALUE
rb_f_printf(int argc, VALUE *argv, VALUE _)
{
VALUE out;
if (argc == 0) return Qnil;
if (RB_TYPE_P(argv[0], T_STRING)) {
out = rb_ractor_stdout();
}
else {
out = argv[0];
argv++;
argc--;
}
rb_io_write(out, rb_f_sprintf(argc, argv));
return Qnil;
};T; I"static VALUE;T;2To;
;F;;
;;;I"Kernel#print;F;[[@0;[[@iN;T;:
print;0;[ ;{ ;IC;"Prints each object in turn to $stdout
. If the output
field separator ($,
) is not +nil+, its
contents will appear between each field. If the output record
separator ($\\
) is not +nil+, it will be
appended to the output. If no arguments are given, prints
$_
. Objects that aren't strings will be converted by
calling their to_s
method.
print "cat", [1,2,3], 99, "\n"
$, = ", "
$\ = "\n"
print "cat", [1,2,3], 99
produces:
cat12399
cat, 1, 2, 3, 99
;T;[o;
;I"
overload;F;0;;;0; I"print(obj, ...);T;IC;" ;T;[o;!
;I"return;F;I" ;T;0;[I"nil;T;"@;#[ ;$I"@return [nil];T;%0;"@;&F;'i ;(0;[[I"obj;T0[I"...;T0;"@;#[ ;$I"GPrints each object in turn to $stdout
. If the output
field separator ($,
) is not +nil+, its
contents will appear between each field. If the output record
separator ($\\
) is not +nil+, it will be
appended to the output. If no arguments are given, prints
$_
. Objects that aren't strings will be converted by
calling their to_s
method.
print "cat", [1,2,3], 99, "\n"
$, = ", "
$\ = "\n"
print "cat", [1,2,3], 99
produces:
cat12399
cat, 1, 2, 3, 99
@overload print(obj, ...)
@return [nil];T;%0;"@;&F;+o;,;-T;.i7;/iK;0@z;1I"static VALUE
rb_f_print(int argc, const VALUE *argv, VALUE _)
{
rb_io_print(argc, argv, rb_ractor_stdout());
return Qnil;
};T; I"static VALUE;T;2To;
;F;;
;;;I"Kernel#putc;F;[[I"ch;T0;[[@i;T;: putc;0;[ ;{ ;IC;"Equivalent to:
$stdout.putc(int)
Refer to the documentation for IO#putc for important information regarding
multi-byte characters.
;T;[o;
;I"
overload;F;0;;;0; I"putc(int);T;IC;" ;T;[o;!
;I"return;F;I" ;T;0;[I"Integer;T;"@;#[ ;$I"@return [Integer];T;%0;"@;&F;'i ;(0;[[I"int;T0;"@;#[ ;$I"Equivalent to:
$stdout.putc(int)
Refer to the documentation for IO#putc for important information regarding
multi-byte characters.
@overload putc(int)
@return [Integer];T;%0;"@;&F;+o;,;-T;.iu;/i~;0@z;1I"static VALUE
rb_f_putc(VALUE recv, VALUE ch)
{
VALUE r_stdout = rb_ractor_stdout();
if (recv == r_stdout) {
return rb_io_putc(recv, ch);
}
return rb_funcallv(r_stdout, rb_intern("putc"), 1, &ch);
};T; I"static VALUE;T;2To;
;F;;
;;;I"Kernel#puts;F;[[@0;[[@i;T;: puts;0;[ ;{ ;IC;".Equivalent to
$stdout.puts(obj, ...)
;T;[o;
;I"
overload;F;0;;;0; I"puts(obj, ...);T;IC;" ;T;[o;!
;I"return;F;I" ;T;0;[I"nil;T;"@0;#[ ;$I"@return [nil];T;%0;"@0;&F;'i ;(0;[[I"obj;T0[I"...;T0;"@0;#[ ;$I"YEquivalent to
$stdout.puts(obj, ...)
@overload puts(obj, ...)
@return [nil];T;%0;"@0;&F;+o;,;-T;.i;/i;0@z;1I"static VALUE
rb_f_puts(int argc, VALUE *argv, VALUE recv)
{
VALUE r_stdout = rb_ractor_stdout();
if (recv == r_stdout) {
return rb_io_puts(argc, argv, recv);
}
return rb_funcallv(r_stdout, rb_intern("puts"), argc, argv);
};T; I"static VALUE;T;2To;
;F;;
;;;I"Kernel#gets;F;[[@0;[[@i]#;T;: gets;0;[ ;{ ;IC;"Returns (and assigns to $_
) the next line from the list
of files in +ARGV+ (or $*
), or from standard input if
no files are present on the command line. Returns +nil+ at end of
file. The optional argument specifies the record separator. The
separator is included with the contents of each record. A separator
of +nil+ reads the entire contents, and a zero-length separator
reads the input one paragraph at a time, where paragraphs are
divided by two consecutive newlines. If the first argument is an
integer, or optional second argument is given, the returning string
would not be longer than the given value in bytes. If multiple
filenames are present in +ARGV+, gets(nil)
will read
the contents one file at a time.
ARGV << "testfile"
print while gets
produces:
This is line one
This is line two
This is line three
And so on...
The style of programming using $_
as an implicit
parameter is gradually losing favor in the Ruby community.
;T;[o;
;I"
overload;F;0;;;0; I""gets(sep=$/ [, getline_args]);T;IC;" ;T;[o;!
;I"return;F;I" ;T;0;[I"String;TI"nil;T;"@P;#[ ;$I"@return [String, nil];T;%0;"@P;&F;'i ;(0;[[I"sep;TI"$/[, getline_args];T;"@Po;
;I"
overload;F;0;;;0; I"!gets(limit [, getline_args]);T;IC;" ;T;[o;!
;I"return;F;I" ;T;0;[I"String;TI"nil;T;"@P;#[ ;$I"@return [String, nil];T;%0;"@P;&F;'i ;(0;[[I"limit[, getline_args];T0;"@Po;
;I"
overload;F;0;;;0; I"&gets(sep, limit [, getline_args]);T;IC;" ;T;[o;!
;I"return;F;I" ;T;0;[I"String;TI"nil;T;"@P;#[ ;$I"@return [String, nil];T;%0;"@P;&F;'i ;(0;[[I"sep;T0[I"limit[, getline_args];T0;"@P;#[ ;$I"Returns (and assigns to $_
) the next line from the list
of files in +ARGV+ (or $*
), or from standard input if
no files are present on the command line. Returns +nil+ at end of
file. The optional argument specifies the record separator. The
separator is included with the contents of each record. A separator
of +nil+ reads the entire contents, and a zero-length separator
reads the input one paragraph at a time, where paragraphs are
divided by two consecutive newlines. If the first argument is an
integer, or optional second argument is given, the returning string
would not be longer than the given value in bytes. If multiple
filenames are present in +ARGV+, gets(nil)
will read
the contents one file at a time.
ARGV << "testfile"
print while gets
produces:
This is line one
This is line two
This is line three
And so on...
The style of programming using $_
as an implicit
parameter is gradually losing favor in the Ruby community.
@overload gets(sep=$/ [, getline_args])
@return [String, nil]
@overload gets(limit [, getline_args])
@return [String, nil]
@overload gets(sep, limit [, getline_args])
@return [String, nil];T;%0;"@P;&F;+o;,;-T;.i<#;/i\#;0@z;1I"static VALUE
rb_f_gets(int argc, VALUE *argv, VALUE recv)
{
if (recv == argf) {
return argf_gets(argc, argv, argf);
}
return rb_funcallv(argf, idGets, argc, argv);
};T; I"static VALUE;T;2To;
;F;;
;;;I"Kernel#readline;F;[[@0;[[@i#;T;:
readline;0;[ ;{ ;IC;"TEquivalent to Kernel::gets, except
+readline+ raises +EOFError+ at end of file.
;T;[o;
;I"
overload;F;0;;;0; I"readline(sep=$/);T;IC;" ;T;[o;!
;I"return;F;I" ;T;0;[I"String;T;"@;#[ ;$I"@return [String];T;%0;"@;&F;'i ;(0;[[I"sep;TI"$/;T;"@o;
;I"
overload;F;0;;;0; I"readline(limit);T;IC;" ;T;[o;!
;I"return;F;I" ;T;0;[I"String;T;"@;#[ ;$I"@return [String];T;%0;"@;&F;'i ;(0;[[I"
limit;T0;"@o;
;I"
overload;F;0;;;0; I"readline(sep, limit);T;IC;" ;T;[o;!
;I"return;F;I" ;T;0;[I"String;T;"@;#[ ;$I"@return [String];T;%0;"@;&F;'i ;(0;[[I"sep;T0[I"
limit;T0;"@;#[ ;$I"Equivalent to Kernel::gets, except
+readline+ raises +EOFError+ at end of file.
@overload readline(sep=$/)
@return [String]
@overload readline(limit)
@return [String]
@overload readline(sep, limit)
@return [String];T;%0;"@;&F;+o;,;-T;.i#;/i#;0@z;1I"static VALUE
rb_f_readline(int argc, VALUE *argv, VALUE recv)
{
if (recv == argf) {
return argf_readline(argc, argv, argf);
}
return rb_funcallv(argf, rb_intern("readline"), argc, argv);
};T; I"static VALUE;T;2To;
;F;;
;;;I"Kernel#select;F;[[@0;[[@i&;T;:select;0;[ ;{ ;IC;"Calls select(2) system call.
It monitors given arrays of IO objects, waits until one or more of
IO objects are ready for reading, are ready for writing, and have
pending exceptions respectively, and returns an array that contains
arrays of those IO objects. It will return +nil+ if optional
timeout value is given and no IO object is ready in
timeout seconds.
IO.select peeks the buffer of IO objects for testing readability.
If the IO buffer is not empty, IO.select immediately notifies
readability. This "peek" only happens for IO objects. It does not
happen for IO-like objects such as OpenSSL::SSL::SSLSocket.
The best way to use IO.select is invoking it after nonblocking
methods such as #read_nonblock, #write_nonblock, etc. The methods
raise an exception which is extended by IO::WaitReadable or
IO::WaitWritable. The modules notify how the caller should wait
with IO.select. If IO::WaitReadable is raised, the caller should
wait for reading. If IO::WaitWritable is raised, the caller should
wait for writing.
So, blocking read (#readpartial) can be emulated using
#read_nonblock and IO.select as follows:
begin
result = io_like.read_nonblock(maxlen)
rescue IO::WaitReadable
IO.select([io_like])
retry
rescue IO::WaitWritable
IO.select(nil, [io_like])
retry
end
Especially, the combination of nonblocking methods and IO.select is
preferred for IO like objects such as OpenSSL::SSL::SSLSocket. It
has #to_io method to return underlying IO object. IO.select calls
#to_io to obtain the file descriptor to wait.
This means that readability notified by IO.select doesn't mean
readability from OpenSSL::SSL::SSLSocket object.
The most likely situation is that OpenSSL::SSL::SSLSocket buffers
some data. IO.select doesn't see the buffer. So IO.select can
block when OpenSSL::SSL::SSLSocket#readpartial doesn't block.
However, several more complicated situations exist.
SSL is a protocol which is sequence of records.
The record consists of multiple bytes.
So, the remote side of SSL sends a partial record, IO.select
notifies readability but OpenSSL::SSL::SSLSocket cannot decrypt a
byte and OpenSSL::SSL::SSLSocket#readpartial will block.
Also, the remote side can request SSL renegotiation which forces
the local SSL engine to write some data.
This means OpenSSL::SSL::SSLSocket#readpartial may invoke #write
system call and it can block.
In such a situation, OpenSSL::SSL::SSLSocket#read_nonblock raises
IO::WaitWritable instead of blocking.
So, the caller should wait for ready for writability as above
example.
The combination of nonblocking methods and IO.select is also useful
for streams such as tty, pipe socket socket when multiple processes
read from a stream.
Finally, Linux kernel developers don't guarantee that
readability of select(2) means readability of following read(2) even
for a single process.
See select(2) manual on GNU/Linux system.
Invoking IO.select before IO#readpartial works well as usual.
However it is not the best way to use IO.select.
The writability notified by select(2) doesn't show
how many bytes are writable.
IO#write method blocks until given whole string is written.
So, IO#write(two or more bytes)
can block after
writability is notified by IO.select. IO#write_nonblock is required
to avoid the blocking.
Blocking write (#write) can be emulated using #write_nonblock and
IO.select as follows: IO::WaitReadable should also be rescued for
SSL renegotiation in OpenSSL::SSL::SSLSocket.
while 0 < string.bytesize
begin
written = io_like.write_nonblock(string)
rescue IO::WaitReadable
IO.select([io_like])
retry
rescue IO::WaitWritable
IO.select(nil, [io_like])
retry
end
string = string.byteslice(written..-1)
end
=== Parameters
read_array:: an array of IO objects that wait until ready for read
write_array:: an array of IO objects that wait until ready for write
error_array:: an array of IO objects that wait for exceptions
timeout:: a numeric value in second
=== Example
rp, wp = IO.pipe
mesg = "ping "
100.times {
# IO.select follows IO#read. Not the best way to use IO.select.
rs, ws, = IO.select([rp], [wp])
if r = rs[0]
ret = r.read(5)
print ret
case ret
when /ping/
mesg = "pong\n"
when /pong/
mesg = "ping "
end
end
if w = ws[0]
w.write(mesg)
end
}
produces:
ping pong
ping pong
ping pong
(snipped)
ping
;T;[o;
;I"
overload;F;0;;;0; I"Cselect(read_array [, write_array [, error_array [, timeout]]]);T;IC;" ;T;[o;!
;I"return;F;I" ;T;0;[I"
Array;TI"nil;T;"@;#[ ;$I"@return [Array, nil];T;%0;"@;&F;'i ;(0;[[I":read_array[, write_array [, error_array [, timeout]]];T0;"@;#[ ;$I"+Calls select(2) system call.
It monitors given arrays of IO objects, waits until one or more of
IO objects are ready for reading, are ready for writing, and have
pending exceptions respectively, and returns an array that contains
arrays of those IO objects. It will return +nil+ if optional
timeout value is given and no IO object is ready in
timeout seconds.
IO.select peeks the buffer of IO objects for testing readability.
If the IO buffer is not empty, IO.select immediately notifies
readability. This "peek" only happens for IO objects. It does not
happen for IO-like objects such as OpenSSL::SSL::SSLSocket.
The best way to use IO.select is invoking it after nonblocking
methods such as #read_nonblock, #write_nonblock, etc. The methods
raise an exception which is extended by IO::WaitReadable or
IO::WaitWritable. The modules notify how the caller should wait
with IO.select. If IO::WaitReadable is raised, the caller should
wait for reading. If IO::WaitWritable is raised, the caller should
wait for writing.
So, blocking read (#readpartial) can be emulated using
#read_nonblock and IO.select as follows:
begin
result = io_like.read_nonblock(maxlen)
rescue IO::WaitReadable
IO.select([io_like])
retry
rescue IO::WaitWritable
IO.select(nil, [io_like])
retry
end
Especially, the combination of nonblocking methods and IO.select is
preferred for IO like objects such as OpenSSL::SSL::SSLSocket. It
has #to_io method to return underlying IO object. IO.select calls
#to_io to obtain the file descriptor to wait.
This means that readability notified by IO.select doesn't mean
readability from OpenSSL::SSL::SSLSocket object.
The most likely situation is that OpenSSL::SSL::SSLSocket buffers
some data. IO.select doesn't see the buffer. So IO.select can
block when OpenSSL::SSL::SSLSocket#readpartial doesn't block.
However, several more complicated situations exist.
SSL is a protocol which is sequence of records.
The record consists of multiple bytes.
So, the remote side of SSL sends a partial record, IO.select
notifies readability but OpenSSL::SSL::SSLSocket cannot decrypt a
byte and OpenSSL::SSL::SSLSocket#readpartial will block.
Also, the remote side can request SSL renegotiation which forces
the local SSL engine to write some data.
This means OpenSSL::SSL::SSLSocket#readpartial may invoke #write
system call and it can block.
In such a situation, OpenSSL::SSL::SSLSocket#read_nonblock raises
IO::WaitWritable instead of blocking.
So, the caller should wait for ready for writability as above
example.
The combination of nonblocking methods and IO.select is also useful
for streams such as tty, pipe socket socket when multiple processes
read from a stream.
Finally, Linux kernel developers don't guarantee that
readability of select(2) means readability of following read(2) even
for a single process.
See select(2) manual on GNU/Linux system.
Invoking IO.select before IO#readpartial works well as usual.
However it is not the best way to use IO.select.
The writability notified by select(2) doesn't show
how many bytes are writable.
IO#write method blocks until given whole string is written.
So, IO#write(two or more bytes)
can block after
writability is notified by IO.select. IO#write_nonblock is required
to avoid the blocking.
Blocking write (#write) can be emulated using #write_nonblock and
IO.select as follows: IO::WaitReadable should also be rescued for
SSL renegotiation in OpenSSL::SSL::SSLSocket.
while 0 < string.bytesize
begin
written = io_like.write_nonblock(string)
rescue IO::WaitReadable
IO.select([io_like])
retry
rescue IO::WaitWritable
IO.select(nil, [io_like])
retry
end
string = string.byteslice(written..-1)
end
=== Parameters
read_array:: an array of IO objects that wait until ready for read
write_array:: an array of IO objects that wait until ready for write
error_array:: an array of IO objects that wait for exceptions
timeout:: a numeric value in second
=== Example
rp, wp = IO.pipe
mesg = "ping "
100.times {
# IO.select follows IO#read. Not the best way to use IO.select.
rs, ws, = IO.select([rp], [wp])
if r = rs[0]
ret = r.read(5)
print ret
case ret
when /ping/
mesg = "pong\n"
when /pong/
mesg = "ping "
end
end
if w = ws[0]
w.write(mesg)
end
}
produces:
ping pong
ping pong
ping pong
(snipped)
ping
@overload select(read_array [, write_array [, error_array [, timeout]]])
@return [Array, nil];T;%0;"@;&F;+o;,;-T;.i|%;/i&;0@z;1I"
static VALUE
rb_f_select(int argc, VALUE *argv, VALUE obj)
{
VALUE timeout;
struct select_args args;
struct timeval timerec;
int i;
rb_scan_args(argc, argv, "13", &args.read, &args.write, &args.except, &timeout);
if (NIL_P(timeout)) {
args.timeout = 0;
}
else {
timerec = rb_time_interval(timeout);
args.timeout = &timerec;
}
for (i = 0; i < numberof(args.fdsets); ++i)
rb_fd_init(&args.fdsets[i]);
return rb_ensure(select_call, (VALUE)&args, select_end, (VALUE)&args);
};T; I"static VALUE;T;2To;
;F;;
;;;I"Kernel#readlines;F;[[@0;[[@i#;T;:readlines;0;[ ;{ ;IC;"zReturns an array containing the lines returned by calling
Kernel.gets(sep)
until the end of file.
;T;[o;
;I"
overload;F;0;;;0; I"readlines(sep=$/);T;IC;" ;T;[o;!
;I"return;F;I" ;T;0;[I"
Array;T;"@;#[ ;$I"@return [Array];T;%0;"@;&F;'i ;(0;[[I"sep;TI"$/;T;"@o;
;I"
overload;F;0;;;0; I"readlines(limit);T;IC;" ;T;[o;!
;I"return;F;I" ;T;0;[I"
Array;T;"@;#[ ;$I"@return [Array];T;%0;"@;&F;'i ;(0;[[I"
limit;T0;"@o;
;I"
overload;F;0;;;0; I"readlines(sep, limit);T;IC;" ;T;[o;!
;I"return;F;I" ;T;0;[I"
Array;T;"@;#[ ;$I"@return [Array];T;%0;"@;&F;'i ;(0;[[I"sep;T0[I"
limit;T0;"@;#[ ;$I"Returns an array containing the lines returned by calling
Kernel.gets(sep)
until the end of file.
@overload readlines(sep=$/)
@return [Array]
@overload readlines(limit)
@return [Array]
@overload readlines(sep, limit)
@return [Array];T;%0;"@;&F;+o;,;-T;.i#;/i#;0@z;1I"static VALUE
rb_f_readlines(int argc, VALUE *argv, VALUE recv)
{
if (recv == argf) {
return argf_readlines(argc, argv, argf);
}
return rb_funcallv(argf, rb_intern("readlines"), argc, argv);
};T; I"static VALUE;T;2To;
;F;;
;;;I"
Kernel#`;F;[[I"str;T0;[[@i$;T;:`;0;[ ;{ ;IC;"ZReturns the standard output of running _cmd_ in a subshell.
The built-in syntax %x{...}
uses
this method. Sets $?
to the process status.
`date` #=> "Wed Apr 9 08:56:30 CDT 2003\n"
`ls testdir`.split[1] #=> "main.rb"
`echo oops && exit 99` #=> "oops\n"
$?.exitstatus #=> 99
;T;[o;
;I"
overload;F;0;;;0; I"
`cmd`;T;IC;" ;T;[o;!
;I"return;F;I" ;T;0;[I"String;T;"@/;#[ ;$I"@return [String];T;%0;"@/;&F;'i ;(0;[ ;"@/;#[ ;$I"Returns the standard output of running _cmd_ in a subshell.
The built-in syntax %x{...}
uses
this method. Sets $?
to the process status.
`date` #=> "Wed Apr 9 08:56:30 CDT 2003\n"
`ls testdir`.split[1] #=> "main.rb"
`echo oops && exit 99` #=> "oops\n"
$?.exitstatus #=> 99
@overload `cmd`
@return [String];T;%0;"@/;&F;+o;,;-T;.i$;/i$;0@z;1I"static VALUE
rb_f_backquote(VALUE obj, VALUE str)
{
VALUE port;
VALUE result;
rb_io_t *fptr;
SafeStringValue(str);
rb_last_status_clear();
port = pipe_open_s(str, "r", FMODE_READABLE|DEFAULT_TEXTMODE, NULL);
if (NIL_P(port)) return rb_str_new(0,0);
GetOpenFile(port, fptr);
result = read_all(fptr, remain_size(fptr), Qnil);
rb_io_close(port);
RFILE(port)->fptr = NULL;
rb_io_fptr_finalize(fptr);
rb_gc_force_recycle(port); /* also guards from premature GC */
return result;
};T; I"static VALUE;T;2To;
;F;;
;;;I"
Kernel#p;F;[[@0;[[@i9;T;:p;0;[ ;{ ;IC;"For each object, directly writes _obj_.+inspect+ followed by a
newline to the program's standard output.
S = Struct.new(:name, :state)
s = S['dave', 'TX']
p s
produces:
#
;T;[o;
;I"
overload;F;0;;;0; I"p(obj);T;IC;" ;T;[o;!
;I"return;F;I" ;T;0;[I"Object;T;"@L;#[ ;$I"@return [Object];T;%0;"@L;&F;'i ;(0;[[I"obj;T0;"@Lo;
;I"
overload;F;0;;;0; I"p(obj1, obj2, ...);T;IC;" ;T;[o;!
;I"return;F;I" ;T;0;[I"
Array;T;"@L;#[ ;$I"@return [Array];T;%0;"@L;&F;'i ;(0;[[I" obj1;T0[I" obj2;T0[I"...;T0;"@Lo;
;I"
overload;F;0;;;0; I"p();T;IC;" ;T;[o;!
;I"return;F;I" ;T;0;[I"nil;T;"@L;#[ ;$I"@return [nil];T;%0;"@L;&F;'i ;(0;[ ;"@L;#[ ;$I"PFor each object, directly writes _obj_.+inspect+ followed by a
newline to the program's standard output.
S = Struct.new(:name, :state)
s = S['dave', 'TX']
p s
produces:
#
@overload p(obj)
@return [Object]
@overload p(obj1, obj2, ...)
@return [Array]
@overload p()
@return [nil];T;%0;"@L;&F;+o;,;-T;.i';/i8;0@z;1I" static VALUE
rb_f_p(int argc, VALUE *argv, VALUE self)
{
int i;
for (i=0; i