Class: String

Inherits:
Object show all
Defined in:
lib/rwd/ruby.rb,
lib/zip/stdrubyext.rb

Instance Method Summary collapse

Instance Method Details

#chomp(dummy = nil) ⇒ Object



95
96
97
# File 'lib/rwd/ruby.rb', line 95

def chomp(dummy=nil)
  self.gsub(/[\r\n]*\z/, "")
end

#chomp!(dummy = nil) ⇒ Object



91
92
93
# File 'lib/rwd/ruby.rb', line 91

def chomp!(dummy=nil)
  self.gsub!(/[\r\n]*\z/, "")
end

#compressObject



119
120
121
# File 'lib/rwd/ruby.rb', line 119

def compress
  self.gsub(/[[:blank:]\r\n]+/, " ").strip
end

#compressperlineObject



127
128
129
130
131
132
# File 'lib/rwd/ruby.rb', line 127

def compressperline
  res	= self.split(/\n/)
  res.collect!{|line| line.compress}
  res.delete_if{|line| line.empty?}
  res.join("\n")
end

#compressspacesObject



123
124
125
# File 'lib/rwd/ruby.rb', line 123

def compressspaces
  self.gsub(/[[:blank:]]+/, " ")
end

#crlfObject



103
104
105
# File 'lib/rwd/ruby.rb', line 103

def crlf
  self.gsub(/\r*\n/, "\r\n").gsub(/\r\n\z/, "") + "\r\n"
end

#ends_with(aString) ⇒ Object



40
41
42
# File 'lib/zip/stdrubyext.rb', line 40

def ends_with(aString)
  index(aString, -aString.size)
end

#ensure_end(aString) ⇒ Object



44
45
46
# File 'lib/zip/stdrubyext.rb', line 44

def ensure_end(aString)
  ends_with(aString) ? self : self + aString
end

#evalObject



153
154
155
# File 'lib/rwd/ruby.rb', line 153

def eval
  Kernel::eval(self)
end

#exec(input = nil, output = true) ⇒ Object



140
141
142
143
144
145
146
147
148
149
150
151
# File 'lib/rwd/ruby.rb', line 140

def exec(input=nil, output=true)
  res	= []

  IO.popen(self, "w+") do |f|
    f.puts input	unless input.nil?
    f.close_write

    res	= f.readlines if output
  end

  res.join("")
end

#from_html(eolconversion = true) ⇒ Object



272
273
274
275
276
277
278
279
280
281
282
283
284
285
# File 'lib/rwd/ruby.rb', line 272

def from_html(eolconversion=true)
  s	= self

  s.gsub!(/"/ , "\"")
  s.gsub!(/´/, "\'")

 # s	= CGI.unescapeHTML(self)

  if eolconversion
    s.gsub!(/<br>/, "\n")
  end

  s
end

#lchopObject



48
49
50
# File 'lib/zip/stdrubyext.rb', line 48

def lchop
  slice(1, length)
end

#lfObject



99
100
101
# File 'lib/rwd/ruby.rb', line 99

def lf
  self.gsub(/\r*\n/, "\n").gsub(/\n\z/, "") + "\n"
end

#noquotesObject



255
256
257
# File 'lib/rwd/ruby.rb', line 255

def noquotes
  self.sub(/\A['"]/, "").sub(/['"]\z/, "")
end

#numeric?Boolean

Returns:

  • (Boolean)


134
135
136
137
138
# File 'lib/rwd/ruby.rb', line 134

def numeric?
  d, a, n	= [self].to_par

  not n.empty?
end

#speakObject



157
158
159
160
161
162
# File 'lib/rwd/ruby.rb', line 157

def speak
  require "drb"

  DRb.start_service
  DRbObject.new(nil, "druby://localhost:3100").speak(self)
end

#splitblocks(*delimiters) ⇒ Object



164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
# File 'lib/rwd/ruby.rb', line 164

def splitblocks(*delimiters)
  begindelimiters	= []
  enddelimiters	= []

  delimiters.each do |k, v|
    begindelimiters	<< k.downcase
    enddelimiters	<< v.downcase
  end

  bd	= begindelimiters.collect	{|s| Regexp.escape(s)}
  ed	= enddelimiters.collect		{|s| Regexp.escape(s)}

  be	= bd.join("|")
  ee	= ed.join("|")

  res		= []
  type	= 0
  tmp		= ""
  bs		= ""
  es		= ""

  self.split(/(#{ee}|#{be})/i).each do |s|
    if type == 0
      if begindelimiters.include?(s.downcase)
        i	= begindelimiters.index(s.downcase)
        type	= i+1
        tmp	= s
        bs	= s.downcase
        es	= enddelimiters[i]
      else
        res << [0, s]	unless s.empty?
      end
    else
      if s.downcase == es
        res << [type, tmp + s]
        type	= 0
        tmp	= ""
        bs	= ""
        es	= ""
      else
        if s.downcase == bs
          res << [0, tmp]
          tmp	= s
        else
          tmp	= tmp + s
        end
      end
    end
  end

  res << [0, tmp]	unless tmp.empty?

  return res
end

#splitwords(tokens = []) ⇒ Object



219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
# File 'lib/rwd/ruby.rb', line 219

def splitwords(tokens=[])
  tokens		= [tokens]	unless tokens.kind_of?(Array)
  res			= []

  self.splitblocks(["'", "'"], ['"', '"']).each do |type, s|
    case type
    when 0
      tokens.each do |token|
        token2	= token
        token2	= Regexp.escape(token2)	if token2.kind_of?(String)
        s.gsub!(/#{token2}/, " #{token} ")
      end
      s.split().each do |w|
        res << w
      end
    when 1, 2
      res << s[1..-2]
    end
  end

  return res
end

#starts_with(aString) ⇒ Object



36
37
38
# File 'lib/zip/stdrubyext.rb', line 36

def starts_with(aString)
  rindex(aString, 0) == 0
end

#stripObject



107
108
109
# File 'lib/rwd/ruby.rb', line 107

def strip
  self.stripbefore.stripafter
end

#stripafterObject



115
116
117
# File 'lib/rwd/ruby.rb', line 115

def stripafter
  self.gsub(/[[:blank:]\r\n]*\z/, "")
end

#stripbeforeObject



111
112
113
# File 'lib/rwd/ruby.rb', line 111

def stripbefore
  self.gsub(/\A[[:blank:]\r\n]*/, "")
end

#to_fsObject



287
288
289
290
291
292
293
# File 'lib/rwd/ruby.rb', line 287

def to_fs
  if numeric?
    to_f
  else
    to_s
  end
end

#to_html(eolconversion = true) ⇒ Object



259
260
261
262
263
264
265
266
267
268
269
270
# File 'lib/rwd/ruby.rb', line 259

def to_html(eolconversion=true)
  s	= CGI.escapeHTML(self)

  s.gsub!(/\"/, "\&#34;")
  s.gsub!(/\'/, "\&#180;")

  if eolconversion
    s.gsub!(/\n/ , "<br>")
  end

  s
end

#uncommentObject



242
243
244
245
246
247
248
249
250
251
252
253
# File 'lib/rwd/ruby.rb', line 242

def uncomment
  res	= []

  self.splitblocks(["'", "'"], ['"', '"'], ["#", "\n"]).each do |type, s|
    case type
    when 0, 1, 2	then	res << s
    when 3		then	res << "\n"
    end
  end

  res.join("")
end