Ruby检验变量

 
更新: 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

 

posted @ 2017-06-10 09:45  懒虫哥哥  阅读(512)  评论(1编辑  收藏  举报