class NilClass
单例对象 nil 的类。
它的几个方法充当运算符
-
#&
-
#|
-
#=~
-
#^
其他方法充当转换器,将空值的概念传递给其他类
虽然 nil 没有明确定义的 to_hash 方法,但它可以用于 ** 解包,不添加任何关键字参数。
另一个方法提供检查
最后,有这个查询方法
公共实例方法
false & object → false 点击切换源代码
nil & object → false
返回 false
false & true # => false false & Object.new # => false
参数 object 被求值
false & raise # Raises RuntimeError.
static VALUE
false_and(VALUE obj, VALUE obj2)
{
return Qfalse;
}
true === other → true 或 false 点击切换源代码
false === other → true 或 false
nil === other → true 或 false
nil =~ object → nil 点击切换源代码
返回 nil。
这个方法使其在编写时很有用
while gets =~ /re/ # ... end
static VALUE
nil_match(VALUE obj1, VALUE obj2)
{
return Qnil;
}
false ^ object → true 或 false 点击切换源代码
nil ^ object → true 或 false
如果 object 是 nil 或 false,则返回 false,否则返回 true
nil ^ nil # => false nil ^ false # => false nil ^ Object.new # => true
#define false_xor true_and
inspect → 'nil' 点击切换源代码
返回字符串 'nil'
nil.inspect # => "nil"
static VALUE
nil_inspect(VALUE obj)
{
return rb_usascii_str_new2("nil");
}
nil? → true 点击切换源代码
返回 true。对于所有其他对象,方法 nil? 返回 false。
static VALUE
rb_true(VALUE obj)
{
return Qtrue;
}
rationalize(eps = nil) → (0/1) 点击切换源代码
将零作为 Rational 返回
nil.rationalize # => (0/1)
参数 eps 被忽略。
static VALUE
nilclass_rationalize(int argc, VALUE *argv, VALUE self)
{
rb_check_arity(argc, 0, 1);
return nilclass_to_r(self);
}
to_a → [] 点击切换源代码
to_c → (0+0i) 点击切换源代码
将零作为 Complex 返回
nil.to_c # => (0+0i)
static VALUE
nilclass_to_c(VALUE self)
{
return rb_complex_new1(INT2FIX(0));
}
to_f → 0.0 点击切换源代码
始终返回零。
nil.to_f #=> 0.0
# File ruby_3_4_1/nilclass.rb, line 22 def to_f return 0.0 end
to_h → {} 点击切换源代码
to_i → 0 点击切换源代码
始终返回零。
nil.to_i #=> 0
# File ruby_3_4_1/nilclass.rb, line 10 def to_i return 0 end
to_r → (0/1) 点击切换源代码
将零作为 Rational 返回
nil.to_r # => (0/1)
static VALUE
nilclass_to_r(VALUE self)
{
return rb_rational_new1(INT2FIX(0));
}
to_s → '' 点击切换源代码
返回一个空字符串
nil.to_s # => ""
VALUE
rb_nil_to_s(VALUE obj)
{
return rb_cNilClass_to_s;
}
false | object → true 或 false 点击切换源代码
nil | object → true 或 false
如果 object 是 nil 或 false,则返回 false,否则返回 true
nil | nil # => false nil | false # => false nil | Object.new # => true
#define false_or true_and