Class: NilClass

Inherits:
Object show all
Defined in:
object.c,
object.c

Overview

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:

  • #rationalize

  • #to_a

  • #to_c

  • #to_h

  • #to_r

  • #to_s

Another method provides inspection:

  • #inspect

Finally, there is this query method:

  • #nil?

Instance Method Summary collapse

Instance Method Details

#&(object) ⇒ false #&(object) ⇒ false

Returns false:

false & true       # => false
false & Object.new # => false

Argument object is evaluated:

false & raise # Raises RuntimeError.

Overloads:

  • #&(object) ⇒ false

    Returns:

    • (false)
  • #&(object) ⇒ false

    Returns:

    • (false)


1562
1563
1564
1565
1566
# File 'object.c', line 1562

static VALUE
false_and(VALUE obj, VALUE obj2)
{
    return Qfalse;
}

#===Object

#=~(object) ⇒ nil

Returns nil.

This method makes it useful to write:

while gets =~ /re/
  # ...
end

Returns:

  • (nil)


1417
1418
1419
1420
1421
# File 'object.c', line 1417

static VALUE
nil_match(VALUE obj1, VALUE obj2)
{
    return Qnil;
}

#^Object

#inspectObject

Returns string 'nil':

nil.inspect # => "nil"


1397
1398
1399
1400
1401
# File 'object.c', line 1397

static VALUE
nil_inspect(VALUE obj)
{
    return rb_usascii_str_new2("nil");
}

#nil?true

Returns true. For all other objects, method nil? returns false.

Returns:

  • (true)


1607
1608
1609
1610
1611
# File 'object.c', line 1607

static VALUE
rb_true(VALUE obj)
{
    return Qtrue;
}

#rationalize(eps = nil) ⇒ Object

Returns zero as a Rational:

nil.rationalize # => (0/1)

Argument eps is ignored.



2131
2132
2133
2134
2135
2136
# File 'rational.c', line 2131

static VALUE
nilclass_rationalize(int argc, VALUE *argv, VALUE self)
{
    rb_check_arity(argc, 0, 1);
    return nilclass_to_r(self);
}

#to_aObject

call-seq:

to_a -> []

Returns an empty Array.

nil.to_a # => []


1363
1364
1365
1366
1367
# File 'object.c', line 1363

static VALUE
nil_to_a(VALUE obj)
{
    return rb_ary_new2(0);
}

#to_cObject

Returns zero as a Complex:

nil.to_c # => (0+0i)


1927
1928
1929
1930
1931
# File 'complex.c', line 1927

static VALUE
nilclass_to_c(VALUE self)
{
    return rb_complex_new1(INT2FIX(0));
}

#to_hObject

call-seq:

to_h -> {}

Returns an empty Hash.

nil.to_h   #=> {}


1381
1382
1383
1384
1385
# File 'object.c', line 1381

static VALUE
nil_to_h(VALUE obj)
{
    return rb_hash_new();
}

#to_rObject

Returns zero as a Rational:

nil.to_r # => (0/1)


2114
2115
2116
2117
2118
# File 'rational.c', line 2114

static VALUE
nilclass_to_r(VALUE self)
{
    return rb_rational_new1(INT2FIX(0));
}

#to_sObject

Returns an empty String:

nil.to_s # => ""


1345
1346
1347
1348
1349
# File 'object.c', line 1345

VALUE
rb_nil_to_s(VALUE obj)
{
    return rb_cNilClass_to_s;
}

#|Object