Class: String

Inherits:
Object show all
Defined in:
lib/ruby/to_d.rb,
lib/ruby/blank.rb,
lib/ruby/string.rb,
lib/ruby/to_date.rb,
lib/ruby/to_time.rb

Constant Summary collapse

BIGDECIMAL =
/\A[+-]?            (?# optional leading sign            )
 (?:
   (?:\d+\.?\d*)  | (?# whole with optional decimal or ..)
   (?:\d*?\.?\d+) ) (?# optional whole with decimal      )
 (?:E[+-]?\d+)?     (?# optional exponent                )
\Z/ix

Instance Method Summary collapse

Instance Method Details

#at(n) ⇒ String

Return the one-character string at the given index

Examples:

"abc".at(0)   #=> "a"
"abc".at(2)   #=> "c"

Parameters:

  • n (Integer)

    zero-based index of the character to read

Returns:

Raises:

  • (ArgumentError)


12
13
14
15
# File 'lib/ruby/string.rb', line 12

def at(n)
  raise ArgumentError, "n must be positive" if n < 0
  self[n, 1] unless n >= length
end

#blank?Boolean

True if the string is ‘empty?` or contains all whitespace

Examples:

"abc".blank?    #=> false
"   ".blank?    #=> true
"".blank?       #=> true

Returns:

  • (Boolean)


10
11
12
# File 'lib/ruby/blank.rb', line 10

def blank?
  self !~ /\S/
end

#defined_at?(n) ⇒ Boolean

True if the string is long enough such that #at is defined for the given ‘n`

Examples:

"abc".defined_at?(0)  #=> true
"abc".defined_at?(3)  #=> false

Returns:

  • (Boolean)


64
65
66
# File 'lib/ruby/string.rb', line 64

def defined_at?(n)
  n < length
end

#drop(n) ⇒ String

Return the string with ‘n` characters removed from the front

Examples:

"abc".drop(0)   #=> "abc"
"abc".drop(2)   #=> "c"

Parameters:

  • n (Integer)

    number of characters to drop (‘n > 0`)

Returns:

Raises:

  • (ArgumentError)


26
27
28
29
# File 'lib/ruby/string.rb', line 26

def drop(n)
  raise ArgumentError, "n must be positive" if n < 0
  (length >= n) ? self[n..-1] : ""
end

#positionObject

To make String compatible with the Stupidedi::Reader::Input interface, we have to define ‘#position`… shameful!



70
71
72
# File 'lib/ruby/string.rb', line 70

def position
  nil
end

#present?Boolean

Returns:

  • (Boolean)


14
15
16
# File 'lib/ruby/blank.rb', line 14

def present?
  self =~ /\S/
end

#split_at(n) ⇒ Array(String, String)

Split the string in two at the given position

Examples:

"abc".split_at(0)   #=> ["", "abc"]
"abc".split_at(2)   #=> ["ab", "c"]

Parameters:

  • n (Integer)

    number of characters at which to split (‘n > 0`)

Returns:



54
55
56
# File 'lib/ruby/string.rb', line 54

def split_at(n)
  [take(n), drop(n)]
end

#take(n) ⇒ String

Return the first ‘n` characters from the front

Examples:

"abc".take(0)   #=> ""
"abc".take(2)   #=> "ab"

Parameters:

  • n (Integer)

    number of characters to select (‘n > 0`)

Returns:

Raises:

  • (ArgumentError)


40
41
42
43
# File 'lib/ruby/string.rb', line 40

def take(n)
  raise ArgumentError, "n must be positive" if n < 0
  self[0, n]
end

#to_dBigDecimal

Converts the string to a BigDecimal after validating the format. If the string does not match the pattern for a valid number, an ‘ArgumentError` is raised.

Examples:

"1.0".to_d  #=> BigDecimal("1.0")

Returns:



28
29
30
31
32
33
34
# File 'lib/ruby/to_d.rb', line 28

def to_d
  if BIGDECIMAL =~ self
    BigDecimal(to_s)
  else
    raise ArgumentError, "#{inspect} is not a valid number"
  end
end

#to_dateObject



13
14
15
# File 'lib/ruby/to_date.rb', line 13

def to_date
  Date.parse(self)
end

#to_timeObject



18
19
20
# File 'lib/ruby/to_time.rb', line 18

def to_time
  Time.parse(self)
end