Class: String

Inherits:
Object show all
Defined in:
lib/nrser/core_ext/string.rb

Overview

Extension methods for String

Unicode Stylization collapse

Inflection Instance Methods collapse

Instance Method Summary collapse

Instance Method Details

#dedentObject



15
16
17
# File 'lib/nrser/core_ext/string.rb', line 15

def dedent
  NRSER.dedent self
end

#ellipsis(*args) ⇒ Object

Calls NRSER.ellipsis on ‘self`.



49
50
51
# File 'lib/nrser/core_ext/string.rb', line 49

def ellipsis *args
  NRSER.ellipsis self, *args
end

#env_varizenil, String

Attempt to convert ‘self` into an ENV var name.

Returns:

  • (nil)

    If we didn’t end up with a legal ENV var name.

  • (String)

    If we were able to munge a legal ENV var name.

See Also:



148
149
150
# File 'lib/nrser/core_ext/string.rb', line 148

def env_varize
  NRSER::Sys::Env.varize self
end

#indent(*args) ⇒ Object



20
21
22
# File 'lib/nrser/core_ext/string.rb', line 20

def indent *args
  NRSER.indent self, *args
end

#start_with?(*prefixes) ⇒ Boolean

Augment #start_with? to accept Regexp prefixes.

I guess I always *just felt* like this should work… so now it does (kinda, at least).

Everything should work the exact same for String prefixes.

Use Regexp ones at your own pleasure and peril.

Parameters:

  • *prefixes (String | Regexp)

    Strings behave as usual per the standard lib.

    Regexp sources are used to create a new Regexp with ‘A` at the start - unless their source already starts with `A` or `^` - and those Regexp are tested against the string.

    Regexp options are also copied over if a new Regexp is created. I can def imagine things getting weird with some exotic regular expression or another, but this feature is really indented for very simple patterns, for which it should suffice.

Returns:

  • (Boolean)

    ‘true` if `self` starts with any of the `prefixes`.



90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
# File 'lib/nrser/core_ext/string.rb', line 90

def start_with? *prefixes
  unless prefixes.any? { |x| Regexp === x }
    return stdlib_start_with? *prefixes
  end

  prefixes.any? { |prefix|
    case prefix
    when Regexp
      unless prefix.source.start_with? '\A', '^'
        prefix = Regexp.new( "\\A#{ prefix.source }", prefix.options )
      end
      
      prefix =~ self
    else
      stdlib_start_with? prefix
    end
  }
end

#stdlib_start_with?Object

Alias the stdlib #start_with? ‘cause we’ll need to use it when redefining the method below.



63
# File 'lib/nrser/core_ext/string.rb', line 63

alias_method :stdlib_start_with?, :start_with?

#to_constObject



25
26
27
# File 'lib/nrser/core_ext/string.rb', line 25

def to_const
  safe_constantize
end

#to_const!Object



30
31
32
# File 'lib/nrser/core_ext/string.rb', line 30

def to_const!
  constantize
end

#to_pnPathname

Returns Convert self into a Pathname.

Returns:



38
39
40
# File 'lib/nrser/core_ext/string.rb', line 38

def to_pn
  Pathname.new self
end

#u_boldObject

Calls NRSER.u_bold on ‘self`



120
121
122
# File 'lib/nrser/core_ext/string.rb', line 120

def u_bold
  NRSER.u_bold self
end

#u_bold_italicObject

Calls NRSER.u_bold_italic on ‘self`



126
127
128
# File 'lib/nrser/core_ext/string.rb', line 126

def u_bold_italic
  NRSER.u_bold_italic self
end

#u_italicObject

Calls NRSER.u_italic on ‘self`



114
115
116
# File 'lib/nrser/core_ext/string.rb', line 114

def u_italic
  NRSER.u_italic self
end

#u_monoObject

Calls NRSER.u_mono on ‘self`



132
133
134
# File 'lib/nrser/core_ext/string.rb', line 132

def u_mono
  NRSER.u_mono self
end

#unblockObject



10
11
12
# File 'lib/nrser/core_ext/string.rb', line 10

def unblock
  NRSER.unblock self
end

#whitespace?Boolean

Returns:

  • (Boolean)


43
44
45
# File 'lib/nrser/core_ext/string.rb', line 43

def whitespace?
  NRSER.whitespace? self
end

#words(*args, &block) ⇒ Object

Calls NRSER.words on ‘self`



55
56
57
# File 'lib/nrser/core_ext/string.rb', line 55

def words *args, &block
  NRSER::words self, *args, &block
end