Tag: 定义了

Ruby“定义?”运算符错误?

所以,我们有代码: class Foo def bar puts “Before existent: #{(defined? some_variable)}” puts “Before not_existent: #{(defined? nonexistent_variable)}” raise “error” some_variable = 42 rescue puts “exception” ensure puts “Ensure existent: #{(defined? some_variable)}” puts “Ensure not_existent: #{(defined? nonexistent_variable)}” end end 并从irb调用它: > Foo.new.bar 而且,这将返回: Before existent: Before not_existent: exception Ensure existent: local-variable Ensure not_existent: => nil 现在问题 – 为什么? […]

是否定义了嵌套哈希?()

确定@hash[:key1][:key2]是否最简洁的方法是什么,如果@hash或@hash[:key1]为零,则不会抛出错误? 如果@hash[:key1]存在,则( defined?(@hash[:key1][:key2])返回True(它不确定:key2是否定义了:key2 )