class String

Public Instance Methods

end_with?(string) click to toggle source
# File lib/ayril/core_ext/core_ext.rb, line 91
def end_with?(string) self.index(string) == (self.length - string.length) end
interpolate(object, pattern=/(^|.|\r|\n)( click to toggle source
# File lib/ayril/core_ext/core_ext.rb, line 49
def interpolate(object, pattern=/(^|.|\r|\n)(#\{(.*?)\})/)
  self.gsub(pattern) do |match|
    return '' if object.nil?

    before = $1 || ''
    return $2 if before == '\\'

    ctx = object; expr = $3
    pattern = /^([^.\[]+|\[((?:.*?[^\\])?)\])(\.|\[|$)/
    match = pattern.match(expr)
    return before if match.nil?
    
    while not match.nil?
      comp = match[1].start_with?('[') ? match[2].gsub('\\\\]', ']') : match[1]
      if ctx.kind_of?(Array) or ctx.kind_of?(MatchData)
        ctx = ctx[comp.to_i]
      elsif ctx.kind_of?(Hash)
        types = ctx.keys.invoke(:class)
        
        major_type = types.uniq.map do |type| 
          [type, types.find_all { |i| i == type }.length]
        end.max { |a, b| a[1] <=> b[1] }[0]
        
        method = {
          Symbol => :to_sym,
          String => :to_s
        }[major_type]

        ctx = ctx[comp.send method]
      else
        ctx = ctx[comp]
      end
      break if ctx.nil? or match[3] == ''
      expr = expr[(match[3] == '[' ? match[1].length : match[0].length)..-1]
      match = pattern.match expr
    end
    
    before + ctx.to_s
  end
end
start_with?(string) click to toggle source
# File lib/ayril/core_ext/core_ext.rb, line 90
def start_with?(string) self.index(string) == 0 end
to_elem() click to toggle source
# File lib/ayril/core_ext/core_ext.rb, line 93
def to_elem; Ayril::XMLElement.new self end