Class: String
- 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
-
#at(n) ⇒ String
Return the one-character string at the given index.
-
#blank? ⇒ Boolean
True if the string is ‘empty?` or contains all whitespace.
-
#defined_at?(n) ⇒ Boolean
True if the string is long enough such that #at is defined for the given ‘n`.
-
#drop(n) ⇒ String
Return the string with ‘n` characters removed from the front.
-
#position ⇒ Object
To make String compatible with the Stupidedi::Reader::Input interface, we have to define ‘#position`…
- #present? ⇒ Boolean
-
#split_at(n) ⇒ Array(String, String)
Split the string in two at the given position.
-
#take(n) ⇒ String
Return the first ‘n` characters from the front.
-
#to_d ⇒ BigDecimal
Converts the string to a BigDecimal after validating the format.
- #to_date ⇒ Object
- #to_time ⇒ Object
Instance Method Details
#at(n) ⇒ String
Return the one-character string at the given index
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
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`
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
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 |
#position ⇒ Object
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
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
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
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_d ⇒ BigDecimal
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.
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 |