Class: String
- Defined in:
- vendor/bundler/ruby/3.3.0/bundler/gems/sxp.rb-7a771a32c5fe/lib/sxp/extensions.rb,
vendor/bundler/ruby/3.3.0/bundler/gems/rdf-rdfa-ea6265716853/lib/rdf/rdfa/patches/string_hacks.rb
Overview
Extensions for Ruby's String
class.
Instance Attribute Summary collapse
-
#quote_style ⇒ :dquote, :squote
Record quote style used when parsing.
Instance Method Summary collapse
-
#align_left ⇒ Object
Trim beginning of each line by the amount of indentation in the first line.
-
#as_dquote? ⇒ Boolean
Render string using single quotes.
-
#as_squote? ⇒ Boolean
Render string using double quotes.
-
#to_sxp(**options) ⇒ String
Returns the SXP representation of this object.
Instance Attribute Details
#quote_style ⇒ :dquote, :squote
Record quote style used when parsing
89 90 91 |
# File 'vendor/bundler/ruby/3.3.0/bundler/gems/sxp.rb-7a771a32c5fe/lib/sxp/extensions.rb', line 89 def quote_style @quote_style end |
Instance Method Details
#align_left ⇒ Object
Trim beginning of each line by the amount of indentation in the first line
3 4 5 6 7 8 |
# File 'vendor/bundler/ruby/3.3.0/bundler/gems/rdf-rdfa-ea6265716853/lib/rdf/rdfa/patches/string_hacks.rb', line 3 def align_left str = self.sub(/^\s*$/, '') # Remove leading newline str = str[1..-1] if str[0,1] == "\n" ws = str.match(/^(\s*)\S/m) ? $1 : '' str.gsub(/^#{ws}/m, '') end |
#as_dquote? ⇒ Boolean
Render string using single quotes
95 |
# File 'vendor/bundler/ruby/3.3.0/bundler/gems/sxp.rb-7a771a32c5fe/lib/sxp/extensions.rb', line 95 def as_dquote?; quote_style != :squote; end |
#as_squote? ⇒ Boolean
Render string using double quotes
92 |
# File 'vendor/bundler/ruby/3.3.0/bundler/gems/sxp.rb-7a771a32c5fe/lib/sxp/extensions.rb', line 92 def as_squote?; quote_style == :squote; end |
#to_sxp(**options) ⇒ String
Returns the SXP representation of this object. Uses SPARQL-like escaping. Uses any recorded quote style from an originally parsed string.
62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 |
# File 'vendor/bundler/ruby/3.3.0/bundler/gems/sxp.rb-7a771a32c5fe/lib/sxp/extensions.rb', line 62 def to_sxp(**) buffer = "" each_char do |u| buffer << case u.ord when (0x00..0x07) then sprintf("\\u%04X", u.ord) when (0x08) then '\b' when (0x09) then '\t' when (0x0A) then '\n' when (0x0C) then '\f' when (0x0D) then '\r' when (0x0E..0x1F) then sprintf("\\u%04X", u.ord) when (0x22) then as_dquote? ? '\"' : '"' when (0x27) then as_squote? ? "\'" : "'" when (0x5C) then '\\\\' when (0x7F) then sprintf("\\u%04X", u.ord) else u.chr end end if as_dquote? '"' + buffer + '"' else "'" + buffer + "'" end end |