The class of the singleton object nil
.
Several of its methods act as operators:
-
#&
-
#|
-
#=~
-
#^
Others act as converters, carrying the concept of nullity to other classes:
Another method provides inspection:
Finally, there is this query method:
Instance Public methods
false & object → false
nil & object → false
Link
Returns false
:
false & true # => false
false & Object.new # => false
Argument object
is evaluated:
false & raise # Raises RuntimeError.
Source: show
static VALUE false_and(VALUE obj, VALUE obj2) { return Qfalse; }
true === other → true or false
false === other → true or false
nil === other → true or false
Link
Returns true
or false
.
Like Object#==
, if object
is an instance of Object
(and not an instance of one of its many subclasses).
This method is commonly overridden by those subclasses, to provide meaningful semantics in case
statements.
Source: show
#define case_equal rb_equal
nil =~ object → nil Link
Returns nil
.
This method makes it useful to write:
while gets =~ /re/
# ...
end
Source: show
static VALUE nil_match(VALUE obj1, VALUE obj2) { return Qnil; }
false ^ object → true or false
nil ^ object → true or false
Link
Returns false
if object
is nil
or false
, true
otherwise:
nil ^ nil # => false
nil ^ false # => false
nil ^ Object.new # => true
Source: show
#define false_xor true_and
inspect → 'nil' Link
Returns string 'nil'
:
nil.inspect # => "nil"
Source: show
static VALUE nil_inspect(VALUE obj) { return rb_usascii_str_new2("nil"); }
nil.nil? → true Link
Returns true
. For all other objects, method nil?
returns false
.
Source: show
static VALUE rb_true(VALUE obj) { return Qtrue; }
rationalize(eps = nil) → (0/1) Link
Returns zero as a Rational:
nil.rationalize # => (0/1)
Argument eps
is ignored.
Source: show
static VALUE nilclass_rationalize(int argc, VALUE *argv, VALUE self) { rb_check_arity(argc, 0, 1); return nilclass_to_r(self); }
to_a → [] Link
Returns an empty Array
.
nil.to_a # => []
Source: show
static VALUE nil_to_a(VALUE obj) { return rb_ary_new2(0); }
to_c → (0+0i) Link
Returns zero as a Complex:
nil.to_c # => (0+0i)
Source: show
static VALUE nilclass_to_c(VALUE self) { return rb_complex_new1(INT2FIX(0)); }
nil.to_d → bigdecimal Link
Returns nil represented as a BigDecimal
.
require 'bigdecimal'
require 'bigdecimal/util'
nil.to_d # => 0.0
nil.to_f → 0.0 Link
Always returns zero.
nil.to_f #=> 0.0
to_h → {} Link
Returns an empty Hash
.
nil.to_h #=> {}
Source: show
static VALUE nil_to_h(VALUE obj) { return rb_hash_new(); }
nil.to_i → 0 Link
Always returns zero.
nil.to_i #=> 0
to_r → (0/1) Link
Returns zero as a Rational:
nil.to_r # => (0/1)
Source: show
static VALUE nilclass_to_r(VALUE self) { return rb_rational_new1(INT2FIX(0)); }