Ever wonder where ruby’s defined? is defined? Because it looks so like a method, you might end up trying to check out a bunch of places, such as: Object, Kernel, BasicObject, or even Class or Module, but only to find no luck locating it in any of such places. Well, only until you’ve realized it, the answer is pretty simple: defined? is not a method but an operator or a keyword.

How to use defined??

irb(main):001:0> defined? String
=> "constant"
irb(main):002:0> defined?(String)
=> "constant"

What does defined? return?

irb(main):001:0> defined? 1
=> "expression"
irb(main):002:0> defined? dummy
=> nil
irb(main):003:0> defined? printf
=> "method"
irb(main):004:0> defined? String
=> "constant"
irb(main):005:0> defined? $_
=> "global-variable"
irb(main):006:0> defined? true
=> "true"
irb(main):007:0> defined? false
=> "false"
irb(main):008:0> defined? nil
=> "nil"
irb(main):009:0> defined? RUBY_ENGINE
=> "constant"
irb(main):010:0> defined? (a, b = 1, 2)
=> "assignment"
# ...

References

TOP▲
Post tagged with: ruby