更新: 2017/06/12
更新: 2017/06/16 补充.class的输出
更新: 2017/06/23 .include?检验数组/哈希表是否包含目标值
更新: 2017/07/02 block_given?检验是否给了代码块以及检验是否有layout
更新: 2017/08/26 增加prensent?并与prensence比较
更新: 2018/01/03 增加is_a?, kind_of? (Object)
更新: 2018/02/03 增加instance_of?()的例子
更新: 2018/06/02 交换[instance_of?()] 和 [instance.class]的位置并修改相关标题,更方便查询
更新: 2018/10/12  增加frozen?, eql?, equal?
 
 检验是否定义  defined? (sample)

返回值
str
 未定义  nil
 本地变量  local-variable
 全局变量  global-variable
 实例变量   instance-variable
 类变量  class variable
 常数   constant
   
   
   
   
 检验是否有值  sample.nil?
 检验是否为空  sample.empty?
 只对数组和哈希表可用
 rails可用
 empty? + nil?
 sample.blank?
 rails可用
 blank?的否定
 是否有值(+数组哈希表是否空)

 sample.presence 返回值

存在   不存在
 self   nil

注意: 没有?
n. 出席; 仪表; 风度; 鬼魂,神灵;

 sample.present?   返回值

存在   不存在
 true  false
   
   
   
   
   
   
   
   
   
 获取所属的类

instance.class

输出的是类,如Test等
 判断用  instance.class == Class

判断类

instance_of?(Object)

arr = Array.new(4, 1) # [1, 1, 1, 1]
arr.instance_of?(Array) # -> true
 判断类2 better  is_a?, kind_of? (Object)
 和instance_of?的区别
   instance_of?只能判断自己当前的类
   is_a?,
kind_of?可以判断当前实例继承的类以及被包含的module
   
 检验是否包含目标   temp = [1, 2, 3]
 temp.include?(1)

不能用来比较哈希值

 是否给了代码块  block_given?
 检验是否有layout  content_for?(:sample) ? yield(:sample)
: yield
 检验是否冻结

 frozen?

 检验是否是指定的实例

 equal?

eql?

   
   
