require "ostruct"
module Scarlet
class << self
def init ssss , mod , h
h.each do | m , *h |
next unless ssss.methods.include? m
a = ssss.method( m ).call *h
mod.define_singleton_method "#{m}".to_sym do | *h , &block | a.method( m ).call *h , &block end
mod.define_singleton_method "#{m.to_s.delete('!?')}_".to_sym do a end
end
end
end
module Default
def self.extended mod
Scarlet.init self , mod , [
[ :uniq_sym ] ,
]
end
def self.uniq_sym *h
ru = Hash.new
ru[:hs] = Hash.new
a = OpenStruct.new ru
a.define_singleton_method __method__ do | str = "misaka_" , n = rand(10000) |
tmp = "#{str}_#{n}".to_sym
if a.hs.keys.include? tmp
return uniq_sym str , n + 1
else
a.hs.store( tmp , true )
return tmp
end
end # singleton
return a
end # self
end # Dfault
end
module A
extend Scarlet::Default
end
p A.uniq_sym("test",1)
p A.uniq_sym("test",1)
p A.uniq_sym("rrrr",5)
p A.uniq_sym("rrrr",4)
p A.uniq_sym("rrrr",4)
CnJlcXVpcmUgIm9zdHJ1Y3QiCgptb2R1bGUgU2NhcmxldAogIGNsYXNzIDw8IHNlbGYKICAgIGRlZiBpbml0IHNzc3MgLCBtb2QgLCBoCiAgICAgIGguZWFjaCBkbyB8IG0gLCAqaCB8CiAgICAgICAgbmV4dCB1bmxlc3Mgc3Nzcy5tZXRob2RzLmluY2x1ZGU/IG0KICAgICAgICBhID0gc3Nzcy5tZXRob2QoIG0gKS5jYWxsICpoCiAgICAgICAgbW9kLmRlZmluZV9zaW5nbGV0b25fbWV0aG9kICIje219Ii50b19zeW0gIGRvIHwgKmggLCAmYmxvY2sgfCBhLm1ldGhvZCggbSApLmNhbGwgKmggLCAmYmxvY2sgIGVuZAogICAgICAgIG1vZC5kZWZpbmVfc2luZ2xldG9uX21ldGhvZCAiI3ttLnRvX3MuZGVsZXRlKCchPycpfV8iLnRvX3N5bSBkbyBhIGVuZAogICAgICBlbmQKICAgIGVuZAogIGVuZAogIG1vZHVsZSBEZWZhdWx0CiAgICBkZWYgc2VsZi5leHRlbmRlZCBtb2QKICAgIC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