Class: NilClass
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 
While nil doesn’t have an explicitly defined #to_hash method, it can be used in ** unpacking, not adding any keyword arguments.
Another method provides inspection:
- 
#inspect 
Finally, there is this query method:
- 
#nil? 
Instance Method Summary collapse
- 
  
    
      #&(obj2)  ⇒ Object 
    
    
  
  
  
  
  
  
  
  
  
    Returns false:.
- #=== ⇒ Object
- 
  
    
      #=~(object)  ⇒ nil 
    
    
  
  
  
  
  
  
  
  
  
    Returns nil.
- #^ ⇒ Object
- 
  
    
      #inspect  ⇒ Object 
    
    
  
  
  
  
  
  
  
  
  
    Returns string 'nil':.
- 
  
    
      #nil?  ⇒ true 
    
    
  
  
  
  
  
  
  
  
  
    Returns true.
- 
  
    
      #rationalize(eps = nil)  ⇒ Object 
    
    
  
  
  
  
  
  
  
  
  
    Returns zero as a Rational:. 
- 
  
    
      #to_a  ⇒ Object 
    
    
  
  
  
  
  
  
  
  
  
    call-seq: to_a -> []. 
- 
  
    
      #to_c  ⇒ Object 
    
    
  
  
  
  
  
  
  
  
  
    Returns zero as a Complex:. 
- 
  
    
      #to_h  ⇒ Object 
    
    
  
  
  
  
  
  
  
  
  
    call-seq: to_h -> {}. 
- 
  
    
      #to_r  ⇒ Object 
    
    
  
  
  
  
  
  
  
  
  
    Returns zero as a Rational:. 
- 
  
    
      #to_s  ⇒ Object 
    
    
  
  
  
  
  
  
  
  
  
    Returns an empty String:. 
- #| ⇒ Object
Instance Method Details
#&(object) ⇒ false #&(object) ⇒ false
| 1601 1602 1603 1604 1605 | # File 'object.c', line 1601
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
| 1456 1457 1458 1459 1460 | # File 'object.c', line 1456
static VALUE
nil_match(VALUE obj1, VALUE obj2)
{
    return Qnil;
} | 
#^ ⇒ Object
#inspect ⇒ Object
Returns string 'nil':
nil.inspect # => "nil"
| 1436 1437 1438 1439 1440 | # File 'object.c', line 1436
static VALUE
nil_inspect(VALUE obj)
{
    return rb_usascii_str_new2("nil");
} | 
#nil? ⇒ true
Returns true. For all other objects, method nil? returns false.
| 1646 1647 1648 1649 1650 | # File 'object.c', line 1646
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.
| 2136 2137 2138 2139 2140 2141 | # File 'rational.c', line 2136
static VALUE
nilclass_rationalize(int argc, VALUE *argv, VALUE self)
{
    rb_check_arity(argc, 0, 1);
    return nilclass_to_r(self);
} | 
#to_a ⇒ Object
call-seq:
to_a -> []
Returns an empty Array.
nil.to_a # => []
| 1402 1403 1404 1405 1406 | # File 'object.c', line 1402
static VALUE
nil_to_a(VALUE obj)
{
    return rb_ary_new2(0);
} | 
#to_c ⇒ Object
Returns zero as a Complex:
nil.to_c # => (0+0i)
| 1936 1937 1938 1939 1940 | # File 'complex.c', line 1936
static VALUE
nilclass_to_c(VALUE self)
{
    return rb_complex_new1(INT2FIX(0));
} | 
#to_h ⇒ Object
call-seq:
to_h -> {}
Returns an empty Hash.
nil.to_h   #=> {}
| 1420 1421 1422 1423 1424 | # File 'object.c', line 1420
static VALUE
nil_to_h(VALUE obj)
{
    return rb_hash_new();
} | 
#to_r ⇒ Object
Returns zero as a Rational:
nil.to_r # => (0/1)
| 2119 2120 2121 2122 2123 | # File 'rational.c', line 2119
static VALUE
nilclass_to_r(VALUE self)
{
    return rb_rational_new1(INT2FIX(0));
} | 
#to_s ⇒ Object
Returns an empty String:
nil.to_s # => ""
| 1384 1385 1386 1387 1388 | # File 'object.c', line 1384
VALUE
rb_nil_to_s(VALUE obj)
{
    return rb_cNilClass_to_s;
} |