Class: String
- Defined in:
- lib/ruby/jruby_hack.rb,
lib/ruby/jruby_hack.rb,
lib/ruby/jruby_hack.rb,
lib/ruby/jruby_hack.rb,
lib/ruby/jruby_hack.rb
Overview
lib/ruby/string.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
443 444 445 446 |
# File 'lib/ruby/jruby_hack.rb', line 443 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
235 236 237 |
# File 'lib/ruby/jruby_hack.rb', line 235 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`
495 496 497 |
# File 'lib/ruby/jruby_hack.rb', line 495 def defined_at?(n) n < length end |
#drop(n) ⇒ String
Return the string with ‘n` characters removed from the front
457 458 459 460 |
# File 'lib/ruby/jruby_hack.rb', line 457 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!
501 502 503 |
# File 'lib/ruby/jruby_hack.rb', line 501 def position nil end |
#present? ⇒ Boolean
239 240 241 |
# File 'lib/ruby/jruby_hack.rb', line 239 def present? self =~ /\S/ end |
#split_at(n) ⇒ Array(String, String)
Split the string in two at the given position
485 486 487 |
# File 'lib/ruby/jruby_hack.rb', line 485 def split_at(n) [take(n), drop(n)] end |
#take(n) ⇒ String
Return the first ‘n` characters from the front
471 472 473 474 |
# File 'lib/ruby/jruby_hack.rb', line 471 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.
553 554 555 556 557 558 559 |
# File 'lib/ruby/jruby_hack.rb', line 553 def to_d if BIGDECIMAL =~ self BigDecimal(to_s) else raise ArgumentError, "#{inspect} is not a valid number" end end |