Class: String

Inherits:
Object show all
Defined in:
lib/epitools/minimal.rb,
lib/epitools/core_ext/string.rb,
lib/epitools/core_ext/truthiness.rb

Constant Summary collapse

LOWERCASE_WORDS =

For titlecase

Set.new %w[of to or the and an a at is for from in]
STOP_WORDS =

For words_without_stopwords

%w[a cannot into our thus about co is ours to above could it ourselves together across down its out too after during itself over toward afterwards each last own towards again eg latter per under against either latterly perhaps until all else least rather up almost elsewhere less same upon alone enough ltd seem us along etc many seemed very already even may seeming via also ever me seems was although every meanwhile several we always everyone might she well among everything more should were amongst everywhere moreover since what an except most so whatever and few mostly some when another first much somehow whence any for must someone whenever anyhow former my something where anyone formerly myself sometime whereafter anything from namely sometimes whereas anywhere further neither somewhere whereby are had never still wherein around has nevertheless such whereupon as have next than wherever at he no that whether be hence nobody the whither became her none their which because here noone them while become hereafter nor themselves who becomes hereby not then whoever becoming herein nothing thence whole been hereupon now there whom before hers nowhere thereafter whose beforehand herself of thereby why behind him off therefore will being himself often therein with below his on thereupon within beside how once these without besides however one they would between i only this yet beyond ie onto those you both if or though your but in other through yours by inc others throughout yourself can indeed otherwise thru yourselves]
COLOR_REGEXP =

A Regexp to recognize ANSI escape sequences

