summaryrefslogtreecommitdiff
path: root/lib/ostruct.rb
diff options
context:
space:
mode:
authornobu <nobu@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2009-02-15 12:43:46 +0000
committernobu <nobu@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2009-02-15 12:43:46 +0000
commita4eb3395643835b87c504701f2964ae461a5727c (patch)
tree6100df5506b6fcea160eaec74999fb25627b2b86 /lib/ostruct.rb
parent8fc45476ebf80e8da9c50ff441e623c1550799e6 (diff)
* lib/ostruct.rb (OpenStruct#new_ostruct_member): checks if frozen.
[ruby-talk:328195], [ruby-core:22142] git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@22332 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
Diffstat (limited to 'lib/ostruct.rb')
-rw-r--r--lib/ostruct.rb20
1 files changed, 12 insertions, 8 deletions
diff --git a/lib/ostruct.rb b/lib/ostruct.rb
index 35a14b4..45ebb80 100644
--- a/lib/ostruct.rb
+++ b/lib/ostruct.rb
@@ -67,29 +67,33 @@ class OpenStruct
@table.each_key{|key| new_ostruct_member(key)}
end
+ def modifiable
+ if self.frozen?
+ raise TypeError, "can't modify frozen #{self.class}", caller(2)
+ end
+ @table
+ end
+ protected :modifiable
+
def new_ostruct_member(name)
name = name.to_sym
unless self.respond_to?(name)
class << self; self; end.class_eval do
define_method(name) { @table[name] }
- define_method(:"#{name}=") { |x| @table[name] = x }
+ define_method("#{name}=") { |x| modifiable[name] = x }
end
end
+ name
end
def method_missing(mid, *args) # :nodoc:
mname = mid.id2name
len = args.length
- if mname =~ /=$/
+ if mname.chomp!('=')
if len != 1
raise ArgumentError, "wrong number of arguments (#{len} for 1)", caller(1)
end
- if self.frozen?
- raise TypeError, "can't modify frozen #{self.class}", caller(1)
- end
- mname.chop!
- self.new_ostruct_member(mname)
- @table[mname.intern] = args[0]
+ modifiable[new_ostruct_member(mname)] = args[0]
elsif len == 0
@table[mid]
else