Trouble understanding const_set()

OK, I guess I understand const_set… I guess I even understand the scoping
rules, but I certainly don’t get the scoping rules.

:slight_smile:

Even though self is Bar, we are at Foo scope? I’m sure there’s some sort of
good reason for this, but could someone clue me in? I’d be far less likely
to get “had” in the future if I understood the principle behind the
behavior.

Thanks,

Chris

···

----- Original Message -----
From: “Christoph” chr_news@gmx.net

This has little to do with const_set - its essentially a
consequenz of general ``constant’’ scoping rules .


class Foo
def Foo.foo
const_set ‘Constant’, 'just a string’
puts self.inspect
puts Constant
end
Constant = 'surprise’
end

$a = Object.new

class Bar < Foo
def $a.show
puts Constant
end

end

Bar.foo
$a.show

Bar
surprise
just a string

/Christoph