/\e\[.*?(\d)*[mA-Z]/
BASE_DIGITS =

Cached constants for base conversion.

Integer::BASE_DIGITS.map.with_index.to_h

Instance Method Summary collapse

Instance Method Details

#_rfc2396_parser ⇒ Object

Cache an URI::RFC2396_Parser instance, because it's slowwww to initialize



328
329
330
# File 'lib/epitools/core_ext/string.rb', line 328

def _rfc2396_parser
  @@rfc2396_parser ||= URI::RFC2396_Parser.new
end

#amount(n) ⇒ Object

Convert this string into a string describing this many of the string. (Note: Doesn't know anything about proper grammar.)

Example:

"cookie".amount(0)    #=> "0 cookies"
"shirt".amount(17)    #=> "17 shirts"
"dollar".amount(-10)  #=> "-10 dollars"
"love".amount(1)      #=> "1 love"


562
563
564
565
566
567
568
569
570
571
# File 'lib/epitools/core_ext/string.rb', line 562

def amount(n)
  case n
  when 0
    "0 #{self}s"
  when 1, -1
    "#{n} #{self}"
  else
    "#{n} #{self}s"
  end
end

#any? ⇒ Boolean

Are there any non-whitespace characters in the string?

Returns:

  • (Boolean)


20
21
22
# File 'lib/epitools/core_ext/string.rb', line 20

def any?
  not blank?
end

#blank? ⇒ Boolean

'true' if the string's length is 0 (after whitespace has been stripped from the ends)

Returns:

  • (Boolean)


126
127
128
# File 'lib/epitools/core_ext/truthiness.rb', line 126

def blank?
  strip.size == 0
end

#contains_color? ⇒ Boolean Also known as: contains_colors?, contains_ansi?

This string contains ANSI (VT100) control codes

Returns:

  • (Boolean)


119
120
121
# File 'lib/epitools/core_ext/string.rb', line 119

def contains_color?
  self[COLOR_REGEXP]
end

#convert_encoding(from, to) ⇒ Object

Convert the string from one encoding into another (eg: convert_encoding('cp437', 'utf-8'))



34
35
36
# File 'lib/epitools/core_ext/string.rb', line 34

def convert_encoding(from, to)
  force_encoding(from).encode(to)
end

#crc32 ⇒ Object Also known as: crc

CRC32 the string



456
457
458
# File 'lib/epitools/core_ext/string.rb', line 456

def crc32
  Zlib.crc32(self).to_hex
end

#deflate(level = nil) ⇒ Object

deflate the string



504
505
506
# File 'lib/epitools/core_ext/string.rb', line 504

def deflate(level=nil)
  Zlib::Deflate.deflate(self, level)
end

#dewhitespace ⇒ Object

Remove redundant whitespace AND newlines.



84
85
86
# File 'lib/epitools/core_ext/string.rb', line 84

def dewhitespace
  gsub(/\s+/,' ').strip
end

#each_chomped ⇒ Object Also known as: chomped_lines, chomp_lines

Like #each_line, but removes trailing \n



147
148
149
# File 'lib/epitools/core_ext/string.rb', line 147

def each_chomped
  each_line { |line| yield line.chomp }
end

#each_slice(slice_width, &block) ⇒ Object

Iterate over slices of the string of size slice_width.



291
292
293
294
295
296
297
298
# File 'lib/epitools/core_ext/string.rb', line 291

def each_slice(slice_width, &block)
  max = size
  p = 0
  while p < max
    yield self[p...p+slice_width]
    p += slice_width
  end
end

#endswith?(substring) ⇒ Boolean Also known as: endswith

true if this string ends with the substring

Returns:

  • (Boolean)


525
526
527
# File 'lib/epitools/core_ext/string.rb', line 525

def endswith?(substring)
  self[-substring.size..-1] == substring
end

#float? ⇒ Boolean

Could this string be cast to an float?

Returns:

  • (Boolean)


112
113
114
# File 'lib/epitools/core_ext/truthiness.rb', line 112

def float?
  !!strip.match(/^-?\d+\.\d+$/)
end

#from_base(base = 10) ⇒ Object

Convert a string encoded in some base <= 64 into an integer. (See Integer#to_base for more info.)



408
409
410
411
412
413
414
415
# File 'lib/epitools/core_ext/string.rb', line 408

def from_base(base=10)
  n = 0
  chars.reverse_each.with_index do |c, power|
    value = BASE_DIGITS[c]
    n += (base**power) * value
  end
  n
end

#from_base62 ⇒ Object



417
418
419
# File 'lib/epitools/core_ext/string.rb', line 417

def from_base62
  from_base(62)
end

#from_base64 ⇒ Object Also known as: decode64

Decode a mime64/base64 encoded string



432
433
434
# File 'lib/epitools/core_ext/string.rb', line 432

def from_base64
  unpack("m").first
end

#from_bencode ⇒ Object

Convert Python serialized bencoded (pickled) objects to Ruby Objects



449
450
451
# File 'lib/epitools/core_ext/string.rb', line 449

def from_bencode
  BEncode.load(self)
end

#from_cp437 ⇒ Object

Convert a string from cp437 into utf-8



48
49
50
# File 'lib/epitools/core_ext/string.rb', line 48

def from_cp437
  to_utf8
end

#from_hms ⇒ Object

Converts time duration strings (mm:ss, mm:ss.dd, hh:mm:ss, or dd:hh:mm:ss) to seconds. (The reverse of Integer#to_hms)



577
578
579
580
581
582
583
584
585
# File 'lib/epitools/core_ext/string.rb', line 577

def from_hms
  nums = split(':')

  nums[-1] = nums[-1].to_f if nums[-1] =~ /\d+\.\d+/ # convert fractional seconds to a float
  nums.map! { |n| n.is_a?(String) ? n.to_i : n } # convert the rest to integers

  nums_and_units = nums.reverse.zip %w[seconds minutes hours days]
  nums_and_units.map { |num, units| num.send(units) }.sum
end

#from_json ⇒ Object

Parse this string as JSON



533
534
535
# File 'lib/epitools/core_ext/string.rb', line 533

def from_json
  JSON.parse(self)
end

#from_yaml ⇒ Object

Parse this string as YAML



540
541
542
# File 'lib/epitools/core_ext/string.rb', line 540

def from_yaml
  YAML.load(self)
end

#gunzip ⇒ Object

gunzip the string



496
497
498
499
# File 'lib/epitools/core_ext/string.rb', line 496

def gunzip
  data = StringIO.new(self)
  Zlib::GzipReader.new(data).read
end

#gzip(level = nil) ⇒ Object

gzip the string



487
488
489
490
491
# File 'lib/epitools/core_ext/string.rb', line 487

def gzip(level=nil)
  zipped = StringIO.new
  Zlib::GzipWriter.wrap(zipped, level) { |io| io.write(self) }
  zipped.string
end

#hexdump ⇒ Object

Print a hexdump of the string to STDOUT (coloured, if the terminal supports it)



652
653
654
# File 'lib/epitools/core_ext/string.rb', line 652

def hexdump
  Hex.dump(self)
end

#indent(prefix = " ") ⇒ Object

Indent all the lines, if "prefix" is a string, prepend that string to each lien. If it's an integer, prepend that many spaces.



188
189
190
191
192
193
194
195
196
# File 'lib/epitools/core_ext/string.rb', line 188

def indent(prefix="  ")
  prefix = (" " * prefix) if prefix.is_an? Integer

  if block_given?
    lines.each { |line| yield prefix + line }
  else
    lines.map { |line| prefix + line }.join('')
  end
end

#inflate ⇒ Object

inflate the string



511
512
513
# File 'lib/epitools/core_ext/string.rb', line 511

def inflate
  Zlib::Inflate.inflate(self)
end

#integer? ⇒ Boolean

Could this string be cast to an integer?

Returns:

  • (Boolean)


105
106
107
# File 'lib/epitools/core_ext/truthiness.rb', line 105

def integer?
  !!strip.match(/^-?\d+$/)
end

#md5 ⇒ Object

MD5 the string



464
465
466
# File 'lib/epitools/core_ext/string.rb', line 464

def md5
  Digest::MD5.hexdigest self
end

#nice_html(indent = 2) ⇒ Object Also known as: nicehtml, indent_html

Use Nokogiri to parse this string as HTML, and return an indented version



201
202
203
# File 'lib/epitools/core_ext/string.rb', line 201

def nice_html(indent=2)
  Nokogiri::HTML.fragment(self).to_xhtml(indent: indent)
end

#nice_lines ⇒ Object Also known as: nicelines, clean_lines

Like #each_line, but skips empty lines and removes \n's.



136
137
138
139
# File 'lib/epitools/core_ext/string.rb', line 136

def nice_lines
  # note: $/ is the platform's newline separator
  split($/).select{|l| not l.blank? }
end

#number? ⇒ Boolean

Could this string be cast to an number?

Returns:

  • (Boolean)


119
120
121
# File 'lib/epitools/core_ext/truthiness.rb', line 119

def number?
  !!strip.match(/^-?\d\.?\d*$/)
end

#parse_units ⇒ Object Also known as: from_units, from_human, from_size, from_percent, from_time

Translate numbers with units (like 25k, 150GB, 15%, 5 hours) into their expanded numeric value



591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
# File 'lib/epitools/core_ext/string.rb', line 591

def parse_units
  # extract the unit suffix
  if self =~ /(\d[\d_]*(?:\.\d+)?)\s*([a-zA-Z]+\b|%(?= \s|$))/
    units = $2.downcase
    num   = $1 #.to_f
    num   = num["."] ? num.to_f : num.to_i

    case units
    when "%"
      # 0.01
      num / 100.0
    when "k"
      # 10**3
      num.thousand
    when "m", "mm"
      # 10**6
      num.million
    when "b", "bn"
      # 10**9
      num.billion
    when "gib", "gb", "g"
      num * 2**30
    when "mib", "mb"
      num * 2**20
    when "kib", "kb"
      num * 2**10
    when "t", "tb"
      # 10**12
      num.trillion
    when "q"
      # 10**15
      num.quadrillion
    when "Q"
      # 10**18
      num.quintillion
    when "min"
      # 1.minute
      num.minutes
    when "hours", "h", "hr", "hrs"
      # 1.hour
      num.hours
    when "d", "days", "dy"
      num.days
    else
      raise "Invalid units: #{units.inspect}, in: #{self.inspect}"
    end
  else
    raise "Couldn't find any units to parse! (expecting: '<a number><some letters>')"
  end
end

#present? ⇒ Boolean

Is there anything in the string? (ignoring whitespace/newlines)

Returns:

  • (Boolean)


133
134
135
# File 'lib/epitools/core_ext/truthiness.rb', line 133

def present?
  not blank?
end

#rot(amount = 1) ⇒ Object

Implements the unfamous non-Caesar ciphers. Want to rot your string more or less than 13? You've come to the right place! (NOTE: Currently only works on American letters. No fancy accents or cyrillic letters please!)



312
313
314
315
316
317
318
319
320
321
322
323
# File 'lib/epitools/core_ext/string.rb', line 312

def rot(amount=1)
  chars.map do |c|
    case c
    when ?a..?z
      (((c.ord - ?a.ord + amount) % 26) + ?a.ord).chr
    when ?A..?Z
      (((c.ord - ?A.ord + amount) % 26) + ?A.ord).chr
    else
      c
    end
  end.join
end

#rot13 ⇒ Object

Implements the Infamous Caesar-Cipher. Unbreakable to this day.



304
305
306
# File 'lib/epitools/core_ext/string.rb', line 304

def rot13
  tr('n-za-mN-ZA-M', 'a-zA-Z')
end

#sentences ⇒ Object



275
276
277
# File 'lib/epitools/core_ext/string.rb', line 275

def sentences
  split_after(/[\.\!\?]+/).lazy.map {|s| s.strip.gsub(/\s+/, " ") }
end

#sha1 ⇒ Object

SHA1 the string



471
472
473
# File 'lib/epitools/core_ext/string.rb', line 471

def sha1
  Digest::SHA1.hexdigest self
end

#sha256 ⇒ Object

SHA256 the string



478
479
480
# File 'lib/epitools/core_ext/string.rb', line 478

def sha256
  Digest::SHA256.hexdigest self
end

#shellescape ⇒ Object

Escape shell characters (globs, quotes, parens, etc.)



55
56
57
# File 'lib/epitools/core_ext/string.rb', line 55

def shellescape
  Shellwords.escape(self)
end

#smash ⇒ Object

Smash together all the characters in a string (removing whitespace)



77
78
79
# File 'lib/epitools/core_ext/string.rb', line 77

def smash
  downcase.scan(/\w+/).join
end

#split_after(boundary) ⇒ Object



176
177
178
# File 'lib/epitools/core_ext/string.rb', line 176

def split_after(boundary)
  split_at(boundary, include_boundary: true)
end

#split_at(boundary, **options) ⇒ Object



154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
# File 'lib/epitools/core_ext/string.rb', line 154

def split_at(boundary, **options)
  include_boundary = options[:include_boundary] || false

  boundary = Regexp.new(Regexp.escape(boundary)) if boundary.is_a?(String)
  s        = StringScanner.new(self)

  Enumerator.new do |yielder|
    loop do
      if match = s.scan_until(boundary)
        if include_boundary
          yielder << match
        else
          yielder << match[0..-(s.matched_size+1)]
        end
      else
        yielder << s.rest if s.rest?
        break
      end
    end
  end
end

#split_before(boundary) ⇒ Object



180
181
182
# File 'lib/epitools/core_ext/string.rb', line 180

def split_before(boundary)
  raise "Why would you want this? Sorry, unimplemented. Send patches."
end

#startswith?(substring) ⇒ Boolean Also known as: startswith

true if this string starts with the substring

Returns:

  • (Boolean)


517
518
519
# File 'lib/epitools/core_ext/string.rb', line 517

def startswith?(substring)
  self[0...substring.size] == substring
end

#strip_color ⇒ Object Also known as: strip_ansi

Remove ANSI color codes.



128
129
130
# File 'lib/epitools/core_ext/string.rb', line 128

def strip_color
  gsub(COLOR_REGEXP, '')
end

#tighten ⇒ Object

Remove redundant whitespaces (not including newlines).



70
71
72
# File 'lib/epitools/core_ext/string.rb', line 70

def tighten
  gsub(/[\t ]+/,' ').strip
end

#titlecase ⇒ Object

Return a new string converted to "Title Case" (first letter of each word capitalized)



91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/epitools/core_ext/string.rb', line 91

def titlecase
  first = true
  words = downcase.split(/(?<!\w')\b/)

  words.map.with_index do |word,i|
    if LOWERCASE_WORDS.include?(word) and i > 0 # leave LOWERCASE_WORDS lowercase, unless it's the first word.
      word
    else
      word.gsub(/^\w/) { |c| c.upcase } # capitalize first letter
    end
  end.join('')
end

#titlecase! ⇒ Object

Convert string to "Title Case" (first letter of each word capitalized)



107
108
109
# File 'lib/epitools/core_ext/string.rb', line 107

def titlecase!
  replace(titlecase)
end

#to_base62 ⇒ Object

Convert a string (encoded in base16 "hex" -- for example, an MD5 or SHA1 hash) into "base62" format. (See Integer#to_base62 for more info.)



425
426
427
# File 'lib/epitools/core_ext/string.rb', line 425

def to_base62
  to_i(16).to_base62
end

#to_base64 ⇒ Object Also known as: base64, encode64

Encode into a mime64/base64 string



440
441
442
# File 'lib/epitools/core_ext/string.rb', line 440

def to_base64
  [self].pack("m")
end

#to_bigdecimal ⇒ Object Also known as: to_d, to_dec, to_decimal



350
351
352
353
# File 'lib/epitools/core_ext/string.rb', line 350

def to_bigdecimal
  BigDecimal
  BigDecimal(self)
end

#to_hex ⇒ Object

Convert the bytes of this string to hexadecimcal digits



395
396
397
# File 'lib/epitools/core_ext/string.rb', line 395

def to_hex
  unpack("H*").first
end

#to_i_from_bytes(big_endian = false) ⇒ Object

Raw bytes to an integer (as big as necessary)



387
388
389
390
# File 'lib/epitools/core_ext/string.rb', line 387

def to_i_from_bytes(big_endian=false)
  bs = big_endian ? bytes.reverse_each : bytes.each
  bs.with_index.inject(0) { |sum,(b,i)| (b << (8*i)) + sum }
end

#to_params ⇒ Object

Convert a query string to a hash of params



369
370
371
372
373
374
375
376
377
378
379
380
381
382
# File 'lib/epitools/core_ext/string.rb', line 369

def to_params
  params = {}

  split(/[&;]/).each do |pairs|
    key, value = pairs.split('=',2).collect { |v| CGI.unescape(v) }

    if key and value
      params[key] ||= []
      params[key] << value
    end
  end

  params.map_values { |v| v.size > 1 ? v : v.first }
end

#to_Path ⇒ Object Also known as: to_P

Convert the string to a Path object (for representing files/directories).



269
270
271
# File 'lib/epitools/minimal.rb', line 269

def to_Path
  Path[self]
end

#to_proc(&block) ⇒ Object

String#to_proc

See: http://weblog.raganwald.com/2007/10/stringtoproc.html

Ported from the String Lambdas in Oliver Steele's Functional Javascript http://osteele.com/sources/javascript/functional/

This work is licensed under the MIT License:

(c) 2007 Reginald Braithwaite Portions Copyright (c) 2006 Oliver Steele

Basic Usage

'x+1'.to_proc; → 3 'x+2*y'.to_proc[2, 3]; → 8 or (more usefully) later:

square = 'x*x'.to_proc; square(3); → 9 square(4); → 16

Explicit parameters

If the string contains a ->, this separates the parameters from the body.

'x y -> x+2y'.to_proc[2, 3]; → 8 'y x -> x+2y'.to_proc[2, 3]; → 7 Otherwise, if the string contains a _, it’s a unary function and _ is name of the parameter:

'+1'.to_proc; → 3 '*_'.to_proc; → 9

Implicit parameters

If the string doesn’t specify explicit parameters, they are implicit.

If the string starts with an operator or relation besides -, or ends with an operator or relation, then its implicit arguments are placed at the beginning and/or end:

'*2'.to_proc; → 4 '/2'.to_proc; → 2 '2/'.to_proc; → 0.5 '/'.to_proc[2, 4]; → 0.5 ’.’ counts as a right operator:

'.abs'.to_proc; → 1

Otherwise, the variables in the string, in order of occurrence, are its parameters.

'x+1'.to_proc; → 3 'xx'.to_proc; → 9 'x + 2y'.to_proc[1, 2]; → 5 'y + 2*x'.to_proc[1, 2]; → 5

Chaining

Chain -> to create curried functions.

'x y -> x+y'.to_proc[2, 3]; → 5 'x -> y -> x+y'.to_proc[3]; → 5 plus_two = 'x -> y -> x+y'.to_proc; plus_two → 5

Using String#to_proc in Idiomatic Ruby

Ruby on Rails popularized Symbol#to_proc, so much so that it will be part of Ruby 1.9.

If you like:

%w[dsf fgdg fg].map(&:capitalize) → ["Dsf", "Fgdg", "Fg"] then %w[dsf fgdg fg].map(&'.capitalize') isn’t much of an improvement.

But what about doubling every value in a list:

(1..5).map &'*2' → [2, 4, 6, 8, 10]

Or folding a list:

(1..5).inject &'+' → 15

Or having fun with factorial:

factorial = "(1.._).inject &'*'".to_proc factorial → 120

LICENSE: Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.



789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
# File 'lib/epitools/core_ext/string.rb', line 789

def to_proc &block
  params = []
  expr = self
  sections = expr.split(/\s*->\s*/m)
  if sections.length > 1 then
      eval_block(sections.reverse!.inject { |e, p| "(Proc.new { |#{p.split(/\s/).join(', ')}| #{e} })" }, block)
  elsif expr.match(/\b_\b/)
      eval_block("Proc.new { |_| #{expr} }", block)
  else
      leftSection = expr.match(/^\s*(?:[+*\/%&|\^\.=<>\[]|!=)/m)
      rightSection = expr.match(/[+\-*\/%&|\^\.=<>!]\s*$/m)
      if leftSection || rightSection then
          if (leftSection) then
              params.push('$left')
              expr = '$left' + expr
          end
          if (rightSection) then
              params.push('$right')
              expr = expr + '$right'
          end
      else
          self.gsub(
              /(?:\b[A-Z]|\.[a-zA-Z_$])[a-zA-Z_$\d]*|[a-zA-Z_$][a-zA-Z_$\d]*:|self|arguments|'(?:[^'\\]|\\.)*'|"(?:[^"\\]|\\.)*"/, ''
          ).scan(
            /([a-z_$][a-z_$\d]*)/i
          ) do |v|
            params.push(v) unless params.include?(v)
          end
      end
      eval_block("Proc.new { |#{params.join(', ')}| #{expr} }", block)
  end
end

#to_unix ⇒ Object

Convert \r\n to \n



27
28
29
# File 'lib/epitools/core_ext/string.rb', line 27

def to_unix
  gsub("\r\n", "\n")
end

#to_uri ⇒ Object Also known as: to_URI

URI.parse the string and return an URI object



361
362
363
# File 'lib/epitools/core_ext/string.rb', line 361

def to_uri
  URI.parse self
end

#to_utf8(from = "cp437") ⇒ Object

Convert the string to utf-8, forcing another encoding



41
42
43
# File 'lib/epitools/core_ext/string.rb', line 41

def to_utf8(from="cp437")
  convert_encoding(from, "utf-8")
end

#truthy? ⇒ Boolean

Does this string contain something that means roughly "true"?

Returns:

  • (Boolean)


140
141
142
143
144
145
146
147
# File 'lib/epitools/core_ext/truthiness.rb', line 140

def truthy?
  case strip.downcase
  when "1", "true", "yes", "on", "enabled", "affirmative"
    true
  else
    false
  end
end

#unmarshal ⇒ Object Also known as: from_marshal

Unmarshal the string (transform it into Ruby datatypes).



547
548
549
# File 'lib/epitools/core_ext/string.rb', line 547

def unmarshal
  Marshal.load self
end

#urldecode ⇒ Object

Convert an URI's %XXes into regular characters.



343
344
345
# File 'lib/epitools/core_ext/string.rb', line 343

def urldecode
  _rfc2396_parser.unescape(self)
end

#urlencode ⇒ Object

Convert non-URI characters into %XXes.



335
336
337
338
# File 'lib/epitools/core_ext/string.rb', line 335

def urlencode
  #URI.escape(self)
  _rfc2396_parser.escape(self)
end

#urlescape ⇒ Object

Do what a browser would do when you type something into the address bar



62
63
64
65
# File 'lib/epitools/core_ext/string.rb', line 62

def urlescape
  @@uri_parser ||= URI::RFC2396_Parser.new
  @@uri_parser.escape(self)
end

#words ⇒ Object



279
280
281
# File 'lib/epitools/core_ext/string.rb', line 279

def words
  scan /[[:alnum:]]+/
end

#words_without_stopwords ⇒ Object Also known as: without_stopwords



283
284
285
# File 'lib/epitools/core_ext/string.rb', line 283

def words_without_stopwords
  downcase.words - STOP_WORDS
end

#wrap(width = nil) ⇒ Object Also known as: word_wrap

Word-wrap the string so each line is at most width wide. Returns a string, or, if a block is given, yields each word-wrapped line to the block.

If width is nil, find the current width of the terminal and use that. If width is negative, subtract width from the terminal's current width.



215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
# File 'lib/epitools/core_ext/string.rb', line 215

def wrap(width=nil)
  if width.nil? or width < 0
    term_width, _ = Term.size

    if width and width < 0
      width = (term_width - 1) + width
    else
      width = term_width - 1
    end
  end

  return self if size <= width

  strings   = []
  start_pos = 0
  end_pos   = width

  loop do
    split_pos = rindex(/\s/, end_pos) || end_pos

    strings << self[start_pos...split_pos]

    start_pos = index(/\S/, split_pos)
    break if start_pos == nil
    end_pos   = start_pos + width

    if end_pos > size
      strings << self[start_pos..-1]
      break
    end
  end

  if block_given?
    strings.each { |s| yield s }
  else
    strings.join("\n")
  end
end

#wrap_and_indent(prefix, width = nil) ⇒ Object Also known as: wrapdent

Wrap all lines at window size, and indent



260
261
262
263
264
265
266
267
268
269
270
271
272
# File 'lib/epitools/core_ext/string.rb', line 260

def wrap_and_indent(prefix, width=nil)
  prefix = " "*prefix if prefix.is_a? Numeric

  prefix_size = prefix.strip_color.size

  if width
    width = width - prefix_size
  else
    width = -prefix_size
  end

  wrap(width).each_line.map { |line| prefix + line }.join
end