Class: String

Inherits:
Object show all
Includes:
ValueType
Defined in:
dragon/string.rb

Overview

Copyright 2019 DragonRuby LLC MIT License string.rb has been released under MIT (*only this file*).

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.wrapped_lines(string, length) ⇒ Object


41
42
43
44
45
46
47
48
49
50
51
# File 'dragon/string.rb', line 41

def self.wrapped_lines string, length
  string.each_line.map do |l|
    l = l.rstrip
    if l.length < length
      l + "\n"
    else
      words = l.split ' '
      wrapped_lines_recur(words[0], words[1..-1], length, []).flatten
    end
  end.flatten
end

.wrapped_lines_recur(word, rest, length, aggregate) ⇒ Object


9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'dragon/string.rb', line 9

def self.wrapped_lines_recur word, rest, length, aggregate
  if word.nil?
    return aggregate
  elsif rest[0].nil?
    aggregate << word + "\n"
    return aggregate
  elsif (word + " " + rest[0]).length > length
    aggregate << word + "\n"
    return wrapped_lines_recur rest[0], rest[1..-1], length, aggregate
  elsif (word + " " + rest[0]).length <= length
    next_word = (word + " " + rest[0])
    return wrapped_lines_recur next_word, rest[1..-1], length, aggregate
  else
    log <<-S
WARNING:
#{word} is too long to fit in length of #{length}.

S
    next_word = (word + " " + rest[0])
    return wrapped_lines_recur next_word, rest[1..-1], length, aggregate
  end
end

Instance Method Details

#end_with_bang?Boolean

Returns:

  • (Boolean)

32
33
34
# File 'dragon/string.rb', line 32

def end_with_bang?
  self[-1] == "!"
end

#indent_lines(amount, char = " ") ⇒ Object


67
68
69
70
71
72
73
74
75
# File 'dragon/string.rb', line 67

def indent_lines amount, char = " "
  self.each_line.each_with_index.map do |l, i|
    if i == 0
      l
    else
      char * amount + l
    end
  end.join
end

#ltrimObject


89
90
91
# File 'dragon/string.rb', line 89

def ltrim
  lstrip
end

#ltrim!Object


93
94
95
# File 'dragon/string.rb', line 93

def ltrim!
  lstrip!
end

#multiline?Boolean

Returns:

  • (Boolean)

63
64
65
# File 'dragon/string.rb', line 63

def multiline?
  include? "\n"
end

#quoteObject


77
78
79
# File 'dragon/string.rb', line 77

def quote
  "\"#{self}\""
end

#rtrimObject


97
98
99
# File 'dragon/string.rb', line 97

def rtrim
  rstrip
end

#rtrim!Object


101
102
103
# File 'dragon/string.rb', line 101

def rtrim!
  rstrip!
end

#serializeObject


105
106
107
# File 'dragon/string.rb', line 105

def serialize
  self
end

#trimObject


81
82
83
# File 'dragon/string.rb', line 81

def trim
  strip
end

#trim!Object


85
86
87
# File 'dragon/string.rb', line 85

def trim!
  strip!
end

#without_ending_bangObject


36
37
38
39
# File 'dragon/string.rb', line 36

def without_ending_bang
  return self unless end_with_bang?
  self[0..-2]
end

#wrap(length) ⇒ Object


58
59
60
# File 'dragon/string.rb', line 58

def wrap length
  wrapped_lines(length).join.rstrip
end

#wrapped_lines(length) ⇒ Object


53
54
55
# File 'dragon/string.rb', line 53

def wrapped_lines length
  String.wrapped_lines self, length
end