うれしい場面を探ってみる。
>> class Symbol >> def to_proc >> Proc.new{|obj, *args| obj.__send__(self, *args)} >> end >> end >> a = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] >> sum = a.inject(0, &:+) #=> 55 >> a.map!(&:to_s) #=> ["1", "2", "3", "4", "5", "6", "7", "8", "9", "10"] >> a.object_id #=> -606102960 >> a.map(&:object_id) #=> [-606126370, -606126410, -606126450, ...] >> b = a.map(&:dup) >> b.object_id #=> -606133810 >> b.map(&:object_id) #=> [-606133880, -606133920, -606133990, ...]
一次元だけ?
>> h = {"a" => "a", "b" => "b", "c" => "c"} >> h.delete_if(&:==) #=> {}
これぐらいかな。
>> h = {"controller" => "blog", "action" => "edit", "id" => 1234} >> h.map(&:to_sym).inject({}, &:store) #=> {:controller => "blog", :action => "edit", :id => 1234}
これはうそ。
Proc.new{|*args| args.shift.__send__(self, *args)}
だと、意図しない挙動をする方が多いかなぁ。
class Hash def symbolize_keys inject({}, &:_symbolize_keys) end def symbolize_keys! replace(symbolize_keys) end def stringify_keys inject({}, &:_stringify_keys) end def stringify_keys! replace(stringify_keys) end private def _symbolize_keys(pair) _convert_keys(pair, :to_sym) end def _stringify_keys(pair) _convert_keys(pair, :to_s) end def _convert_keys(pair, method) k, v = pair store(k.__send__(method), v) self end end
ちょっとはすっきりしましたか?[N/n]
のは、自分なわけですが。 Log200603#2006-03-15 から。
<script type="text/javascript"> <!-- var h2s = document.getElementsByTagName("h2"); for(var i = 0, j = h2s.length; i < j; i++){ var h2 = h2s[i]; var textnode = h2.childNodes[0]; var text = textnode.nodeValue; var date = text.split(" ")[0]; h2.id = date; var anchor = document.createElement("a"); anchor.name = date; anchor.appendChild(document.createTextNode(text)); h2.replaceChild(anchor, textnode); } //--> </script>
<h2>2006-03-14 (Tue.)</h2> #=> <h2 id="2006-03-14"><a name="2006-03-14">2006-03-14 (Tue.)</a></h2>
今はこんな感じのを忍ばせてもらってます。Opera とかで動くのかなぁ。