class XPath::XPathString
Public Class Methods
Source
# File lib/xml/xpath.rb, line 800 def initialize(str) raise ::TypeError, "must be a String" unless str.is_a? String @value = str end
Public Instance Methods
Source
# File lib/xml/xpath.rb, line 839 def contain?(s) /#{Regexp.quote(s)}/ =~ @value end
Source
# File lib/xml/xpath.rb, line 898 def normalize_space @value = @value.strip @value.gsub!(/\s+/, ' ') self end
Source
# File lib/xml/xpath.rb, line 894 def size @value.gsub(/[^\Wa-zA-Z_\d]/, ' ').size end
Source
# File lib/xml/xpath.rb, line 835 def start_with?(s) /\A#{Regexp.quote(s)}/ =~ @value end
Source
# File lib/xml/xpath.rb, line 861 def substring(start, len) start = start.round.to_f if start.infinite? or start.nan? then @value = '' elsif len then len = len.round.to_f maxlen = start + len len = maxlen - 1.0 if len >= maxlen if start <= 1.0 then start = 0 else start = start.to_i - 1 end if len.nan? or len < 1.0 then @value = '' elsif len.infinite? then # @value = @value[start..-1] /\A[\W\w]{0,#{start}}/ =~ @value @value = $' else # @value = @value[start, len.to_i] /\A[\W\w]{0,#{start}}([\W\w]{0,#{len.to_i}})/ =~ @value @value = $1 end elsif start > 1.0 then # @value = @value[(start-1)..-1] /\A[\W\w]{0,#{start.to_i-1}}/ =~ @value @value = $' end raise "BUG" unless @value self end
Source
# File lib/xml/xpath.rb, line 852 def substring_after(s) if /#{Regexp.quote(s)}/ =~ @value then @value = $' else @value = '' end self end
Source
# File lib/xml/xpath.rb, line 843 def substring_before(s) if /#{Regexp.quote(s)}/ =~ @value then @value = $` else @value = '' end self end
Source
# File lib/xml/xpath.rb, line 809 def to_f if /\A\s*(-?\d+\.?\d*)(?:\s|\z)/ =~ @value then $1.to_f else 0.0 / 0.0 # NaN end end
Source
# File lib/xml/xpath.rb, line 904 def translate(from, to) to = to.split(//) h = {} from.split(//).each_with_index { |i,n| h[i] = to[n] unless h.key? i } @value = @value.gsub(/[#{Regexp.quote(h.keys.join)}]/) { |s| h[s] } self end