Class: String
- 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
-
#_rfc2396_parser ⇒ Object
Cache an ‘URI::RFC2396_Parser` instance, because it’s slowwww to initialize.
-
#amount(n) ⇒ Object
Convert this string into a string describing this many of the string.
-
#any? ⇒ Boolean
Are there any non-whitespace characters in the string?.
-
#blank? ⇒ Boolean
‘true’ if the string’s length is 0 (after whitespace has been stripped from the ends).
-
#contains_color? ⇒ Boolean
(also: #contains_colors?, #contains_ansi?)
This string contains ANSI (VT100) control codes.
-
#deflate(level = nil) ⇒ Object
deflate the string.
-
#dewhitespace ⇒ Object
Remove redundant whitespace AND newlines.
-
#each_chomped ⇒ Object
(also: #chomped_lines, #chomp_lines)
Like #each_line, but removes trailing n.
-
#each_slice(slice_width, &block) ⇒ Object
Iterate over slices of the string of size ‘slice_width`.
-
#endswith?(substring) ⇒ Boolean
(also: #endswith)
‘true` if this string ends with the substring.
-
#float? ⇒ Boolean
Could this string be cast to an float?.
-
#from_base(base = 10) ⇒ Object
Convert a string encoded in some base <= 64 into an integer.
- #from_base62 ⇒ Object
-
#from_base64 ⇒ Object
(also: #decode64)
Decode a mime64/base64 encoded string.
-
#from_bencode ⇒ Object
Convert Python serialized bencoded (pickled) objects to Ruby Objects.
-
#from_hms ⇒ Object
Converts time duration strings (mm:ss, mm:ss.dd, hh:mm:ss, or dd:hh:mm:ss) to seconds.
-
#from_json ⇒ Object
Parse this string as JSON.
-
#from_yaml ⇒ Object
Parse this string as YAML.
-
#gunzip ⇒ Object
gunzip the string.
-
#gzip(level = nil) ⇒ Object
gzip the string.
-
#hexdump ⇒ Object
Print a hexdump of the string to STDOUT (coloured, if the terminal supports it).
-
#indent(prefix = " ") ⇒ Object
Indent all the lines, if “prefix” is a string, prepend that string to each lien.
-
#inflate ⇒ Object
inflate the string.
-
#integer? ⇒ Boolean
Could this string be cast to an integer?.
-
#md5 ⇒ Object
MD5 the string.
-
#nice_html(indent = 2) ⇒ Object
(also: #nicehtml, #indent_html)
Use Nokogiri to parse this string as HTML, and return an indented version.
-
#nice_lines ⇒ Object
(also: #nicelines, #clean_lines)
Like #each_line, but skips empty lines and removes n‘s.
-
#number? ⇒ Boolean
Could this string be cast to an number?.
-
#parse_units ⇒ Object
(also: #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.
-
#present? ⇒ Boolean
Is there anything in the string? (ignoring whitespace/newlines).
-
#rot13 ⇒ Object
The Infamous Caesar-Cipher.
- #sentences ⇒ Object
-
#sha1 ⇒ Object
SHA1 the string.
-
#sha256 ⇒ Object
SHA256 the string.
-
#shellescape ⇒ Object
Escape shell characters (globs, quotes, parens, etc.).
-
#smash ⇒ Object
Smash together all the characters in a string (removing whitespace).
- #split_after(boundary) ⇒ Object
- #split_at(boundary, **options) ⇒ Object
- #split_before(boundary) ⇒ Object
-
#startswith?(substring) ⇒ Boolean
(also: #startswith)
‘true` if this string starts with the substring.
-
#strip_color ⇒ Object
(also: #strip_ansi)
Remove ANSI color codes.
-
#tighten ⇒ Object
Remove redundant whitespaces (not including newlines).
-
#titlecase ⇒ Object
Return a new string converted to “Title Case” (first letter of each word capitalized).
-
#titlecase! ⇒ Object
Convert string to “Title Case” (first letter of each word capitalized).
-
#to_base62 ⇒ Object
Convert a string (encoded in base16 “hex” – for example, an MD5 or SHA1 hash) into “base62” format.
-
#to_base64 ⇒ Object
(also: #base64, #encode64)
Encode into a mime64/base64 string.
- #to_bigdecimal ⇒ Object (also: #to_d, #to_dec, #to_decimal)
-
#to_i_from_bytes(big_endian = false) ⇒ Object
Raw bytes to an integer (as big as necessary).
-
#to_params ⇒ Object
Convert a query string to a hash of params.
-
#to_Path ⇒ Object
(also: #to_P)
Convert the string to a Path object (for representing files/directories).
-
#to_proc(&block) ⇒ Object
String#to_proc.
-
#to_unix ⇒ Object
Convert rn to n.
-
#to_uri ⇒ Object
(also: #to_URI)
URI.parse the string and return an URI object.
-
#truthy? ⇒ Boolean
Does this string contain something that means roughly “true”?.
-
#unmarshal ⇒ Object
(also: #from_marshal)
Unmarshal the string (transform it into Ruby datatypes).
-
#urldecode ⇒ Object
Convert an URI’s %XXes into regular characters.
-
#urlencode ⇒ Object
Convert non-URI characters into %XXes.
-
#urlescape ⇒ Object
Do what a browser would do when you type something into the address bar.
- #words ⇒ Object
- #words_without_stopwords ⇒ Object (also: #without_stopwords)
-
#wrap(width = nil) ⇒ Object
(also: #word_wrap)
Word-wrap the string so each line is at most ‘width` wide.
-
#wrap_and_indent(prefix, width = nil) ⇒ Object
(also: #wrapdent)
Wrap all lines at window size, and indent.
Instance Method Details
#_rfc2396_parser ⇒ Object
Cache an ‘URI::RFC2396_Parser` instance, because it’s slowwww to initialize
290 291 292 |
# File 'lib/epitools/core_ext/string.rb', line 290 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"
507 508 509 510 511 512 513 514 515 516 |
# File 'lib/epitools/core_ext/string.rb', line 507 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?
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)
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
98 99 100 |
# File 'lib/epitools/core_ext/string.rb', line 98 def contains_color? self[COLOR_REGEXP] end |
#deflate(level = nil) ⇒ Object
deflate the string
449 450 451 |
# File 'lib/epitools/core_ext/string.rb', line 449 def deflate(level=nil) Zlib::Deflate.deflate(self, level) end |
#dewhitespace ⇒ Object
Remove redundant whitespace AND newlines.
63 64 65 |
# File 'lib/epitools/core_ext/string.rb', line 63 def dewhitespace gsub(/\s+/,' ').strip end |
#each_chomped ⇒ Object Also known as: chomped_lines, chomp_lines
Like #each_line, but removes trailing n
126 127 128 |
# File 'lib/epitools/core_ext/string.rb', line 126 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`.
270 271 272 273 274 275 276 277 |
# File 'lib/epitools/core_ext/string.rb', line 270 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
470 471 472 |
# File 'lib/epitools/core_ext/string.rb', line 470 def endswith?(substring) self[-substring.size..-1] == substring end |
#float? ⇒ Boolean
Could this string be cast to an float?
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.)
363 364 365 366 367 368 369 370 |
# File 'lib/epitools/core_ext/string.rb', line 363 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
372 373 374 |
# File 'lib/epitools/core_ext/string.rb', line 372 def from_base62 from_base(62) end |
#from_base64 ⇒ Object Also known as: decode64
Decode a mime64/base64 encoded string
387 388 389 |
# File 'lib/epitools/core_ext/string.rb', line 387 def from_base64 unpack("m").first end |
#from_bencode ⇒ Object
Convert Python serialized bencoded (pickled) objects to Ruby Objects
404 405 406 |
# File 'lib/epitools/core_ext/string.rb', line 404 def from_bencode BEncode.load(self) 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)
522 523 524 525 526 527 528 529 530 |
# File 'lib/epitools/core_ext/string.rb', line 522 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
478 479 480 |
# File 'lib/epitools/core_ext/string.rb', line 478 def from_json JSON.parse(self) end |
#from_yaml ⇒ Object
Parse this string as YAML
485 486 487 |
# File 'lib/epitools/core_ext/string.rb', line 485 def from_yaml YAML.load(self) end |
#gunzip ⇒ Object
gunzip the string
441 442 443 444 |
# File 'lib/epitools/core_ext/string.rb', line 441 def gunzip data = StringIO.new(self) Zlib::GzipReader.new(data).read end |
#gzip(level = nil) ⇒ Object
gzip the string
432 433 434 435 436 |
# File 'lib/epitools/core_ext/string.rb', line 432 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)
597 598 599 |
# File 'lib/epitools/core_ext/string.rb', line 597 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.
167 168 169 170 171 172 173 174 175 |
# File 'lib/epitools/core_ext/string.rb', line 167 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
456 457 458 |
# File 'lib/epitools/core_ext/string.rb', line 456 def inflate Zlib::Inflate.inflate(self) end |
#integer? ⇒ Boolean
Could this string be cast to an integer?
105 106 107 |
# File 'lib/epitools/core_ext/truthiness.rb', line 105 def integer? !!strip.match(/^-?\d+$/) end |
#md5 ⇒ Object
MD5 the string
411 412 413 |
# File 'lib/epitools/core_ext/string.rb', line 411 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
180 181 182 |
# File 'lib/epitools/core_ext/string.rb', line 180 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.
115 116 117 118 |
# File 'lib/epitools/core_ext/string.rb', line 115 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?
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
536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 |
# File 'lib/epitools/core_ext/string.rb', line 536 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)
133 134 135 |
# File 'lib/epitools/core_ext/truthiness.rb', line 133 def present? not blank? end |
#rot13 ⇒ Object
The Infamous Caesar-Cipher. Unbreakable to this day.
283 284 285 |
# File 'lib/epitools/core_ext/string.rb', line 283 def rot13 tr('n-za-mN-ZA-M', 'a-zA-Z') end |
#sentences ⇒ Object
254 255 256 |
# File 'lib/epitools/core_ext/string.rb', line 254 def sentences split_after(/[\.\!\?]+/).lazy.map {|s| s.strip.gsub(/\s+/, " ") } end |
#sha1 ⇒ Object
SHA1 the string
418 419 420 |
# File 'lib/epitools/core_ext/string.rb', line 418 def sha1 Digest::SHA1.hexdigest self end |
#sha256 ⇒ Object
SHA256 the string
425 426 427 |
# File 'lib/epitools/core_ext/string.rb', line 425 def sha256 Digest::SHA256.hexdigest self end |
#shellescape ⇒ Object
Escape shell characters (globs, quotes, parens, etc.)
34 35 36 |
# File 'lib/epitools/core_ext/string.rb', line 34 def shellescape Shellwords.escape(self) end |
#smash ⇒ Object
Smash together all the characters in a string (removing whitespace)
56 57 58 |
# File 'lib/epitools/core_ext/string.rb', line 56 def smash downcase.scan(/\w+/).join end |
#split_after(boundary) ⇒ Object
155 156 157 |
# File 'lib/epitools/core_ext/string.rb', line 155 def split_after(boundary) split_at(boundary, include_boundary: true) end |
#split_at(boundary, **options) ⇒ Object
133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 |
# File 'lib/epitools/core_ext/string.rb', line 133 def split_at(boundary, **) include_boundary = [: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
159 160 161 |
# File 'lib/epitools/core_ext/string.rb', line 159 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
462 463 464 |
# File 'lib/epitools/core_ext/string.rb', line 462 def startswith?(substring) self[0...substring.size] == substring end |
#strip_color ⇒ Object Also known as: strip_ansi
Remove ANSI color codes.
107 108 109 |
# File 'lib/epitools/core_ext/string.rb', line 107 def strip_color gsub(COLOR_REGEXP, '') end |
#tighten ⇒ Object
Remove redundant whitespaces (not including newlines).
49 50 51 |
# File 'lib/epitools/core_ext/string.rb', line 49 def tighten gsub(/[\t ]+/,' ').strip end |
#titlecase ⇒ Object
Return a new string converted to “Title Case” (first letter of each word capitalized)
70 71 72 73 74 75 76 77 78 79 80 81 |
# File 'lib/epitools/core_ext/string.rb', line 70 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)
86 87 88 |
# File 'lib/epitools/core_ext/string.rb', line 86 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.)
380 381 382 |
# File 'lib/epitools/core_ext/string.rb', line 380 def to_base62 to_i(16).to_base62 end |
#to_base64 ⇒ Object Also known as: base64, encode64
Encode into a mime64/base64 string
395 396 397 |
# File 'lib/epitools/core_ext/string.rb', line 395 def to_base64 [self].pack("m") end |
#to_bigdecimal ⇒ Object Also known as: to_d, to_dec, to_decimal
312 313 314 315 |
# File 'lib/epitools/core_ext/string.rb', line 312 def to_bigdecimal BigDecimal BigDecimal(self) end |
#to_i_from_bytes(big_endian = false) ⇒ Object
Raw bytes to an integer (as big as necessary)
349 350 351 352 |
# File 'lib/epitools/core_ext/string.rb', line 349 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
331 332 333 334 335 336 337 338 339 340 341 342 343 344 |
# File 'lib/epitools/core_ext/string.rb', line 331 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: weblog.raganwald.com/2007/10/stringtoproc.html
Ported from the String Lambdas in Oliver Steele’s Functional Javascript osteele.com/sources/javascript/functional/
This work is licensed under the MIT License:
© 2007 Reginald Braithwaite Portions Copyright © 2006 Oliver Steele
## Basic Usage
→ 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+2*y’.to_proc[2, 3];
→ 8
‘y x -> x+2*y’.to_proc[2, 3];
→ 7
Otherwise, if the string contains a _, it’s a unary function and _ is name of the parameter:
→ 3
→ 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
→ 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.
→ 3
→ 9
‘x + 2*y’.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.
734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 |
# File 'lib/epitools/core_ext/string.rb', line 734 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 rn 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
323 324 325 |
# File 'lib/epitools/core_ext/string.rb', line 323 def to_uri URI.parse self end |
#truthy? ⇒ Boolean
Does this string contain something that means roughly “true”?
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).
492 493 494 |
# File 'lib/epitools/core_ext/string.rb', line 492 def unmarshal Marshal.restore self end |
#urldecode ⇒ Object
Convert an URI’s %XXes into regular characters.
305 306 307 |
# File 'lib/epitools/core_ext/string.rb', line 305 def urldecode _rfc2396_parser.unescape(self) end |
#urlencode ⇒ Object
Convert non-URI characters into %XXes.
297 298 299 300 |
# File 'lib/epitools/core_ext/string.rb', line 297 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
41 42 43 44 |
# File 'lib/epitools/core_ext/string.rb', line 41 def urlescape @@uri_parser ||= URI::RFC2396_Parser.new @@uri_parser.escape(self) end |
#words ⇒ Object
258 259 260 |
# File 'lib/epitools/core_ext/string.rb', line 258 def words scan /[[:alnum:]]+/ end |
#words_without_stopwords ⇒ Object Also known as: without_stopwords
262 263 264 |
# File 'lib/epitools/core_ext/string.rb', line 262 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.
194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 |
# File 'lib/epitools/core_ext/string.rb', line 194 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
239 240 241 242 243 244 245 246 247 248 249 250 251 |
# File 'lib/epitools/core_ext/string.rb', line 239 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 |