Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Messing with unicode "constants"
# ruby 2.0.0p353
module Kernel
def const_set(sym, value)
if sym.to_s.match /^[^A-Z]/
@@__cool_constants ||= {}
@@__cool_constants[sym] = value
else
super(sym, value)
end
end
def const_get(sym)
if sym.to_s.match /^[^A-Z]/
@@__cool_constants ||= {}
if @@__cool_constants.has_key? sym
return @@__cool_constants[sym]
end
end
super(sym)
end
def const_missing(sym, *args)
@@__cool_constants ||= {}
if @@__cool_constants.has_key? sym
@@__cool_constants[sym]
else
super(sym, *args)
end
end
def method_missing(sym, *args)
# "Real" Methods have precedence over unicode-constants which have
# precedence over dynamic methods.
@@__cool_constants ||= {}
if @@__cool_constants.has_key? sym
@@__cool_constants[sym]
else
super(sym, *args)
end
end
end
const_set(:"心", Class.new do
def foo
puts "HI"
end
end)
const_get(:"心").new.foo
心.new.foo
# output:
# HI
# HI
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment