Created
May 1, 2014 02:54
-
-
Save wilkie/10cf0cf60f371f680d30 to your computer and use it in GitHub Desktop.
Messing with unicode "constants"
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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