Class: Toys::WrappableString
- Inherits:
-
Object
- Object
- Toys::WrappableString
- Defined in:
- core-docs/toys/wrappable_string.rb
Overview
Defined in the toys-core gem
A string intended for word-wrapped display.
A WrappableString is an array of string "fragments" representing the atomic units that should not be split when word-wrapping. It should be possible to reconstruct the original string by joining these fragments with whitespace.
Instance Attribute Summary collapse
-
#fragments ⇒ Array<String>
readonly
Returns the string fragments, i.e.
Class Method Summary collapse
-
.make(obj) ⇒ Toys::WrappableString
Make the given object a WrappableString.
-
.make_array(objs) ⇒ Array<Toys::WrappableString>
Make the given object an array of WrappableString.
-
.wrap_lines(strs, width, width2 = nil) ⇒ Array<String>
Wraps an array of lines to the given width.
Instance Method Summary collapse
-
#+(other) ⇒ WrappableString
Returns a new WrappaableString whose content is the concatenation of this WrappableString with another WrappableString.
-
#==(other) ⇒ Boolean
(also: #eql?)
Tests two wrappable strings for equality.
-
#empty? ⇒ Boolean
Returns true if the string is empty (i.e. has no fragments).
-
#hash ⇒ Integer
Returns a hash code for this object.
-
#initialize(string = "") ⇒ WrappableString
constructor
Create a wrapped string.
-
#string ⇒ String
(also: #to_s)
Returns the string without any wrapping.
-
#wrap(width, width2 = nil) ⇒ Array<String>
Wraps the string to the given width.
Constructor Details
#initialize(string = "") ⇒ WrappableString
Create a wrapped string.
You can pass either:
- A single String, which will be split into fragments by whitespace.
- An array of Strings representing the fragments explicitly.
23 24 25 |
# File 'core-docs/toys/wrappable_string.rb', line 23 def initialize(string = "") # Source available in the toys-core gem end |
Instance Attribute Details
#fragments ⇒ Array<String> (readonly)
Returns the string fragments, i.e. the individual "words" for wrapping.
32 33 34 |
# File 'core-docs/toys/wrappable_string.rb', line 32 def fragments @fragments end |
Class Method Details
.make(obj) ⇒ Toys::WrappableString
Make the given object a WrappableString. If the object is already a WrappableString, return it. Otherwise, treat it as a string or an array of strings and wrap it in a WrappableString.
118 119 120 |
# File 'core-docs/toys/wrappable_string.rb', line 118 def self.make(obj) # Source available in the toys-core gem end |
.make_array(objs) ⇒ Array<Toys::WrappableString>
Make the given object an array of WrappableString.
128 129 130 |
# File 'core-docs/toys/wrappable_string.rb', line 128 def self.make_array(objs) # Source available in the toys-core gem end |
.wrap_lines(strs, width, width2 = nil) ⇒ Array<String>
Wraps an array of lines to the given width.
105 106 107 |
# File 'core-docs/toys/wrappable_string.rb', line 105 def self.wrap_lines(strs, width, width2 = nil) # Source available in the toys-core gem end |
Instance Method Details
#+(other) ⇒ WrappableString
Returns a new WrappaableString whose content is the concatenation of this WrappableString with another WrappableString.
41 42 43 |
# File 'core-docs/toys/wrappable_string.rb', line 41 def +(other) # Source available in the toys-core gem end |
#==(other) ⇒ Boolean Also known as: eql?
Tests two wrappable strings for equality
69 70 71 |
# File 'core-docs/toys/wrappable_string.rb', line 69 def ==(other) # Source available in the toys-core gem end |
#empty? ⇒ Boolean
Returns true if the string is empty (i.e. has no fragments)
50 51 52 |
# File 'core-docs/toys/wrappable_string.rb', line 50 def empty? # Source available in the toys-core gem end |
#hash ⇒ Integer
Returns a hash code for this object
78 79 80 |
# File 'core-docs/toys/wrappable_string.rb', line 78 def hash # Source available in the toys-core gem end |
#string ⇒ String Also known as: to_s
Returns the string without any wrapping
59 60 61 |
# File 'core-docs/toys/wrappable_string.rb', line 59 def string # Source available in the toys-core gem end |
#wrap(width, width2 = nil) ⇒ Array<String>
Wraps the string to the given width.
91 92 93 |
# File 'core-docs/toys/wrappable_string.rb', line 91 def wrap(width, width2 = nil) # Source available in the toys-core gem end |