mirror of
https://github.com/ruby/ruby.git
synced 2022-11-09 12:17:21 -05:00
* lib/ostruct.rb: a patch from Florian Gross <florgro@gmail.com>
merged to allow recursive inspect (and to_s) for OpenStruct. [ruby-core:05532] git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/branches/ruby_1_8@9079 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
parent
260231d3f6
commit
fe5473f27c
1 changed files with 30 additions and 11 deletions
|
@ -47,7 +47,7 @@ class OpenStruct
|
||||||
@table = {}
|
@table = {}
|
||||||
if hash
|
if hash
|
||||||
for k,v in hash
|
for k,v in hash
|
||||||
@table[k.to_sym] = v
|
@table[k.to_sym] = v
|
||||||
new_ostruct_member(k)
|
new_ostruct_member(k)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
@ -68,11 +68,11 @@ class OpenStruct
|
||||||
end
|
end
|
||||||
|
|
||||||
def new_ostruct_member(name)
|
def new_ostruct_member(name)
|
||||||
|
name = name.to_sym
|
||||||
unless self.respond_to?(name)
|
unless self.respond_to?(name)
|
||||||
self.instance_eval %{
|
meta = class << self; self; end
|
||||||
def #{name}; @table[:#{name}]; end
|
meta.send(:define_method, name) { @table[name] }
|
||||||
def #{name}=(x); @table[:#{name}] = x; end
|
meta.send(:define_method, :"#{name}=") { |x| @table[name] = x }
|
||||||
}
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -81,14 +81,14 @@ class OpenStruct
|
||||||
len = args.length
|
len = args.length
|
||||||
if mname =~ /=$/
|
if mname =~ /=$/
|
||||||
if len != 1
|
if len != 1
|
||||||
raise ArgumentError, "wrong number of arguments (#{len} for 1)", caller(1)
|
raise ArgumentError, "wrong number of arguments (#{len} for 1)", caller(1)
|
||||||
end
|
end
|
||||||
if self.frozen?
|
if self.frozen?
|
||||||
raise TypeError, "can't modify frozen #{self.class}", caller(1)
|
raise TypeError, "can't modify frozen #{self.class}", caller(1)
|
||||||
end
|
end
|
||||||
mname.chop!
|
mname.chop!
|
||||||
@table[mname.intern] = args[0]
|
|
||||||
self.new_ostruct_member(mname)
|
self.new_ostruct_member(mname)
|
||||||
|
@table[mname.intern] = args[0]
|
||||||
elsif len == 0
|
elsif len == 0
|
||||||
@table[mid]
|
@table[mid]
|
||||||
else
|
else
|
||||||
|
@ -103,16 +103,35 @@ class OpenStruct
|
||||||
@table.delete name.to_sym
|
@table.delete name.to_sym
|
||||||
end
|
end
|
||||||
|
|
||||||
|
InspectKey = :__inspect_key__ # :nodoc:
|
||||||
|
|
||||||
#
|
#
|
||||||
# Returns a string containing a detailed summary of the keys and values.
|
# Returns a string containing a detailed summary of the keys and values.
|
||||||
#
|
#
|
||||||
def inspect
|
def inspect
|
||||||
str = "<#{self.class}"
|
str = "#<#{self.class}"
|
||||||
for k,v in @table
|
|
||||||
str << " #{k}=#{v.inspect}"
|
Thread.current[InspectKey] ||= []
|
||||||
|
if Thread.current[InspectKey].include?(self) then
|
||||||
|
str << " ..."
|
||||||
|
else
|
||||||
|
first = true
|
||||||
|
for k,v in @table
|
||||||
|
str << "," unless first
|
||||||
|
first = false
|
||||||
|
|
||||||
|
Thread.current[InspectKey] << v
|
||||||
|
begin
|
||||||
|
str << " #{k}=#{v.inspect}"
|
||||||
|
ensure
|
||||||
|
Thread.current[InspectKey].pop
|
||||||
|
end
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
str << ">"
|
str << ">"
|
||||||
end
|
end
|
||||||
|
alias :to_s :inspect
|
||||||
|
|
||||||
attr_reader :table # :nodoc:
|
attr_reader :table # :nodoc:
|
||||||
protected :table
|
protected :table
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue