Class: FloatString

Inherits:
Object show all
Defined in:
lib/carat/floatstring.rb

Overview

FloatString

FloatString allows unlimited string insertion between string segments.

Note: This is usable, it is still a very new idea, and therefore has a much greater potential for change. If you decide it is useful to you, please contribute to its improvement. Thanks.

Author(s)

  • Thomas Sawyer

History

  • 2005.04.11 Passes basic tests.

Instance Method Summary collapse

Constructor Details

#initialize(str) ⇒ FloatString

Returns a new instance of FloatString.



23
24
25
26
27
28
29
30
31
# File 'lib/carat/floatstring.rb', line 23

def initialize( str )
  @str = str
  @float = {}
  i = 0
  while i < @str.length
    @float[i.to_f] = @str[i,1]
    i += 1
  end 
end

Instance Method Details

#[](arg) ⇒ Object

an inner and outer wrap method would be nice



90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/carat/floatstring.rb', line 90

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

#[]=(arg, v) ⇒ Object



103
104
105
# File 'lib/carat/floatstring.rb', line 103

def []=(arg,v)
  @float[arg.to_f] = v
end

#blank(rng) ⇒ Object



120
121
122
# File 'lib/carat/floatstring.rb', line 120

def blank(rng)
  fill(' ', rng)
end

#empty(rng) ⇒ Object



116
117
118
# File 'lib/carat/floatstring.rb', line 116

def empty(rng)
  fill('', rng)
end

#fill(val, rng = 0..-1)) ⇒ Object



107
108
109
110
111
112
113
114
# File 'lib/carat/floatstring.rb', line 107

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

#inner_append(s, i) ⇒ Object



70
71
72
73
74
75
76
77
# File 'lib/carat/floatstring.rb', line 70

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

#inner_insert(s, i) ⇒ Object

these should probably check the decimal and start there rather then startint at 0.5



52
53
54
55
56
57
58
59
# File 'lib/carat/floatstring.rb', line 52

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

#outer_append(s, i) ⇒ Object



79
80
81
82
83
84
85
86
# File 'lib/carat/floatstring.rb', line 79

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

#outer_insert(s, i) ⇒ Object



61
62
63
64
65
66
67
68
# File 'lib/carat/floatstring.rb', line 61

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

#re_enumerateObject



37
38
39
# File 'lib/carat/floatstring.rb', line 37

def re_enumerate
  initialize( to_s )
end

#to_sObject



41
42
43
# File 'lib/carat/floatstring.rb', line 41

def to_s
  @float.to_a.sort_by{ |k,v| k }.collect{ |k,v| v }.join('')
end

#to_strObject



45
46
47
# File 'lib/carat/floatstring.rb', line 45

def to_str
  @float.to_a.sort_by{ |k,v| k }.collect{ |k,v| v }.join('')
end

#undoObject



33
34
35
# File 'lib/carat/floatstring.rb', line 33

def undo
  initialize( @str )
end