lib/json/ld/extensions.rb in json-ld-0.1.0 vs lib/json/ld/extensions.rb in json-ld-0.1.2

- old
+ new

@@ -35,10 +35,33 @@ class URI def to_s @uri.to_s end end + + class Literal + class Double + ## + # Converts this literal into its canonical lexical representation. + # Update to use %.15E to avoid precision problems + def canonicalize! + @string = case + when @object.nan? then 'NaN' + when @object.infinite? then @object.to_s[0...-'inity'.length].upcase + when @object.zero? then '0.0E0' + else + i, f, e = ('%.15E' % @object.to_f).split(/[\.E]/) + f.sub!(/0*$/, '') # remove any trailing zeroes + f = '0' if f.empty? # ...but there must be a digit to the right of the decimal point + e.sub!(/^\+?0+(\d)$/, '\1') # remove the optional leading '+' sign and any extra leading zeroes + "#{i}.#{f}E#{e}" + end + @object = Float(@string) unless @object.nil? + self + end + end + end end if RUBY_VERSION < "1.9" class InsertOrderPreservingHash < Hash include Enumerable @@ -52,11 +75,11 @@ @ordered_keys << key unless has_key? key super end def each - @ordered_keys.each {|k| yield(k, super[k])} + @ordered_keys.each {|k| yield(k, self[k])} end alias :each_pair :each def each_value @ordered_keys.each {|k| yield(super[k])} @@ -68,14 +91,10 @@ def keys @ordered_keys end - def values - @ordered_keys.map {|k| super[k]} - end - def clear @ordered_keys.clear super end @@ -95,21 +114,29 @@ reject!(&block) self end def merge!(other) - @ordered_keys += other.instance_variable_get(:@ordered_keys) || other.keys + new_keys = other.instance_variable_get(:@ordered_keys) || other.keys + new_keys -= @ordered_keys + @ordered_keys += new_keys super self end def merge(other) self.dup.merge!(other) end end class Hash - def new(obj = nil, &block) + def self.ordered(obj = nil, &block) InsertOrderPreservingHash.new(obj, &block) + end + end +else + class Hash + def self.ordered(obj = nil, &block) + Hash.new(obj, &block) end end end \ No newline at end of file