Skip to content

Instantly share code, notes, and snippets.

@wilkie
Created May 1, 2014 02:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save wilkie/10cf0cf60f371f680d30 to your computer and use it in GitHub Desktop.
Save wilkie/10cf0cf60f371f680d30 to your computer and use it in GitHub Desktop.
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