avatar

An hero

coder by day, an hero by night

ruby defined? operator

Run this on ruby 1.9

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
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…

1
2
3
4
5
6
7
8
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…

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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