FloatString
FloatString is essentially a String but it allows unlimited string insertion between string segments.
NOTE This library is still expiremental.
Methods
- []
- []=
- blank
- empty
- fill
- inner_append
- inner_insert
- new
- outer_append
- outer_insert
- re_enumerate
- renumerate
- to_s
- to_str
- undo
Public Class methods
[ show source ]
# File lib/facets/more/floatstring.rb, line 35 def initialize( str ) @str = str @float = {} i = 0 while i < @str.length @float[i.to_f] = @str[i,1] i += 1 end end
Public Instance methods
an inner and outer wrap method would be nice
[ show source ]
# File lib/facets/more/floatstring.rb, line 103 def [](arg) if arg.kind_of?(Range) #r = Range.new(arg.first.to_f, arg.last.to_f, arg.exclude_end?) a = @float.to_a.sort_by{ |k,v| k } s = a.index(a.find{ |e| e[0] == arg.first.to_f}) f = a.index(a.find{ |e| e[0] == arg.last.to_f}) a = arg.exclude_end? ? a[s...f] : a[s..f] a.collect{ |k,v| v }.join('') else @float[arg.to_f] end end
[ show source ]
# File lib/facets/more/floatstring.rb, line 116 def []=(arg,v) @float[arg.to_f] = v end
[ show source ]
# File lib/facets/more/floatstring.rb, line 133 def blank(rng) fill(' ', rng) end
[ show source ]
# File lib/facets/more/floatstring.rb, line 129 def empty(rng) fill('', rng) end
[ show source ]
# File lib/facets/more/floatstring.rb, line 120 def fill(val, rng=0..-1) a = @float.to_a.sort_by{ |k,v| k } s = a.index( a.find{ |e| e[0] == rng.first.to_f } ) f = a.index( a.find{ |e| e[0] == rng.last.to_f } ) x = (rng.exclude_end? ? a[s...f] : a[s..f]) x.each{ |k,v| @float[k] = val.to_s } self.to_s end
[ show source ]
# File lib/facets/more/floatstring.rb, line 83 def inner_append(s, i) n = 0.5; i = i.to_f + 0.5 while @float.has_key?(i) n = n/2 i -= n end @float[i] = s end
these should probably check the decimal and start there rather then startint at 0.5
[ show source ]
# File lib/facets/more/floatstring.rb, line 65 def inner_insert(s, i) n = 0.5; i = i.to_f - n while @float.has_key?(i) n = n/2 i += n end @float[i] = s end
[ show source ]
# File lib/facets/more/floatstring.rb, line 92 def outer_append(s, i) n = 0.5; i = i.to_f + 0.5 while @float.has_key?(i) n = n/2 i += n end @float[i] = s end
[ show source ]
# File lib/facets/more/floatstring.rb, line 74 def outer_insert(s, i) n = 0.5; i = i.to_f - 0.5 while @float.has_key?(i) n = n/2 i -= n end @float[i] = s end
This method is also aliased as
renumerate
[ show source ]
# File lib/facets/more/floatstring.rb, line 49 def re_enumerate initialize( to_s ) end
Alias for re_enumerate
[ show source ]
# File lib/facets/more/floatstring.rb, line 54 def to_s @float.to_a.sort_by{ |k,v| k }.collect{ |k,v| v }.join('') end
[ show source ]
# File lib/facets/more/floatstring.rb, line 58 def to_str @float.to_a.sort_by{ |k,v| k }.collect{ |k,v| v }.join('') end
[ show source ]
# File lib/facets/more/floatstring.rb, line 45 def undo initialize( @str ) end