* 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
This commit is contained in:
matz 2008-12-18 07:54:50 +00:00
parent cd5c309542
commit 85bae86cb6
14 changed files with 319 additions and 330 deletions

View File

@ -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> 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 * lib/xmlrpc/server.rb: Restricting method inspection to show only

View File

@ -159,7 +159,7 @@ module IRB
end end
next if name != "IRB::Context" and next if name != "IRB::Context" and
/^(IRB|SLex|RubyLex|RubyToken)/ =~ name /^(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.sort!
candidates.uniq! candidates.uniq!

View File

@ -56,6 +56,6 @@ module IRB
# end # end
# end # end
# alias change_workspace change_binding # alias change_workspace change_binding
end end
end end

View File

@ -140,10 +140,8 @@ class Matrix
# #
# #
def Matrix.columns(columns) def Matrix.columns(columns)
rows = (0 .. columns[0].size - 1).collect { rows = (0 .. columns[0].size - 1).collect {|i|
|i| (0 .. columns.size - 1).collect {|j|
(0 .. columns.size - 1).collect {
|j|
columns[j][i] columns[j][i]
} }
} }
@ -159,8 +157,7 @@ class Matrix
# #
def Matrix.diagonal(*values) def Matrix.diagonal(*values)
size = values.size size = values.size
rows = (0 .. size - 1).collect { rows = (0 .. size - 1).collect {|j|
|j|
row = Array.new(size).fill(0, 0, size) row = Array.new(size).fill(0, 0, size)
row[j] = values[j] row[j] = values[j]
row row
@ -311,13 +308,11 @@ class Matrix
# #
def column(j) # :yield: e def column(j) # :yield: e
if block_given? if block_given?
0.upto(row_size - 1) do 0.upto(row_size - 1) do |i|
|i|
yield @rows[i][j] yield @rows[i][j]
end end
else else
col = (0 .. row_size - 1).collect { col = (0 .. row_size - 1).collect {|i|
|i|
@rows[i][j] @rows[i][j]
} }
Vector.elements(col, false) Vector.elements(col, false)
@ -364,8 +359,7 @@ class Matrix
Matrix.Raise ArgumentError, param.inspect Matrix.Raise ArgumentError, param.inspect
end end
rows = @rows[from_row, size_row].collect{ rows = @rows[from_row, size_row].collect{|row|
|row|
row[from_col, size_col] row[from_col, size_col]
} }
Matrix.rows(rows, false) Matrix.rows(rows, false)
@ -421,8 +415,7 @@ class Matrix
def compare_by_row_vectors(rows, comparison = :==) def compare_by_row_vectors(rows, comparison = :==)
return false unless @rows.size == rows.size return false unless @rows.size == rows.size
0.upto(@rows.size - 1) do 0.upto(@rows.size - 1) do |i|
|i|
return false unless @rows[i].send(comparison, rows[i]) return false unless @rows[i].send(comparison, rows[i])
end end
true true
@ -462,10 +455,8 @@ class Matrix
def *(m) # m is matrix or vector or number def *(m) # m is matrix or vector or number
case(m) case(m)
when Numeric when Numeric
rows = @rows.collect { rows = @rows.collect {|row|
|row| row.collect {|e|
row.collect {
|e|
e * m e * m
} }
} }
@ -477,13 +468,10 @@ class Matrix
when Matrix when Matrix
Matrix.Raise ErrDimensionMismatch if column_size != m.row_size Matrix.Raise ErrDimensionMismatch if column_size != m.row_size
rows = (0 .. row_size - 1).collect { rows = (0 .. row_size - 1).collect {|i|
|i| (0 .. m.column_size - 1).collect {|j|
(0 .. m.column_size - 1).collect {
|j|
vij = 0 vij = 0
0.upto(column_size - 1) do 0.upto(column_size - 1) do |k|
|k|
vij += self[i, k] * m[k, j] vij += self[i, k] * m[k, j]
end end
vij vij
@ -516,10 +504,8 @@ class Matrix
Matrix.Raise ErrDimensionMismatch unless row_size == m.row_size and column_size == m.column_size Matrix.Raise ErrDimensionMismatch unless row_size == m.row_size and column_size == m.column_size
rows = (0 .. row_size - 1).collect { rows = (0 .. row_size - 1).collect {|i|
|i| (0 .. column_size - 1).collect {|j|
(0 .. column_size - 1).collect {
|j|
self[i, j] + m[i, 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 Matrix.Raise ErrDimensionMismatch unless row_size == m.row_size and column_size == m.column_size
rows = (0 .. row_size - 1).collect { rows = (0 .. row_size - 1).collect {|i|
|i| (0 .. column_size - 1).collect {|j|
(0 .. column_size - 1).collect {
|j|
self[i, j] - m[i, j] self[i, j] - m[i, j]
} }
} }
@ -565,10 +549,8 @@ class Matrix
def /(other) def /(other)
case other case other
when Numeric when Numeric
rows = @rows.collect { rows = @rows.collect {|row|
|row| row.collect {|e|
row.collect {
|e|
e / other e / other
} }
} }
@ -603,7 +585,7 @@ class Matrix
for k in 0..size for k in 0..size
i = k i = k
akk = a[k][k].abs akk = a[k][k].abs
for j in (k+1)..size ((k+1)..size).each do |j|
v = a[j][k].abs v = a[j][k].abs
if v > akk if v > akk
i = j i = j
@ -697,12 +679,13 @@ class Matrix
det = 1 det = 1
k = 0 k = 0
begin loop do
if (akk = a[k][k]) == 0 if (akk = a[k][k]) == 0
i = k i = k
begin loop do
return 0 if (i += 1) > size return 0 if (ii += 1) > size
end while a[i][k] == 0 break unless a[i][k] == 0
end
a[i], a[k] = a[k], a[i] a[i], a[k] = a[k], a[i]
akk = a[k][k] akk = a[k][k]
det *= -1 det *= -1
@ -710,13 +693,13 @@ class Matrix
for i in k + 1 .. size for i in k + 1 .. size
q = a[i][k].quo(akk) q = a[i][k].quo(akk)
(k + 1).upto(size) do (k + 1).upto(size) do |j|
|j|
a[i][j] -= a[k][j] * q a[i][j] -= a[k][j] * q
end end
end end
det *= akk det *= akk
end while (k += 1) <= size break unless (k += 1) <= size
end
det det
end end
alias det determinant alias det determinant
@ -739,12 +722,13 @@ class Matrix
det = 1 det = 1
k = 0 k = 0
begin loop do
if a[k][k].zero? if a[k][k].zero?
i = k i = k
begin loop do
return 0 if (i += 1) > size 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] a[i], a[k] = a[k], a[i]
det *= -1 det *= -1
end end
@ -761,7 +745,8 @@ class Matrix
end end
end end
det *= a[k][k] det *= a[k][k]
end while (k += 1) <= size break unless (k += 1) <= size
end
det det
end end
alias det_e determinant_e alias det_e determinant_e
@ -786,31 +771,32 @@ class Matrix
end end
rank = 0 rank = 0
k = 0 k = 0
begin loop do
if (akk = a[k][k]) == 0 if (akk = a[k][k]) == 0
i = k i = k
exists = true exists = true
begin loop do
if (i += 1) > a_column_size - 1 if (i += 1) > a_column_size - 1
exists = false exists = false
break break
end end
end while a[i][k] == 0 break unless a[i][k] == 0
end
if exists if exists
a[i], a[k] = a[k], a[i] a[i], a[k] = a[k], a[i]
akk = a[k][k] akk = a[k][k]
else else
i = k i = k
exists = true exists = true
begin loop do
if (i += 1) > a_row_size - 1 if (i += 1) > a_row_size - 1
exists = false exists = false
break break
end end
end while a[k][i] == 0 break unless a[k][i] == 0
end
if exists if exists
k.upto(a_column_size - 1) do k.upto(a_column_size - 1) do |j|
|j|
a[j][k], a[j][i] = a[j][i], a[j][k] a[j][k], a[j][i] = a[j][i], a[j][k]
end end
akk = a[k][k] akk = a[k][k]
@ -827,7 +813,8 @@ class Matrix
end end
end end
rank += 1 rank += 1
end while (k += 1) <= a_column_size - 1 break unless (k += 1) <= a_column_size - 1
end
return rank return rank
end end
@ -873,8 +860,7 @@ class Matrix
# #
def trace def trace
tr = 0 tr = 0
0.upto(column_size - 1) do 0.upto(column_size - 1) do |i|
|i|
tr += @rows[i][i] tr += @rows[i][i]
end end
tr tr
@ -916,8 +902,7 @@ class Matrix
# Returns an array of the row vectors of the matrix. See Vector. # Returns an array of the row vectors of the matrix. See Vector.
# #
def row_vectors def row_vectors
rows = (0 .. row_size - 1).collect { rows = (0 .. row_size - 1).collect {|i|
|i|
row(i) row(i)
} }
rows rows
@ -927,8 +912,7 @@ class Matrix
# Returns an array of the column vectors of the matrix. See Vector. # Returns an array of the column vectors of the matrix. See Vector.
# #
def column_vectors def column_vectors
columns = (0 .. column_size - 1).collect { columns = (0 .. column_size - 1).collect {|i|
|i|
column(i) column(i)
} }
columns columns
@ -961,8 +945,7 @@ class Matrix
# Overrides Object#to_s # Overrides Object#to_s
# #
def to_s def to_s
"Matrix[" + @rows.collect{ "Matrix[" + @rows.collect{|row|
|row|
"[" + row.collect{|e| e.to_s}.join(", ") + "]" "[" + row.collect{|e| e.to_s}.join(", ") + "]"
}.join(", ")+"]" }.join(", ")+"]"
end end
@ -1169,8 +1152,7 @@ class Vector
# #
def each2(v) # :yield: e1, e2 def each2(v) # :yield: e1, e2
Vector.Raise ErrDimensionMismatch if size != v.size Vector.Raise ErrDimensionMismatch if size != v.size
0.upto(size - 1) do 0.upto(size - 1) do |i|
|i|
yield @elements[i], v[i] yield @elements[i], v[i]
end end
end end
@ -1181,8 +1163,7 @@ class Vector
# #
def collect2(v) # :yield: e1, e2 def collect2(v) # :yield: e1, e2
Vector.Raise ErrDimensionMismatch if size != v.size Vector.Raise ErrDimensionMismatch if size != v.size
(0 .. size - 1).collect do (0 .. size - 1).collect do |i|
|i|
yield @elements[i], v[i] yield @elements[i], v[i]
end end
end end
@ -1253,8 +1234,7 @@ class Vector
case v case v
when Vector when Vector
Vector.Raise ErrDimensionMismatch if size != v.size Vector.Raise ErrDimensionMismatch if size != v.size
els = collect2(v) { els = collect2(v) {|v1, v2|
|v1, v2|
v1 + v2 v1 + v2
} }
Vector.elements(els, false) Vector.elements(els, false)
@ -1273,8 +1253,7 @@ class Vector
case v case v
when Vector when Vector
Vector.Raise ErrDimensionMismatch if size != v.size Vector.Raise ErrDimensionMismatch if size != v.size
els = collect2(v) { els = collect2(v) {|v1, v2|
|v1, v2|
v1 - v2 v1 - v2
} }
Vector.elements(els, false) Vector.elements(els, false)
@ -1298,8 +1277,7 @@ class Vector
Vector.Raise ErrDimensionMismatch if size != v.size Vector.Raise ErrDimensionMismatch if size != v.size
p = 0 p = 0
each2(v) { each2(v) {|v1, v2|
|v1, v2|
p += v1 * v2 p += v1 * v2
} }
p p
@ -1309,8 +1287,7 @@ class Vector
# Like Array#collect. # Like Array#collect.
# #
def collect # :yield: e def collect # :yield: e
els = @elements.collect { els = @elements.collect {|v|
|v|
yield v yield v
} }
Vector.elements(els, false) Vector.elements(els, false)
@ -1321,8 +1298,7 @@ class Vector
# Like Vector#collect2, but returns a Vector instead of an Array. # Like Vector#collect2, but returns a Vector instead of an Array.
# #
def map2(v) # :yield: e1, e2 def map2(v) # :yield: e1, e2
els = collect2(v) { els = collect2(v) {|v1, v2|
|v1, v2|
yield v1, v2 yield v1, v2
} }
Vector.elements(els, false) Vector.elements(els, false)

View File

@ -3216,7 +3216,7 @@ module Net
].join(':') ].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 when STAGE_TWO
@stage = nil @stage = nil
# if at the second stage, return an empty string # if at the second stage, return an empty string

View File

@ -1,7 +1,7 @@
# OptionParser internal utility # OptionParser internal utility
class << OptionParser class << OptionParser
def show_version(*pkg) def show_version(*pkgs)
progname = ARGV.options.program_name progname = ARGV.options.program_name
result = false result = false
show = proc do |klass, cname, version| show = proc do |klass, cname, version|
@ -19,14 +19,14 @@ class << OptionParser
puts str puts str
result = true result = true
end 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| self.search_const(::Object, /\AV(?:ERSION|ersion)\z/) do |klass, cname, version|
unless cname[1] == ?e and klass.const_defined?(:Version) unless cname[1] == ?e and klass.const_defined?(:Version)
show.call(klass, cname.intern, version) show.call(klass, cname.intern, version)
end end
end end
else else
pkg.each do |pkg| pkgs.each do |pkg|
begin begin
pkg = pkg.split(/::|\//).inject(::Object) {|m, c| m.const_get(c)} pkg = pkg.split(/::|\//).inject(::Object) {|m, c| m.const_get(c)}
v = case v = case
@ -46,8 +46,8 @@ class << OptionParser
result result
end end
def each_const(path, klass = ::Object) def each_const(path, base = ::Object)
path.split(/::|\//).inject(klass) do |klass, name| path.split(/::|\//).inject(base) do |klass, name|
raise NameError, path unless Module === klass raise NameError, path unless Module === klass
klass.constants.grep(/#{name}/i) do |c| klass.constants.grep(/#{name}/i) do |c|
klass.const_defined?(c) or next klass.const_defined?(c) or next

View File

@ -411,8 +411,8 @@ class Prime
loop do loop do
extend_table until @table.length > i extend_table until @table.length > i
if !@table[i].zero? if !@table[i].zero?
(j...32).step(2) do |j| (j...32).step(2) do |k|
return 32*i+j if !@table[i][j.div(2)].zero? return 32*i+k if !@table[i][k.div(2)].zero?
end end
end end
i += 1; j = 1 i += 1; j = 1

View File

@ -159,7 +159,6 @@ module Racc
reduce_n, use_result, * = arg reduce_n, use_result, * = arg
_racc_init_sysvars _racc_init_sysvars
tok = nil
act = nil act = nil
i = nil i = nil
nerr = 0 nerr = 0
@ -189,7 +188,7 @@ module Racc
; ;
end end
while not (i = action_pointer[@racc_state[-1]]) or while not(i = action_pointer[@racc_state[-1]]) or
not @racc_read_next or not @racc_read_next or
@racc_t == 0 # $ @racc_t == 0 # $
unless i and i += @racc_t and unless i and i += @racc_t and

View File

@ -436,7 +436,7 @@ module REXML
arry[-1][-1].event_arg = evt[1] arry[-1][-1].event_arg = evt[1]
@value = false @value = false
end end
else else
arry << [] if evt[0] == :start_element arry << [] if evt[0] == :start_element
arry[-1] << generate_event( evt ) arry[-1] << generate_event( evt )

View File

@ -256,15 +256,15 @@ if __FILE__ == $0
$stdin.gets $stdin.gets
when 'w' when 'w'
finger = Rinda::RingFinger.new(nil) finger = Rinda::RingFinger.new(nil)
finger.lookup_ring do |ts| finger.lookup_ring do |ts2|
p ts p ts2
ts.write([:hello, :world]) ts2.write([:hello, :world])
end end
when 'r' when 'r'
finger = Rinda::RingFinger.new(nil) finger = Rinda::RingFinger.new(nil)
finger.lookup_ring do |ts| finger.lookup_ring do |ts2|
p ts p ts2
p ts.take([nil, nil]) p ts2.take([nil, nil])
end end
end end
end end

View File

@ -26,7 +26,7 @@ class Shell
# #
m = [:initialize, :expand_path] m = [:initialize, :expand_path]
if Object.methods.first.kind_of?(String) if Object.methods.first.kind_of?(String)
NoDelegateMethods = m.collect{|m| m.id2name} NoDelegateMethods = m.collect{|x| x.id2name}
else else
NoDelegateMethods = m NoDelegateMethods = m
end end
@ -124,7 +124,7 @@ class Shell
f = File.open(path, mode, perm) f = File.open(path, mode, perm)
File.chmod(perm & ~@shell.umask, path) File.chmod(perm & ~@shell.umask, path)
if block_given? if block_given?
f.each &b f.each(&b)
end end
f f
else else

View File

@ -62,7 +62,7 @@ class Shell
end end
def block_output_synchronize(&b) def block_output_synchronize(&b)
@BlockOutputMonitor.synchronize &b @BlockOutputMonitor.synchronize(&b)
end end
def wait_to_finish_all_process_controllers def wait_to_finish_all_process_controllers

View File

@ -7,241 +7,236 @@ require 'yaml/encoding'
require 'yaml/error' require 'yaml/error'
module YAML 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
def options( opt = nil )
if opt
@options[opt] || YAML::DEFAULTS[opt]
else
@options
end
end
def options=( opt )
@options = opt
end
#
# Emit binary data
#
def binary_base64( value )
self << "!binary "
self.node_text( [value].pack("m"), '|' )
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
#
# 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
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
#
# Quick sequence
#
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
end end
# #
# Emitter helper classes # Emit binary data
# #
class Mapping < Array def binary_base64( value )
def add( k, v ) self << "!binary "
push [k, v] self.node_text( [value].pack("m"), '|' )
end
end end
class Sequence < Array #
def add( v ) # Emit plain, normal flowing text
push v #
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 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 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
#
# 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
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
#
# Quick sequence
#
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
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 end

View File

@ -11,13 +11,13 @@ module YAML
class YamlNode class YamlNode
include BaseNode include BaseNode
attr_accessor :kind, :type_id, :value, :anchor attr_accessor :kind, :type_id, :value, :anchor
def initialize( t, v ) def initialize(t, v)
@type_id = t @type_id = t
if Hash === v if Hash === v
@kind = 'map' @kind = 'map'
@value = {} @value = {}
v.each { |k,v| v.each {|key,val|
@value[ k.transform ] = [ k, v ] @value[key.transform] = [key, val]
} }
elsif Array === v elsif Array === v
@kind = 'seq' @kind = 'seq'