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

Devise Authentication unscoped

At Shop2 we do the Rails 3.2, Mongoid 2.4, and Devise 2.0. We found that Devise by default does not play nice with default scoping. # Our User model with default_scope of active users, # and a :vip scope # class User include... Continue →