ruby defined? operator

Run this on ruby 1.9

require 'delegate'

class Bam < SimpleDelegator
  def bam
    defined? Bam
  end

  def rails
    defined? Rails
  end
end

module Rails; end

bam = Bam.new(Object.new)

# I'm expecting "constant" to be returned but it's something else in fact.
# Can defined? operator be used inside SimpleDelegator?
bam.bam
# => nil
bam.rails
# => nil

# wtf moment
defined? Bam
# => "constant"
defined? Rails
# => "constant"

Something is up inside SimpleDelegator…

class Normal
  def bam
    defined? Normal
  end
end

Normal.new.bam
# => "constant"

Mon_Ouie: Module#const_defined?

Mon_Ouie: You can also just explicitly start from top-level using ::SomeConstant

Thanks Mon_Ouie on IRC#ruby-lang. Ok, let’s try that again…

class Bam < SimpleDelegator
  def bam
    defined? ::Bam
  end

  def rails
    Module.const_defined? :Rails
  end
end

bam = Bam.new(Object.new)

bam.bam
# => "constant"

bam.rails
# => true

hectic, turns out BasicObject is outside of the namespace of the standard library
see http://www.ruby-doc.org/core-1.9.3/BasicObject.html

 
1
Kudos
 
1
Kudos

Now read this

nginx pow coexistence

I am a user of Pow, is a zero-config Rack server for Mac OS X…serving your apps locally in under a minute and nginx, [engine x] is an HTTP and reverse proxy server Now, pow is good for rack app but nothing else. But if nginx sits in... Continue →