From: why the lucky stiff Date: 2004-10-13T03:03:09+09:00 Subject: Re: YAML: Integers in YPATH --------------050001030503000202060201 Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit James F. Hranicky wrote: > % ruby /tmp/x.rb /tmp/x.yaml 1 > [] > % ruby /tmp/x.rb /tmp/x.yaml x2 > [{"Name"=>"myserv", "Arch"=>"x86", "OS"=>"linux", "Ether"=>2}] > >Is there any way to get the select to work without changing the 1 >to "1" or using !str every time? > > I'm attaching one technique for turning off typing. It looks like you found two bugs: * YPath is trying to match against typed keys. (All data should be untransformed in YAML::Syck::Nodes) * A bug exists in YAML::Syck::Node#transform. Use YAML::Syck::Node#select! for now, which transforms the results of a YPath query. _why --------------050001030503000202060201 Content-Type: text/plain; name="x.rb" Content-Transfer-Encoding: 7bit Content-Disposition: inline; filename="x.rb" require 'yaml' class YAML::Syck::Node alias _transform transform def transform case @kind when :scalar @value when :seq @value.collect do |v| v.transform end when :map hsh = {} @value.each do |k, v| hsh[v[0].transform] = v[1].transform end hsh end end def at( seg ) if Hash === @value k, v = @value.values.detect { |x| x[0].transform == seg } v elsif Array === @value and seg =~ /\A\d+\Z/ and @value[seg.to_i] @value[seg.to_i] end end end y = File.open(ARGV.shift).read node = ARGV.shift p YAML.parse(y).select!("/Domains/*/Networks/*/Hosts/1") p YAML.parse(y).transform --------------050001030503000202060201--