![]() | Another way to implement unicode example in episode 6 |
|
12 Mar 2009, 03:09
foreverman (13 posts) |
The following is the unicode example for demonstrating const_missing hook:
class Module
original_const_missing = instance_method(:const_missing)
define_method(:const_missing) do |name|
if name.to_s =~ /^U([0-9a-fA-F]{4})$/
[$1.to_i(16)].pack("U*")
else
original_const_missing.bind(self).call(name)
end
end
end
Just inspired by the question asking the difference between class Module; def const_missing; end; endand class Dave;def self.const_missing; end; end, i got another implementation for unicode example:
class Object
def self.const_missing(name)
if name.to_s =~ /^U([0-9a-fA-F]{4})$/
[$1.to_i(16)].pack("U*")
else
super
end
end
end
Seems this behaves same as the original function. |
| You must be logged in to comment |

