博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
ruby nil_Ruby中的数据类型-True,False和Nil用示例解释
阅读量:2518 次
发布时间:2019-05-11

本文共 2236 字,大约阅读时间需要 7 分钟。

ruby nil

true, false, and nil are special built-in data types in Ruby. Each of these keywords evaluates to an object that is the sole instance of its respective class.

truefalsenil是Ruby中的特殊内置数据类型。 这些关键字中的每一个都求值为一个对象,该对象是其各自类的唯一实例。

true.class => TrueClassfalse.class => FalseClassnil.class => NilClass

true and false are Ruby’s native boolean values. A boolean value is a value that can only be one of two possible values: true or not true. The object true represents truth, while false represents the opposite. You can assign variables to true / false, pass them to methods, and generally use them as you would other objects (such as numbers, Strings, Arrays, Hashes).

truefalse是Ruby的本地布尔值。 布尔值是只能是两个可能值之一的值:true或not true。 对象true代表真相,而false代表相反。 您可以将变量分配给true / false ,将它们传递给方法,并通常像使用其他对象(例如数字,字符串,数组,哈希)一样使用它们。

nil is a special value that indicates the absence of a value – it is Ruby’s way of referring to “nothing”. An example of when you will encounter the nil object is when you ask for something that doesn’t exist or cannot be found:

nil是一个特殊的值,它指示不存在值–这是Ruby引用“ nothing”的方式。 当您遇到不存在或找不到的东西时,便是遇到nil对象的一个​​示例:

hats = ["beret", "sombrero", "beanie", "fez", "flatcap"]hats[0] => "beret" # the hat at index 0hats[2] => "beanie" # the hat at index 2hats[4] => "flatcap" # the hat at index 4hats[5] => nil # there is no hat at index 5, index 5 holds nothing (nil)

Zero is not nothing (it’s a number, which is something). Likewise, empty strings, arrays, and hashes are not nothing (they are objects, which happen to be empty). You can call the method nil? to check whether an object is nil.

零不是什么(它是一个数字,是个数字)。 同样,空字符串,数组和哈希也不是什么(它们是对象,碰巧是空的)。 您可以调用方法nil? 检查对象是否为零。

0.nil? => false"".nil? => false[].nil? => false{}.nil? => falsenil.nil? => true # from the example abovehats[5].nil? => true

Every object in Ruby has a boolean value, meaning it is considered either true or false in a boolean context. Those considered true in this context are “truthy” and those considered false are “falsey.” In Ruby, only false and nil are “falsey,” everything else is “truthy.”

Ruby中的每个对象都有一个布尔值,这意味着在布尔上下文中它被视为true或false。 在这种情况下,被认为是正确的是“真实的”,被认为是错误的是“假的”。 在Ruby中, 只有 falsenil是“ falsey”,其他所有东西都是“ true”。

更多信息: (More Information:)

翻译自:

ruby nil

转载地址:http://dmrwd.baihongyu.com/

你可能感兴趣的文章
压力测试 相关
查看>>
MyBatis 通过 BATCH 批量提交
查看>>
android update automatically ( android 自动升级)
查看>>
session cookie
查看>>
POJ 1222 EXTENDED LIGHTS OUT(翻转+二维开关问题)
查看>>
【BZOJ-4059】Non-boring sequences 线段树 + 扫描线 (正解暴力)
查看>>
几种简单的负载均衡算法及其Java代码实现
查看>>
TMS3705A PCF7991AT 线路图
查看>>
安装Hadoop
查看>>
[BZOJ2282][Sdoi2011]消防
查看>>
supervisor配置详解(转)
查看>>
ABP框架系列之十一:(AspNet-Core-ASPNET核心)
查看>>
复习一些编译原理
查看>>
新手必看:生成对抗网络的初学者入门指导
查看>>
2019年上半年收集到的人工智能强化学习干货文章
查看>>
jeesite快速导入到myeclipse
查看>>
卷积神经网络CNNs的理解与体会
查看>>
e2e 自动化集成测试 架构 实例 WebStorm Node.js Mocha WebDriverIO Selenium Step by step (六) 自动化测试结构小节...
查看>>
测试笔,测试纸杯
查看>>
Matlab PCM编码解码
查看>>