Permalink
Browse files

Struct - spun and twirled

  • Loading branch information...
Evan Phoenix
Evan Phoenix committed Apr 30, 2011
1 parent 8749138 commit b513d136c194db230ece0a9cf3a2f7afac9790e5
Showing with 33 additions and 25 deletions.
  1. +30 −22 kernel/common/struct.rb
  2. +3 −3 kernel/common/symbol.rb
View
@@ -39,7 +39,7 @@ class << self
# name="Dave", address="123 Main">
def self.new(klass_name, *attrs, &block)
- unless klass_name.nil? then
+ if klass_name
begin
klass_name = StringValue klass_name
rescue TypeError
@@ -48,19 +48,21 @@ def self.new(klass_name, *attrs, &block)
end
end
- attrs = attrs.map do |attr|
- case attr
+ attrs = attrs.map do |a|
+ case a
when Symbol
- attr
+ a
when String
- attr.to_sym
+ sym = a.to_sym
+ unless sym.kind_of? Symbol
+ raise TypeError, "#to_sym didn't return a symbol"
+ end
+ sym
else
- raise TypeError, "#{attr.inspect} is not a symbol"
+ raise TypeError, "#{a.inspect} is not a symbol"
end
end
- raise ArgumentError if attrs.any? { |attr| attr.nil? }
-
klass = Class.new self do
attr_accessor(*attrs)
@@ -84,10 +86,6 @@ def self.[](*args)
return klass
end
- def self.allocate # :nodoc:
- super
- end
-
# Don't specialize any thing created in the kernel. We hook up
# better form of this in delta.
def self.specialize_initialize
@@ -205,8 +203,15 @@ def [](var)
def []=(var, obj)
case var
- when Symbol, String
- # ok
+ when Symbol
+ unless _attrs.include? var
+ raise NameError, "no member '#{var}' in struct"
+ end
+ when String
+ var = var.to_sym
+ unless _attrs.include? var
+ raise NameError, "no member '#{var}' in struct"
+ end
else
var = Integer(var)
a_len = _attrs.length
@@ -220,10 +225,6 @@ def []=(var, obj)
var = _attrs[var]
end
- unless _attrs.include? var.to_s.intern then
- raise NameError, "no member '#{var}' in struct"
- end
-
return instance_variable_set(:"@#{var}", obj)
end
@@ -240,9 +241,10 @@ def eql?(other)
Thread.detect_recursion self, other do
_attrs.each do |var|
- unless instance_variable_get(:"@#{var}").eql? other.instance_variable_get(:"@#{var}")
- return false
- end
+ mine = instance_variable_get(:"@#{var}")
+ theirs = other.instance_variable_get(:"@#{var}")
+
+ return false unless mine.eql? theirs
end
end
@@ -363,7 +365,13 @@ def to_s
return "[...]" if Thread.guarding? self
Thread.recursion_guard self do
- values = _attrs.zip(self.to_a).map { |o| o[1] = o[1].inspect; o.join('=') }
+ values = []
+
+ _attrs.each do |var|
+ val = instance_variable_get :"@#{var}"
+ values << "#{var}=#{val.inspect}"
+ end
+
"#<struct #{self.class.inspect} #{values.join(', ')}>"
end
end
View
@@ -22,12 +22,12 @@ def self.all_symbols
# Returns a pretty version of the symbol, fit for viewing
# :symbol.inspect #=> ":symbol"
def inspect
- str = self.to_s
+ str = to_s
case str
- when /^(\$|@@?)[a-z_][a-z_\d]*$/i, # Variable names
+ when /^(\$|@@?)[a-z_][a-z_\d]*$/i, # Variable names
/^[a-z_][a-z_\d]*[=?!]?$/i, # Method names
- /^\$(-[a-z_\d]|[+~:?<_\/'"$.,`!;\\=*>&@]|\d+)$/i, # Special global variables
+ /^\$(-[a-z_\d]|[+~:?<_\/'"$.,`!;\\=*>&@]|\d+)$/i, # Special global variables
/^([|^&\/%~`]|<<|>>|<=>|===?|=~|[<>]=?|[+-]@?|\*\*?|\[\]=?)$/ # Operators
":#{str}"
else

0 comments on commit b513d13

Please sign in to comment.