Class: Chars
Overview
Unichars is a proxy class for String. It’s used by Unichars as a trimmed down version of ActiveSupport::Multibyte::Chars when ActiveSupport isn’t loaded.
Chars.new('João') #=> #<Chars:0x34c240 @wrapped_string="João">
Instance Attribute Summary collapse
-
#wrapped_string ⇒ Object
(also: #to_s, #to_str)
readonly
Returns the value of attribute wrapped_string.
Instance Method Summary collapse
-
#+(other) ⇒ Object
Returns a new Chars object containing the other object concatenated to the string.
-
#<=>(other) ⇒ Object
Returns
-1
,0
or+1
depending on whether the Chars object is to be sorted before, equal or after the object on the right side of the operation. -
#initialize(string) ⇒ Chars
constructor
:nodoc:.
-
#method_missing(method, *args, &block) ⇒ Object
Forward all undefined methods to the wrapped string.
-
#respond_to?(method, include_private = false) ⇒ Boolean
Returns
true
if obj responds to the given method.
Constructor Details
#initialize(string) ⇒ Chars
:nodoc:
14 15 16 |
# File 'lib/chars.rb', line 14 def initialize(string) @wrapped_string = string.dup.force_encoding(Encoding::UTF_8) end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(method, *args, &block) ⇒ Object
Forward all undefined methods to the wrapped string.
24 25 26 27 28 29 30 31 32 |
# File 'lib/chars.rb', line 24 def method_missing(method, *args, &block) if method.to_s =~ /!$/ @wrapped_string.send(method, *args, &block) self else result = @wrapped_string.send(method, *args, &block) result.kind_of?(String) ? self.class.new(result) : result end end |
Instance Attribute Details
#wrapped_string ⇒ Object (readonly) Also known as: to_s, to_str
Returns the value of attribute wrapped_string.
8 9 10 |
# File 'lib/chars.rb', line 8 def wrapped_string @wrapped_string end |
Instance Method Details
#+(other) ⇒ Object
Returns a new Chars object containing the other object concatenated to the string.
Example:
('Café'.mb_chars + ' périferôl').to_s #=> "Café périferôl"
56 57 58 |
# File 'lib/chars.rb', line 56 def +(other) self << other end |
#<=>(other) ⇒ Object
Returns -1
, 0
or +1
depending on whether the Chars object is to be sorted before, equal or after the object on the right side of the operation. It accepts any object that implements to_s
. See String#<=>
for more details.
Example:
'é'.mb_chars <=> 'ü'.mb_chars #=> -1
48 49 50 |
# File 'lib/chars.rb', line 48 def <=>(other) @wrapped_string <=> other.to_s end |
#respond_to?(method, include_private = false) ⇒ Boolean
Returns true
if obj responds to the given method. Private methods are included in the search only if the optional second parameter evaluates to true
.
36 37 38 |
# File 'lib/chars.rb', line 36 def respond_to?(method, include_private=false) super || @wrapped_string.respond_to?(method, include_private) || false end |