自分しか使わないなら、これでもイイか。
def method_missing(name, *arg)
unless /.+=$/.match(name.to_s)
method_name = name.to_s + “=”
else
method_name = name.to_s
end
instance_eval %Q{
def #{method_name} val
@attributes[“#{method_name.chop}”] = val
end
def #{method_name.chop}
@attributes[“#{method_name.chop}”]
end
}
unless /.+=$/.match(name.to_s)
@attributes[name.to_s]
else
@attributes[name.to_s.chop] = arg.join(”).to_s
end
end
doc = REXML::Document.new
parent = doc.add_element(‘parent’)
parent.value = ‘12345’
p parent.to_s(1)
で
ruby rexmlで、xmlを作るとき。
require ‘rexml/document’
doc = REXML::Document.new
parent = doc.add_element(‘parent’)
parent.add_element(‘chlid’).attributes[‘value’] = ‘abc’
child = parent.add_element(‘child’)
child.attributes[‘value’] = ‘def’
child.attributes[‘value2’] = ‘12345’
puts doc.to_s(1)
—>
<parent>
<chlid value=’abc’/>
<child value2=’12345’ value=’def’/>
</parent>
attributes[…]っていうのがねぇ。
例えば、こうする。
require ‘rexml/document’
module REXML
class Element
def initialize( arg = UNDEFINED, parent=nil, context=nil )
super(parent)
@elements = Elements.new(self)
@attributes = Attributes.new(self)
@context = context
if arg.kind_of? String
self.name = arg
elsif arg.kind_of? Symbol
self.name = arg.to_s
elsif arg.kind_of? Element
self.name = arg.expanded_name
arg.attributes.each_attribute{ |attribute|
@attributes « Attribute.new( attribute )
}
@context = arg.context
end
def []=(name, value)
@attributes[name.to_s] = value
end
def [](name)
@attributes[name.to_s]
end
end
end
end
doc = REXML::Document.new
parent = doc.add_element(:parent)
parent.add_element(:child)[:value] = ‘abc’
parent.add_element(:child)[:value] = ‘def’
puts doc.to_s(1)
ruby 1.9では、to_s(indent)は、deprecated。
2Mくらいのxmlファイルをパースさせてみたら、1.8.6の方が少し
速い。そんなものか。
ruby
http://www.scribd.com/doc/134860/Sockets-Programming-in-Ruby
をみながら。udp echo server。
server:
require ‘socket’
# server
socket = UDPSocket.new
socket.bind(‘localhost’, 9999)
while true
ret = IO::select([socket])
ret[0].each do |sock|
msg, peer_addr = sock.recvfrom(1024)
p “———-“
p Time.now
p peer_addr
p msg
#just echo
sock.send(msg, 0, peer_addr[3], peer_addr[1])
end
end
client。Threadを使って。
require ‘socket’
require ‘thread’
#client
socket = UDPSocket.new
socket.bind(“127.0.0.1”, 0) # assign any available port
socket.connect(“127.0.0.1”, 9999)
# reading thread
Thread.new(socket) do |s|
while true
msg, peer_addr = s.recvfrom(1024)
p msg
end
end
# writing loop
index = 0
loop do
socket.send(“Hello UDP(#{index})”, 0)
index += 1
sleep 1
end
socket.close
uefa.comにインタビュー。
ケガして療養中の選手に400万ポンド払って意味あるのか?という批判を黙らせたのが、2006年のマンチェスターを破った30Mのフリーキック。
「フリーキックをするときの順番はいつも決まっている」
「最初にボールをみてキーパーのポジションをみる。キーパーの背の高さをみて、高いボールか低いボールかを決める。蹴る前はいつもゴールできる信じてるけど、あの日は、決める自信があった。で、ありがたいことにゴールにはいった」
“sublime left foot”と記事では賞賛してます。