Class: String

Inherits:
Object show all
Defined in:
lib/hash-utils/string.rb

Overview

String extension.

Constant Summary collapse

NUMERIC =

Holds numeric matcher.

/^\s*-?\d+(?:\.\d+)?\s*$/

Instance Method Summary collapse

Instance Method Details

#firstString

Returns first character of the string.

Examples:

"abc".first     # will return 'a'

Returns:

  • (String)

    first character

Since:

  • 0.11.0



203
204
205
# File 'lib/hash-utils/string.rb', line 203

def first
    self[0].chr
end

#gsub_f(from, to = nil, &block) ⇒ String

Replaces all substrings defined by Regexp by complex way. It means, it gives to block not only whole match, but submatches too. In fact, emulates PHP’s preg_replace_callback() function. In other ways emulates standard #gsub.

Parameters:

  • regexp (Regexp)

    matching expression

  • to (String) (defaults to: nil)

    new string

  • block (Proc)

    block which will receive each match

Returns:

  • (String)

    resultant string

See Also:

Since:

  • 0.8.0



159
160
161
162
163
164
165
166
167
# File 'lib/hash-utils/string.rb', line 159

def gsub_f(from, to = nil, &block) 
    __prepare_gsub_f(from, to, block) do |callback|
        if to.nil?
            self.gsub(from, &callback)
        else
            self.gsub(from, to)
        end
    end
end

#gsub_f!(from, to = nil, &block) ⇒ String

Performs complex regexp replacing same as #gsub_f, but in place. In other ways emulates standard #gsub!.

Parameters:

  • from (Regexp)

    matching expression

  • to (String) (defaults to: nil)

    new string

  • block (Proc)

    block which will receive each match

Returns:

  • (String)

    resultant string

See Also:

Since:

  • 0.8.0



181
182
183
184
185
186
187
188
189
190
191
# File 'lib/hash-utils/string.rb', line 181

def gsub_f!(from, to = nil, &block) 
    __prepare_gsub_f(from, to, block) do |callback|
        if to.nil?
            self.gsub!(from, &callback)
        else
            self.gsub!(from, to)
        end
    end
    
    self
end

#lastString

Returns last character of the string.

Examples:

"abc".last     # will return 'c'

Returns:

Since:

  • 0.11.0



217
218
219
# File 'lib/hash-utils/string.rb', line 217

def last
    self[-1].chr
end

#map(&block) ⇒ Object

Applies block to each character and returns resultant string.

Examples:

foo = "012"
puts foo.map { |ch| (ch.to_i + 1).to_s }.inspect   # prints out "123"

Parameters:

  • block (Proc)

    transforming block

  • transformed (String)

    string

Since:

  • 0.6.0



119
120
121
122
123
124
125
126
# File 'lib/hash-utils/string.rb', line 119

def map(&block)
    buffer = " " * self.length
    self.length.times do |i|
        buffer[i] = block.call(self[i])
    end
            
    return buffer
end

#map!(&block) ⇒ String

Applies block to each character in place. For example see #map.

Parameters:

  • block (Proc)

    transforming block

Returns:

See Also:

Since:

  • 0.6.0



137
138
139
140
141
142
143
# File 'lib/hash-utils/string.rb', line 137

def map!(&block)
    self.length.times do |i|
        self[i] = block.call(self[i])
    end
    
    self
end

#numeric?Boolean

Indicates, string is numeric, so consists of numbers only.

Returns:

  • (Boolean)

    true if yes, false in otherwise

Since:

  • 0.3.0



26
27
28
29
30
31
32
# File 'lib/hash-utils/string.rb', line 26

def numeric?
    if self.match(self.class::NUMERIC)
        true
    else
        false
    end
end

#strtr(defs, mode = nil, &block) ⇒ String

Replaces set of substrings by another strings.

It’s equivalent of PHP strtr() function. Supports all objects convertable to String, but in that case you must give him block which specifies how to map keys found in the string back to keys in definition Hash or Array.

If you specify the :flat mode, definition array items will be treaten as arguments to Hash#[] method while internal conversion Array to Hash, so then can then use plain array as base for definitions. See Array#to_h.

Examples:

Equivalent calls

"aa bb".tr("aa" => "bb", "bb" => "aa")
"aa bb".tr([["aa", "bb"], ["bb", "aa"]])
"aa bb".tr(["aa", "bb", "bb", "aa"], :flat)

Use with symbols

"aa bb".tr(:aa => "bb", :bb => "aa") { |s| s.to_sym }

Parameters:

  • replacements (Array, Hash)

    replacements definition

  • mode (Symbol) (defaults to: nil)

    flat mode switch, can be :flat or nil

  • block (Proc)

    with keys mapping worker (see description)

Returns:

  • (String)

    string with applied replacements

See Also:

Since:

  • 0.4.0



62
63
64
65
66
67
68
69
# File 'lib/hash-utils/string.rb', line 62

def strtr(defs, mode = nil, &block)
    if block.nil?
        block = Proc::new { |s| s }
    end
    
    defs, matcher = __prepare_strtr(defs, mode)
    self.gsub(matcher) { |s| defs[block.call(s)] }
end

#strtr!(defs, mode = nil, &block) ⇒ String

Replaces set of substrings by another strings – in place. See #strtr for details.

Parameters:

  • replacements (Array, Hash)

    replacements definition

  • mode (Symbol) (defaults to: nil)

    flat mode switch, can be :flat or nil

  • block (Proc)

    with keys mapping worker (see description)

Returns:

  • (String)

    string with applied replacements

See Also:

Since:

  • 0.4.0



83
84
85
86
87
88
89
90
# File 'lib/hash-utils/string.rb', line 83

def strtr!(defs, mode = nil, &block)
    if block.nil?
        block = Proc::new { |s| s }
    end
    
    defs, matcher = __prepare_strtr(defs, mode)
    self.gsub!(matcher) { |s| defs[block.call(s)] }
end

#to_aArray

Converts String to Array of characters.

Examples:

foo = "012"
puts foo.to_a.inspect   # prints out ["0", "1", "2"]

Returns:

  • (Array)

    array of single character strings

Since:

  • 0.6.0



103
104
105
# File 'lib/hash-utils/string.rb', line 103

def to_a
    self.split("")
end