puts("----------------------------------------")
puts("    
     
     
 1.1")
puts("----------------------------------------")
asa = nil
asa = [1,2]
puts("asa#{asa}")
print("defined?: ", defined? asa);puts()
print("nil?: ", asa.nil?);puts()
print("empty?: ", asa.empty?);puts()
#print("blank?: ");print(asa.blank?);puts()
#print("presence?: ");print(asa.presence?);puts()
puts("----------------------------------------")
puts("    
     
     
 1.2")
puts("----------------------------------------")
local = 1
Const = 1
$global = 1
@instance = 1
@@class = 1
print("local: ", defined?(local));puts()
print("Const: ", defined?(Const));puts()
print("$global: ", defined?($global));puts()
print("@instance: ", defined?(@instance));puts()
print("@@class: ", defined?(@@class));puts()
 
puts("----------------------------------------")
puts("    
     
     
 1.3")
puts("----------------------------------------")
sample = []
print("isArray: ", sample.instance_of?(Array));puts();
print("class: ", sample.class);puts();
 
puts("----------------------------------------")
puts("    
     
     
 1.4")
puts("----------------------------------------")
#2017/06/16
class Test
  @a
  def initialize(num = 0)
    @a = num
  end
 
  def myFunc()
    puts(@a)
  end
end
sample = Test.new
p(sample.class)
p(sample.class.instance_of?(String))
p(sample.class.instance_of?(Test))
p(sample.class == Test)
 
puts("----------------------------------------")
puts("    
     
     
 1.5")
puts("----------------------------------------")
#2017/06/23
#include?
puts("array test: ")
temp = [1,2,3,5]
print(temp);puts();
for i in 1...5
  if temp.include?(i)
    printf("temp include
%d\n", i)
  else
    #printf("temp has no
member of %d\n", i)
    puts("temp has no member
of #{i}")
  end
end
 
puts("------------")
puts("hash test: ")
temp = {:a => 1, :b => 2, :c => 3, :d => 5}
temp1 = {x: 1, b: 2, c: 5, d: 7}
temp1.each_key do |item|
  if temp.include?(item)
    print("temp include %s",
item.inspect);puts();
  else
    puts("temp has no member
of #{item}")
  end
end
 
运行结果如下

----------------------------------------

1.1

----------------------------------------

asa[1, 2]

defined?: local-variable

nil?: false

empty?: false

----------------------------------------

1.2

----------------------------------------

check.rb:19: warning: class variable access from
toplevel

local: local-variable

Const: constant

$global: global-variable

@instance: instance-variable

check.rb:24: warning: class variable access from
toplevel

@@class: class variable

----------------------------------------

1.3

----------------------------------------

isArray: true

class: Array

----------------------------------------

1.4

----------------------------------------

Test

false

false

true

----------------------------------------

1.5

----------------------------------------

array test:

[1, 2, 3, 5]

temp include 1

temp include 2

temp include 3

temp has no member of 4

------------

hash test:

temp has no member of x

temp include :b

temp include :c

temp include :d

Ruby检验变量的更多相关文章

  1. 雷林鹏分享:Ruby 环境变量

    Ruby 环境变量 Ruby 解释器使用下列环境变量来控制它的行为.ENV 对象包含了所有当前设置的环境变量列表. 变量描述 DLN_LIBRARY_PATH动态加载模块搜索的路径. HOME当没有参 ...

  2. Ruby 环境变量

    Ruby 环境变量 Ruby 解释器使用下列环境变量来控制它的行为.ENV 对象包含了所有当前设置的环境变量列表. 变量 描述 DLN_LIBRARY_PATH 动态加载模块搜索的路径. HOME 当 ...

  3. centos7 Ruby环境变量配置

    ruby安装参考博客:https://blog.csdn.net/yelllowcong/article/details/78362370  (Redis之集群redis-trib.rb环境的搭建-y ...

  4. JavaScript 检验变量

    创建: 2019/02/20 迁入: 删除[WIP]标签(因为随时更新, 不存在完成不完成)   从[JavaScript 式与运算符]迁入typeof 更新: 2019/03/25 补充静态变量与参 ...

  5. Swift4 检验变量

    创建: 2018/05/03  判断类 public func isKind(of aClass: Swift.AnyClass) -> Bool  是否是aClass或其子类的实例  publ ...

  6. ruby中实例变量、类变量等等的区别和联系

    ruby的变量有局部变量,全局变量,实例变量,类变量,常量. 1.局部变量 局部变量以一个小写字母开头或下划线开头 局部变量有局部作用域限制(比如一个block内),它的作用域起始于声明处,结束于该声 ...

  7. [置顶] ruby变量详解(收集+整理)

    ruby的变量有局部变量,全局变量,实例变量,类变量,常量. 1.局部变量 局部变量以一个小写字母开头或下划线开头 局部变量有局部作用域限制(比如一个block内),它的作用域起始于声明处,结束于该声 ...

  8. 雷林鹏分享:Ruby 变量

    Ruby 变量 变量是持有可被任何程序使用的任何数据的存储位置. Ruby 支持五种类型的变量.您已经在前面的章节中大概了解了这些变量,本章节将为您详细讲解这五种类型的变量. Ruby 全局变量 全局 ...

  9. 理解Ruby中的作用域

    作用域对于Ruby以及其它编程语言都是一个需要理解的至关重要的基础知识.在我刚开始学习ruby的时候遇到很多诸如变量未定义.变量没有正确赋值之类的问题,归根结底是因为自己对于ruby作用域的了解不够, ...

随机推荐

  1. 关于zkfc与zkserver频繁断开的问题

    详见http://blog.csdn.net/dslztx/article/details/51596951

  2. 289. Game of Live

    According to the Wikipedia's article: "The Game of Life, also known simply as Life, is a cellul ...

  3. 联想M7400打印机加粉墨了还是显示没有粉墨

     联想M7400打印机加粉墨了还是显示没有粉墨?想必有很多的办公人士发现这个难题吧.其实很简单的! 先说下打印机的原理,打印机粉墨一边用的时候系统也在计数,当我们打印机加好墨后但打印机是不知道已经加好 ...

  4. 基于TensorFlow的图片识别服务

    1.使用TensorFlow Retrain进行图片分类训练 https://www.tensorflow.org/versions/master/how_tos/image_retraining/i ...

  5. 【Spark】Spark容错机制

    引入 一般来说,分布式数据集的容错性有两种方式:数据检查点和记录数据的更新. 面向大规模数据分析,数据检查点操作成本非常高,须要通过数据中心的网络连接在机器之间复制庞大的数据集,而网络带宽往往比内存带 ...

  6. vi,vim的基本使用方法

    "i”插入 "/" 查找 "wq"保存退出 "q!"不保存退出

  7. {head first} --- networking 1

    Head first系列的书确实非常好,深入浅出解说网络的组成.让曾经那些生涩的概念生动起来. Chapter 1 维修物理网络 CAT5电缆: 两端为RJ-45接头(水晶头).内部为UTP(非屏蔽双 ...

  8. 86. LotusScript中的数组函数

    R6对LotusScript有一些改进和增强,自那之后.Notes对象的接口时有补充和更新,但语言本身没有变化.那些改进就包括添加诸如ArrayGetIndex.ArrayUnique的实用函数. 但 ...

  9. PHP + Socket 发送http请求进而实现站点灌水

    本质上实现组装http信息的请求行,头信息.主题信息.參考it自学网 cookie信息和http请求头有非常大关系,注意把http请求头信息传递到函数里面 01-msg.php <?php re ...

  10. 动态生成页面(一)——ASP.NET中Literal使用

    在页面中加入内容时,假设是静态内容.无需使用容器,能够直接将标记作为HTML直接加入到页面中:可是,假设是动态内容,则必须借助容器将内容加入到页面中.典型的容器有:Label控件.Literal控件. ...