diff options
author | matz <matz@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 2008-12-18 07:54:50 +0000 |
---|---|---|
committer | matz <matz@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 2008-12-18 07:54:50 +0000 |
commit | 85bae86cb63bc564959f4649a837fcffdccae3ae (patch) | |
tree | 931e71bab40a2cdcd839afba48d6cc67161acc6a | |
parent | cd5c309542ad7c352fcbcc0abb5a0bdf3f077a4c (diff) | |
download | ruby-85bae86cb63bc564959f4649a837fcffdccae3ae.tar.gz |
* lib/optparse/version.rb: remove variable shadowing to stop
warning. [ruby-core:20612]
* lib/irb/completion.rb, lib/net/imap.rb, lib/prime.rb,
lib/rinda/ring.rb, lib/racc/parser.rb,
lib/shell/command-processor.rb, lib/yaml/yamlnode.rb: ditto.
* lib/racc/parser.rb: remove space before parentheses.
* lib/shell/command-processor.rb, lib/shell/process-controller.rb:
use parentheses around arguments.
* lib/irb/ext/change-ws.rb, lib/rexml/validation/relaxng.rb,
lib/yaml/baseemitter.rb: indentation fix.
* lib/matrix.rb: small cosmetic change.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@20859 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
-rw-r--r-- | ChangeLog | 19 | ||||
-rw-r--r-- | lib/irb/completion.rb | 2 | ||||
-rw-r--r-- | lib/irb/ext/change-ws.rb | 2 | ||||
-rw-r--r-- | lib/matrix.rb | 132 | ||||
-rw-r--r-- | lib/net/imap.rb | 2 | ||||
-rw-r--r-- | lib/optparse/version.rb | 10 | ||||
-rw-r--r-- | lib/prime.rb | 4 | ||||
-rw-r--r-- | lib/racc/parser.rb | 3 | ||||
-rw-r--r-- | lib/rexml/validation/relaxng.rb | 2 | ||||
-rw-r--r-- | lib/rinda/ring.rb | 12 | ||||
-rw-r--r-- | lib/shell/command-processor.rb | 4 | ||||
-rw-r--r-- | lib/shell/process-controller.rb | 2 | ||||
-rw-r--r-- | lib/yaml/baseemitter.rb | 427 | ||||
-rw-r--r-- | lib/yaml/yamlnode.rb | 6 |
14 files changed, 308 insertions, 319 deletions
@@ -1,3 +1,22 @@ +Thu Dec 18 16:48:12 2008 Yukihiro Matsumoto <matz@ruby-lang.org> + + * lib/optparse/version.rb: remove variable shadowing to stop + warning. [ruby-core:20612] + + * lib/irb/completion.rb, lib/net/imap.rb, lib/prime.rb, + lib/rinda/ring.rb, lib/racc/parser.rb, + lib/shell/command-processor.rb, lib/yaml/yamlnode.rb: ditto. + + * lib/racc/parser.rb: remove space before parentheses. + + * lib/shell/command-processor.rb, lib/shell/process-controller.rb: + use parentheses around arguments. + + * lib/irb/ext/change-ws.rb, lib/rexml/validation/relaxng.rb, + lib/yaml/baseemitter.rb: indentation fix. + + * lib/matrix.rb: small cosmetic change. + Thu Dec 18 08:15:04 2008 James Edward Gray II <jeg2@ruby-lang.org> * lib/xmlrpc/server.rb: Restricting method inspection to show only diff --git a/lib/irb/completion.rb b/lib/irb/completion.rb index 0db470db94..26339f217d 100644 --- a/lib/irb/completion.rb +++ b/lib/irb/completion.rb @@ -159,7 +159,7 @@ module IRB end next if name != "IRB::Context" and /^(IRB|SLex|RubyLex|RubyToken)/ =~ name - candidates.concat m.instance_methods(false).collect{|m| m.to_s} + candidates.concat m.instance_methods(false).collect{|x| x.to_s} } candidates.sort! candidates.uniq! diff --git a/lib/irb/ext/change-ws.rb b/lib/irb/ext/change-ws.rb index dd8f145b27..217d4a58ef 100644 --- a/lib/irb/ext/change-ws.rb +++ b/lib/irb/ext/change-ws.rb @@ -56,6 +56,6 @@ module IRB # end # end # alias change_workspace change_binding - end + end end diff --git a/lib/matrix.rb b/lib/matrix.rb index 229e51e5b7..ec03c730fa 100644 --- a/lib/matrix.rb +++ b/lib/matrix.rb @@ -140,10 +140,8 @@ class Matrix # # def Matrix.columns(columns) - rows = (0 .. columns[0].size - 1).collect { - |i| - (0 .. columns.size - 1).collect { - |j| + rows = (0 .. columns[0].size - 1).collect {|i| + (0 .. columns.size - 1).collect {|j| columns[j][i] } } @@ -159,8 +157,7 @@ class Matrix # def Matrix.diagonal(*values) size = values.size - rows = (0 .. size - 1).collect { - |j| + rows = (0 .. size - 1).collect {|j| row = Array.new(size).fill(0, 0, size) row[j] = values[j] row @@ -311,13 +308,11 @@ class Matrix # def column(j) # :yield: e if block_given? - 0.upto(row_size - 1) do - |i| + 0.upto(row_size - 1) do |i| yield @rows[i][j] end else - col = (0 .. row_size - 1).collect { - |i| + col = (0 .. row_size - 1).collect {|i| @rows[i][j] } Vector.elements(col, false) @@ -364,8 +359,7 @@ class Matrix Matrix.Raise ArgumentError, param.inspect end - rows = @rows[from_row, size_row].collect{ - |row| + rows = @rows[from_row, size_row].collect{|row| row[from_col, size_col] } Matrix.rows(rows, false) @@ -421,8 +415,7 @@ class Matrix def compare_by_row_vectors(rows, comparison = :==) return false unless @rows.size == rows.size - 0.upto(@rows.size - 1) do - |i| + 0.upto(@rows.size - 1) do |i| return false unless @rows[i].send(comparison, rows[i]) end true @@ -462,10 +455,8 @@ class Matrix def *(m) # m is matrix or vector or number case(m) when Numeric - rows = @rows.collect { - |row| - row.collect { - |e| + rows = @rows.collect {|row| + row.collect {|e| e * m } } @@ -477,13 +468,10 @@ class Matrix when Matrix Matrix.Raise ErrDimensionMismatch if column_size != m.row_size - rows = (0 .. row_size - 1).collect { - |i| - (0 .. m.column_size - 1).collect { - |j| + rows = (0 .. row_size - 1).collect {|i| + (0 .. m.column_size - 1).collect {|j| vij = 0 - 0.upto(column_size - 1) do - |k| + 0.upto(column_size - 1) do |k| vij += self[i, k] * m[k, j] end vij @@ -516,10 +504,8 @@ class Matrix Matrix.Raise ErrDimensionMismatch unless row_size == m.row_size and column_size == m.column_size - rows = (0 .. row_size - 1).collect { - |i| - (0 .. column_size - 1).collect { - |j| + rows = (0 .. row_size - 1).collect {|i| + (0 .. column_size - 1).collect {|j| self[i, j] + m[i, j] } } @@ -546,10 +532,8 @@ class Matrix Matrix.Raise ErrDimensionMismatch unless row_size == m.row_size and column_size == m.column_size - rows = (0 .. row_size - 1).collect { - |i| - (0 .. column_size - 1).collect { - |j| + rows = (0 .. row_size - 1).collect {|i| + (0 .. column_size - 1).collect {|j| self[i, j] - m[i, j] } } @@ -565,10 +549,8 @@ class Matrix def /(other) case other when Numeric - rows = @rows.collect { - |row| - row.collect { - |e| + rows = @rows.collect {|row| + row.collect {|e| e / other } } @@ -603,7 +585,7 @@ class Matrix for k in 0..size i = k akk = a[k][k].abs - for j in (k+1)..size + ((k+1)..size).each do |j| v = a[j][k].abs if v > akk i = j @@ -697,12 +679,13 @@ class Matrix det = 1 k = 0 - begin + loop do if (akk = a[k][k]) == 0 i = k - begin - return 0 if (i += 1) > size - end while a[i][k] == 0 + loop do + return 0 if (ii += 1) > size + break unless a[i][k] == 0 + end a[i], a[k] = a[k], a[i] akk = a[k][k] det *= -1 @@ -710,13 +693,13 @@ class Matrix for i in k + 1 .. size q = a[i][k].quo(akk) - (k + 1).upto(size) do - |j| + (k + 1).upto(size) do |j| a[i][j] -= a[k][j] * q end end det *= akk - end while (k += 1) <= size + break unless (k += 1) <= size + end det end alias det determinant @@ -739,12 +722,13 @@ class Matrix det = 1 k = 0 - begin + loop do if a[k][k].zero? i = k - begin + loop do return 0 if (i += 1) > size - end while a[i][k].zero? + break unless a[i][k].zero? + end a[i], a[k] = a[k], a[i] det *= -1 end @@ -761,7 +745,8 @@ class Matrix end end det *= a[k][k] - end while (k += 1) <= size + break unless (k += 1) <= size + end det end alias det_e determinant_e @@ -786,31 +771,32 @@ class Matrix end rank = 0 k = 0 - begin + loop do if (akk = a[k][k]) == 0 i = k exists = true - begin + loop do if (i += 1) > a_column_size - 1 exists = false break end - end while a[i][k] == 0 + break unless a[i][k] == 0 + end if exists a[i], a[k] = a[k], a[i] akk = a[k][k] else i = k exists = true - begin + loop do if (i += 1) > a_row_size - 1 exists = false break end - end while a[k][i] == 0 + break unless a[k][i] == 0 + end if exists - k.upto(a_column_size - 1) do - |j| + k.upto(a_column_size - 1) do |j| a[j][k], a[j][i] = a[j][i], a[j][k] end akk = a[k][k] @@ -827,7 +813,8 @@ class Matrix end end rank += 1 - end while (k += 1) <= a_column_size - 1 + break unless (k += 1) <= a_column_size - 1 + end return rank end @@ -873,8 +860,7 @@ class Matrix # def trace tr = 0 - 0.upto(column_size - 1) do - |i| + 0.upto(column_size - 1) do |i| tr += @rows[i][i] end tr @@ -916,8 +902,7 @@ class Matrix # Returns an array of the row vectors of the matrix. See Vector. # def row_vectors - rows = (0 .. row_size - 1).collect { - |i| + rows = (0 .. row_size - 1).collect {|i| row(i) } rows @@ -927,8 +912,7 @@ class Matrix # Returns an array of the column vectors of the matrix. See Vector. # def column_vectors - columns = (0 .. column_size - 1).collect { - |i| + columns = (0 .. column_size - 1).collect {|i| column(i) } columns @@ -961,8 +945,7 @@ class Matrix # Overrides Object#to_s # def to_s - "Matrix[" + @rows.collect{ - |row| + "Matrix[" + @rows.collect{|row| "[" + row.collect{|e| e.to_s}.join(", ") + "]" }.join(", ")+"]" end @@ -1169,8 +1152,7 @@ class Vector # def each2(v) # :yield: e1, e2 Vector.Raise ErrDimensionMismatch if size != v.size - 0.upto(size - 1) do - |i| + 0.upto(size - 1) do |i| yield @elements[i], v[i] end end @@ -1181,8 +1163,7 @@ class Vector # def collect2(v) # :yield: e1, e2 Vector.Raise ErrDimensionMismatch if size != v.size - (0 .. size - 1).collect do - |i| + (0 .. size - 1).collect do |i| yield @elements[i], v[i] end end @@ -1253,8 +1234,7 @@ class Vector case v when Vector Vector.Raise ErrDimensionMismatch if size != v.size - els = collect2(v) { - |v1, v2| + els = collect2(v) {|v1, v2| v1 + v2 } Vector.elements(els, false) @@ -1273,8 +1253,7 @@ class Vector case v when Vector Vector.Raise ErrDimensionMismatch if size != v.size - els = collect2(v) { - |v1, v2| + els = collect2(v) {|v1, v2| v1 - v2 } Vector.elements(els, false) @@ -1298,8 +1277,7 @@ class Vector Vector.Raise ErrDimensionMismatch if size != v.size p = 0 - each2(v) { - |v1, v2| + each2(v) {|v1, v2| p += v1 * v2 } p @@ -1309,8 +1287,7 @@ class Vector # Like Array#collect. # def collect # :yield: e - els = @elements.collect { - |v| + els = @elements.collect {|v| yield v } Vector.elements(els, false) @@ -1321,8 +1298,7 @@ class Vector # Like Vector#collect2, but returns a Vector instead of an Array. # def map2(v) # :yield: e1, e2 - els = collect2(v) { - |v1, v2| + els = collect2(v) {|v1, v2| yield v1, v2 } Vector.elements(els, false) diff --git a/lib/net/imap.rb b/lib/net/imap.rb index e6ffa3b1e0..09b5349298 100644 --- a/lib/net/imap.rb +++ b/lib/net/imap.rb @@ -3216,7 +3216,7 @@ module Net ].join(':') ) - return response.keys.map { |k| qdval(k.to_s, response[k]) }.join(',') + return response.keys.map {|key| qdval(key.to_s, response[key]) }.join(',') when STAGE_TWO @stage = nil # if at the second stage, return an empty string diff --git a/lib/optparse/version.rb b/lib/optparse/version.rb index 558d9d710b..76ed564287 100644 --- a/lib/optparse/version.rb +++ b/lib/optparse/version.rb @@ -1,7 +1,7 @@ # OptionParser internal utility class << OptionParser - def show_version(*pkg) + def show_version(*pkgs) progname = ARGV.options.program_name result = false show = proc do |klass, cname, version| @@ -19,14 +19,14 @@ class << OptionParser puts str result = true end - if pkg.size == 1 and pkg[0] == "all" + if pkgs.size == 1 and pkgs[0] == "all" self.search_const(::Object, /\AV(?:ERSION|ersion)\z/) do |klass, cname, version| unless cname[1] == ?e and klass.const_defined?(:Version) show.call(klass, cname.intern, version) end end else - pkg.each do |pkg| + pkgs.each do |pkg| begin pkg = pkg.split(/::|\//).inject(::Object) {|m, c| m.const_get(c)} v = case @@ -46,8 +46,8 @@ class << OptionParser result end - def each_const(path, klass = ::Object) - path.split(/::|\//).inject(klass) do |klass, name| + def each_const(path, base = ::Object) + path.split(/::|\//).inject(base) do |klass, name| raise NameError, path unless Module === klass klass.constants.grep(/#{name}/i) do |c| klass.const_defined?(c) or next diff --git a/lib/prime.rb b/lib/prime.rb index 0c60695c6c..650d279bc4 100644 --- a/lib/prime.rb +++ b/lib/prime.rb @@ -411,8 +411,8 @@ class Prime loop do extend_table until @table.length > i if !@table[i].zero? - (j...32).step(2) do |j| - return 32*i+j if !@table[i][j.div(2)].zero? + (j...32).step(2) do |k| + return 32*i+k if !@table[i][k.div(2)].zero? end end i += 1; j = 1 diff --git a/lib/racc/parser.rb b/lib/racc/parser.rb index a7408dd00a..e87a250e56 100644 --- a/lib/racc/parser.rb +++ b/lib/racc/parser.rb @@ -159,7 +159,6 @@ module Racc reduce_n, use_result, * = arg _racc_init_sysvars - tok = nil act = nil i = nil nerr = 0 @@ -189,7 +188,7 @@ module Racc ; end - while not (i = action_pointer[@racc_state[-1]]) or + while not(i = action_pointer[@racc_state[-1]]) or not @racc_read_next or @racc_t == 0 # $ unless i and i += @racc_t and diff --git a/lib/rexml/validation/relaxng.rb b/lib/rexml/validation/relaxng.rb index 969f51bc95..2b863710b4 100644 --- a/lib/rexml/validation/relaxng.rb +++ b/lib/rexml/validation/relaxng.rb @@ -436,7 +436,7 @@ module REXML arry[-1][-1].event_arg = evt[1] @value = false - end + end else arry << [] if evt[0] == :start_element arry[-1] << generate_event( evt ) diff --git a/lib/rinda/ring.rb b/lib/rinda/ring.rb index f9ef3d1a52..4dc7c7d79a 100644 --- a/lib/rinda/ring.rb +++ b/lib/rinda/ring.rb @@ -256,15 +256,15 @@ if __FILE__ == $0 $stdin.gets when 'w' finger = Rinda::RingFinger.new(nil) - finger.lookup_ring do |ts| - p ts - ts.write([:hello, :world]) + finger.lookup_ring do |ts2| + p ts2 + ts2.write([:hello, :world]) end when 'r' finger = Rinda::RingFinger.new(nil) - finger.lookup_ring do |ts| - p ts - p ts.take([nil, nil]) + finger.lookup_ring do |ts2| + p ts2 + p ts2.take([nil, nil]) end end end diff --git a/lib/shell/command-processor.rb b/lib/shell/command-processor.rb index b7b58bb618..900b31a22d 100644 --- a/lib/shell/command-processor.rb +++ b/lib/shell/command-processor.rb @@ -26,7 +26,7 @@ class Shell # m = [:initialize, :expand_path] if Object.methods.first.kind_of?(String) - NoDelegateMethods = m.collect{|m| m.id2name} + NoDelegateMethods = m.collect{|x| x.id2name} else NoDelegateMethods = m end @@ -124,7 +124,7 @@ class Shell f = File.open(path, mode, perm) File.chmod(perm & ~@shell.umask, path) if block_given? - f.each &b + f.each(&b) end f else diff --git a/lib/shell/process-controller.rb b/lib/shell/process-controller.rb index 4f28e018ad..f2bf1d44c8 100644 --- a/lib/shell/process-controller.rb +++ b/lib/shell/process-controller.rb @@ -62,7 +62,7 @@ class Shell end def block_output_synchronize(&b) - @BlockOutputMonitor.synchronize &b + @BlockOutputMonitor.synchronize(&b) end def wait_to_finish_all_process_controllers diff --git a/lib/yaml/baseemitter.rb b/lib/yaml/baseemitter.rb index a1992f498b..4bdc796cbf 100644 --- a/lib/yaml/baseemitter.rb +++ b/lib/yaml/baseemitter.rb @@ -7,241 +7,236 @@ require 'yaml/encoding' require 'yaml/error' module YAML + module BaseEmitter + def options( opt = nil ) + if opt + @options[opt] || YAML::DEFAULTS[opt] + else + @options + end + end - module BaseEmitter + def options=( opt ) + @options = opt + end - def options( opt = nil ) - if opt - @options[opt] || YAML::DEFAULTS[opt] - else - @options - end - end + # + # Emit binary data + # + def binary_base64( value ) + self << "!binary " + self.node_text( [value].pack("m"), '|' ) + end - def options=( opt ) - @options = opt + # + # Emit plain, normal flowing text + # + def node_text( value, block = nil ) + @seq_map = false + valx = value.dup + unless block + block = + if options(:UseBlock) + '|' + elsif not options(:UseFold) and valx =~ /\n[ \t]/ and not valx =~ /#{YAML::ESCAPE_CHAR}/ + '|' + else + '>' + end + indt = $&.to_i if block =~ /\d+/ + if valx =~ /(\A\n*[ \t#]|^---\s+)/ + indt = options(:Indent) unless indt.to_i > 0 + block += indt.to_s end - # - # Emit binary data - # - def binary_base64( value ) - self << "!binary " - self.node_text( [value].pack("m"), '|' ) + block += + if valx =~ /\n\Z\n/ + "+" + elsif valx =~ /\Z\n/ + "" + else + "-" + end + end + block += "\n" + if block[0] == ?" + esc_skip = ( "\t\n" unless valx =~ /^[ \t]/ ) || "" + valx = fold( YAML::escape( valx, esc_skip ) + "\"" ).chomp + self << '"' + indent_text( valx, indt, false ) + else + if block[0] == ?> + valx = fold( valx ) end + #p [block, indt] + self << block + indent_text( valx, indt ) + end + end + + # + # Emit a simple, unqouted string + # + def simple( value ) + @seq_map = false + self << value.to_s + end - # - # Emit plain, normal flowing text - # - def node_text( value, block = nil ) - @seq_map = false - valx = value.dup - unless block - block = - if options(:UseBlock) - '|' - elsif not options(:UseFold) and valx =~ /\n[ \t]/ and not valx =~ /#{YAML::ESCAPE_CHAR}/ - '|' - else - '>' - end - - indt = $&.to_i if block =~ /\d+/ - if valx =~ /(\A\n*[ \t#]|^---\s+)/ - indt = options(:Indent) unless indt.to_i > 0 - block += indt.to_s - end - - block += - if valx =~ /\n\Z\n/ - "+" - elsif valx =~ /\Z\n/ - "" - else - "-" - end - end - block += "\n" - if block[0] == ?" - esc_skip = ( "\t\n" unless valx =~ /^[ \t]/ ) || "" - valx = fold( YAML::escape( valx, esc_skip ) + "\"" ).chomp - self << '"' + indent_text( valx, indt, false ) - else - if block[0] == ?> - valx = fold( valx ) - end - #p [block, indt] - self << block + indent_text( valx, indt ) - end - end - - # - # Emit a simple, unqouted string - # - def simple( value ) - @seq_map = false - self << value.to_s - end - - # - # Emit double-quoted string - # - def double( value ) - "\"#{YAML.escape( value )}\"" - end - - # - # Emit single-quoted string - # - def single( value ) - "'#{value}'" - end - - # - # Write a text block with the current indent - # - def indent_text( text, mod, first_line = true ) - return "" if text.to_s.empty? - spacing = indent( mod ) - text = text.gsub( /\A([^\n])/, "#{ spacing }\\1" ) if first_line - return text.gsub( /\n^([^\n])/, "\n#{spacing}\\1" ) - end - - # - # Write a current indent - # - def indent( mod = nil ) - #p [ self.id, level, mod, :INDENT ] - if level <= 0 - mod ||= 0 - else - mod ||= options(:Indent) - mod += ( level - 1 ) * options(:Indent) - end - return " " * mod - end - - # - # Add indent to the buffer - # - def indent! - self << indent - end - - # - # Folding paragraphs within a column - # - def fold( value ) - value.gsub( /(^[ \t]+.*$)|(\S.{0,#{options(:BestWidth) - 1}})(?:[ \t]+|(\n+(?=[ \t]|\Z))|$)/ ) do - $1 || $2 + ( $3 || "\n" ) - end - end + # + # Emit double-quoted string + # + def double( value ) + "\"#{YAML.escape( value )}\"" + end - # - # Quick mapping - # - def map( type, &e ) - val = Mapping.new - e.call( val ) - self << "#{type} " if type.length.nonzero? - - # - # Empty hashes - # - if val.length.zero? - self << "{}" - @seq_map = false - else - # FIXME - # if @buffer.length == 1 and options(:UseHeader) == false and type.length.zero? - # @headless = 1 - # end - - defkey = @options.delete( :DefaultKey ) - if defkey - seq_map_shortcut - self << "= : " - defkey.to_yaml( :Emitter => self ) - end - - # - # Emit the key and value - # - val.each { |v| - seq_map_shortcut - if v[0].is_complex_yaml? - self << "? " - end - v[0].to_yaml( :Emitter => self ) - if v[0].is_complex_yaml? - self << "\n" - indent! - end - self << ": " - v[1].to_yaml( :Emitter => self ) - } - end - end + # + # Emit single-quoted string + # + def single( value ) + "'#{value}'" + end - def seq_map_shortcut - # FIXME: seq_map needs to work with the new anchoring system - # if @seq_map - # @anchor_extras[@buffer.length - 1] = "\n" + indent - # @seq_map = false - # else - self << "\n" - indent! - # end + # + # Write a text block with the current indent + # + def indent_text( text, mod, first_line = true ) + return "" if text.to_s.empty? + spacing = indent( mod ) + text = text.gsub( /\A([^\n])/, "#{ spacing }\\1" ) if first_line + return text.gsub( /\n^([^\n])/, "\n#{spacing}\\1" ) + end + + # + # Write a current indent + # + def indent( mod = nil ) + #p [ self.id, level, mod, :INDENT ] + if level <= 0 + mod ||= 0 + else + mod ||= options(:Indent) + mod += ( level - 1 ) * options(:Indent) + end + return " " * mod + end + + # + # Add indent to the buffer + # + def indent! + self << indent + end + + # + # Folding paragraphs within a column + # + def fold( value ) + value.gsub( /(^[ \t]+.*$)|(\S.{0,#{options(:BestWidth) - 1}})(?:[ \t]+|(\n+(?=[ \t]|\Z))|$)/ ) do + $1 || $2 + ( $3 || "\n" ) + end + end + + # + # Quick mapping + # + def map( type, &e ) + val = Mapping.new + e.call( val ) + self << "#{type} " if type.length.nonzero? + + # + # Empty hashes + # + if val.length.zero? + self << "{}" + @seq_map = false + else + # FIXME + # if @buffer.length == 1 and options(:UseHeader) == false and type.length.zero? + # @headless = 1 + # end + + defkey = @options.delete( :DefaultKey ) + if defkey + seq_map_shortcut + self << "= : " + defkey.to_yaml( :Emitter => self ) end # - # Quick sequence + # Emit the key and value # - def seq( type, &e ) - @seq_map = false - val = Sequence.new - e.call( val ) - self << "#{type} " if type.length.nonzero? - - # - # Empty arrays - # - if val.length.zero? - self << "[]" - else - # FIXME - # if @buffer.length == 1 and options(:UseHeader) == false and type.length.zero? - # @headless = 1 - # end - - # - # Emit the key and value - # - val.each { |v| - self << "\n" - indent! - self << "- " - @seq_map = true if v.class == Hash - v.to_yaml( :Emitter => self ) - } - end - end + val.each { |v| + seq_map_shortcut + if v[0].is_complex_yaml? + self << "? " + end + v[0].to_yaml( :Emitter => self ) + if v[0].is_complex_yaml? + self << "\n" + indent! + end + self << ": " + v[1].to_yaml( :Emitter => self ) + } + end + end + def seq_map_shortcut + # FIXME: seq_map needs to work with the new anchoring system + # if @seq_map + # @anchor_extras[@buffer.length - 1] = "\n" + indent + # @seq_map = false + # else + self << "\n" + indent! + # end end # - # Emitter helper classes + # Quick sequence # - class Mapping < Array - def add( k, v ) - push [k, v] - end - end + def seq( type, &e ) + @seq_map = false + val = Sequence.new + e.call( val ) + self << "#{type} " if type.length.nonzero? + + # + # Empty arrays + # + if val.length.zero? + self << "[]" + else + # FIXME + # if @buffer.length == 1 and options(:UseHeader) == false and type.length.zero? + # @headless = 1 + # end - class Sequence < Array - def add( v ) - push v - end + # + # Emit the key and value + # + val.each { |v| + self << "\n" + indent! + self << "- " + @seq_map = true if v.class == Hash + v.to_yaml( :Emitter => self ) + } + end + end + end + + # + # Emitter helper classes + # + class Mapping < Array + def add( k, v ) + push [k, v] end + end + class Sequence < Array + def add( v ) + push v + end + end end diff --git a/lib/yaml/yamlnode.rb b/lib/yaml/yamlnode.rb index e36a18e694..8afa142669 100644 --- a/lib/yaml/yamlnode.rb +++ b/lib/yaml/yamlnode.rb @@ -11,13 +11,13 @@ module YAML class YamlNode include BaseNode attr_accessor :kind, :type_id, :value, :anchor - def initialize( t, v ) + def initialize(t, v) @type_id = t if Hash === v @kind = 'map' @value = {} - v.each { |k,v| - @value[ k.transform ] = [ k, v ] + v.each {|key,val| + @value[key.transform] = [key, val] } elsif Array === v @kind = 'seq' |