Check if a constant is already defined

后端 未结 3 1903
故里飘歌
故里飘歌 2020-12-09 00:45

This is a simple one, I hope. How do I check, in the following example, if a constant is already defined?

#this works
var = var||1
puts var
var = var||2
put         


        
3条回答
  •  情歌与酒
    2020-12-09 00:59

    CONST = 2 unless defined? CONST
    

    See here for more about awesome defined? operator.

    P.S. And in the future I guess you'll want var ||= 1 instead of var = var||1.

提交回复
热议问题