Class: FloatString

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

Overview

:title: FloatString

FloatString allows unlimited string insertion between string segments.

Usage

TODO: Comming soon.....

Notes

Although 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

Instance Method Summary collapse

Constructor Details

#initialize(str) ⇒ FloatString

Returns a new instance of FloatString.



44
45
46
47
48
49
50
51
52
# File 'lib/mega/floatstring.rb', line 44

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



112
113
114
115
116
117
118
119
120
121
122
123
# File 'lib/mega/floatstring.rb', line 112

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



125
126
127
# File 'lib/mega/floatstring.rb', line 125

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

#blank(rng) ⇒ Object



142
143
144
# File 'lib/mega/floatstring.rb', line 142

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

#empty(rng) ⇒ Object



138
139
140
# File 'lib/mega/floatstring.rb', line 138

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

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



129
130
131
132
133
134
135
136
# File 'lib/mega/floatstring.rb', line 129

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



92
93
94
95
96
97
98
99
# File 'lib/mega/floatstring.rb', line 92

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



74
75
76
77
78
79
80
81
# File 'lib/mega/floatstring.rb', line 74

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



101
102
103
104
105
106
107
108
# File 'lib/mega/floatstring.rb', line 101

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



83
84
85
86
87
88
89
90
# File 'lib/mega/floatstring.rb', line 83

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 Also known as: renumerate



58
59
60
# File 'lib/mega/floatstring.rb', line 58

def re_enumerate
  initialize( to_s )
end

#to_sObject



63
64
65
# File 'lib/mega/floatstring.rb', line 63

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

#to_strObject



67
68
69
# File 'lib/mega/floatstring.rb', line 67

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

#undoObject



54
55
56
# File 'lib/mega/floatstring.rb', line 54

def undo
  initialize( @str )
end