|
| 1 | +# Ruby `Hash#inspect` / `Hash#to_s` format |
| 2 | + |
| 3 | +Several specs render a hash (or array of hashes) through `{{ var }}` and expect |
| 4 | +Ruby's `Hash#inspect` output — **not** JSON. In Ruby, `Hash#to_s` is an alias |
| 5 | +for `Hash#inspect`, so `{{ my_hash }}` (which calls `to_s` on the resolved |
| 6 | +value) produces the format below. A non-Ruby Liquid implementation must |
| 7 | +reproduce it exactly to pass these specs. |
| 8 | + |
| 9 | +This is a cross-language correctness bar: Liquid defers object rendering to the |
| 10 | +host language's `to_s`, and the reference implementation is Ruby, so the |
| 11 | +expected strings are Ruby's `inspect` output. |
| 12 | + |
| 13 | +## Grammar |
| 14 | + |
| 15 | +``` |
| 16 | +hash_inspect = "{" pairs? "}" |
| 17 | +pairs = pair (", " pair)* |
| 18 | +pair = key_inspect "=>" value_inspect |
| 19 | +array_inspect = "[" elements? "]" |
| 20 | +elements = elem_inspect (", " elem_inspect)* |
| 21 | +``` |
| 22 | + |
| 23 | +`=>` is the **rocket**, never `:`. Pairs are in **insertion order**. The |
| 24 | +separator is `", "` (comma + one space). The whole thing is wrapped in `{` `}`. |
| 25 | + |
| 26 | +## Scalar inspect rules |
| 27 | + |
| 28 | +| Ruby type | Inspect output | Notes | |
| 29 | +|-------------|-----------------------|-------| |
| 30 | +| `String` | `"foo"` | double-quoted, with escapes (below) | |
| 31 | +| `Symbol` | `:foo` | leading colon, no quotes | |
| 32 | +| `Integer` | `42` | bare | |
| 33 | +| `Float` | `1.0` | always has a decimal part (`1.0`, not `1`) | |
| 34 | +| `nil` | `nil` | the literal word | |
| 35 | +| `true` | `true` | | |
| 36 | +| `false` | `false` | | |
| 37 | +| `Array` | `[a, b, c]` | each element via its own inspect, `, ` separated | |
| 38 | +| `Hash` | `{k=>v, ...}` | recursive | |
| 39 | +| self-ref | `{...}` | a hash that refers to an ancestor in the same inspect call renders as `{...}` | |
| 40 | + |
| 41 | +## String escapes (inside double quotes) |
| 42 | + |
| 43 | +The string's `inspect` doubles `\\` and `\"`, and turns control characters into |
| 44 | +their escaped forms: `\n`, `\t`, `\r`, `\f`, `\b`, `\a`, `\e`, `\v`, `\0` / |
| 45 | +`\u0000`-style for other non-printables. Printable ASCII is emitted literally. |
| 46 | +This matters for specs where the hash is piped through `escape`/`url_encode` |
| 47 | +*after* being stringified — the inspect output is the input to those filters. |
| 48 | + |
| 49 | +## Worked examples (all drawn from real specs) |
| 50 | + |
| 51 | +| Input (Ruby) | Expected output | |
| 52 | +|-----------------------------------------------|----------------------------------------| |
| 53 | +| `{}` | `{}` | |
| 54 | +| `{"key1"=>"value1", "key2"=>"value2"}` | `{"key1"=>"value1", "key2"=>"value2"}` | |
| 55 | +| `{"numbers"=>[1, 2, 3]}` | `{"numbers"=>[1, 2, 3]}` | |
| 56 | +| `{"numbers"=>[]}` | `{"numbers"=>[]}` | |
| 57 | +| `{"outer"=>{"inner"=>"value"}}` | `{"outer"=>{"inner"=>"value"}}` | |
| 58 | +| `{{"foo"=>"bar"}=>42}` (hash key) | `{{"foo"=>"bar"}=>42}` | |
| 59 | +| `{:key1=>1, :key2=>2}` (symbol keys) | `{:key1=>1, :key2=>2}` | |
| 60 | +| recursive `{"self"=>{"self"=>{...}}}` | `{"self"=>{"self"=>{...}}}` | |
| 61 | + |
| 62 | +## How filters interact |
| 63 | + |
| 64 | +When a hash is the input to a string filter, the hash is **first** stringified |
| 65 | +via `inspect`, **then** the filter runs on that string. Examples: |
| 66 | + |
| 67 | +- `{{ my_hash | upcase }}` → `{"KEY1"=>"VALUE1", ...}` (inspect, then uppercased) |
| 68 | +- `{{ my_hash | downcase }}` → `{"key1"=>"value1", ...}` |
| 69 | +- `{{ my_hash | append: " text" }}` → `{"Key"=>"Value"} text` |
| 70 | +- `{{ my_hash | replace: "key", "KEY" }}` → replaces inside the inspect string |
| 71 | +- `{{ my_hash | escape }}` → HTML-escapes the inspect string |
| 72 | + (`{"Key"=>"Value"}`) |
| 73 | + |
| 74 | +So the inspect format is the *substrate* every string filter operates on when the |
| 75 | +input is a hash. |
| 76 | + |
| 77 | +## Pseudocode for a non-Ruby implementation |
| 78 | + |
| 79 | +```text |
| 80 | +function rubyInspect(value, seen = new Set()): |
| 81 | + if value is a Hash/Map: |
| 82 | + if value in seen: return "{...}" # cycle guard |
| 83 | + seen.add(value) |
| 84 | + if value is empty: return "{}" |
| 85 | + parts = [] |
| 86 | + for (k, v) in value in insertion order: |
| 87 | + parts.push(rubyInspect(k, seen) + "=>" + rubyInspect(v, seen)) |
| 88 | + return "{" + parts.join(", ") + "}" |
| 89 | + if value is an Array: |
| 90 | + if value is empty: return "[]" |
| 91 | + return "[" + value.map(rubyInspect).join(", ") + "]" |
| 92 | + if value is a String: return rubyStringInspect(value) # double-quoted + escapes |
| 93 | + if value is a Symbol: return ":" + value.name # no quotes |
| 94 | + if value is null/nil: return "nil" |
| 95 | + if value is Boolean: return value ? "true" : "false" |
| 96 | + if value is Integer: return String(value) |
| 97 | + if value is Float: return floatInspect(value) # always show a decimal |
| 98 | + # other objects: defer to the host's inspect/to_s if you emulate ruby_types |
| 99 | +``` |
| 100 | + |
| 101 | +`rubyStringInspect(s)` wraps `s` in `"..."`, escaping `\\` and `"` and control |
| 102 | +chars as above. `floatInspect` must always include a decimal point (`1.0`, not |
| 103 | +`1`; `0.2`, not `0.2` rendered as a bare fraction — match Ruby's `Float#inspect`, |
| 104 | +which uses the shortest representation that round-trips). |
| 105 | + |
| 106 | +## When you need this |
| 107 | + |
| 108 | +These specs are gated behind the **Ruby/reference-quirk** complexity band |
| 109 | +(~220+) and most are tagged `features: [ruby_types]`. JSON-RPC adapters skip |
| 110 | +`ruby_types` by default. If you opt in (remove `:ruby_types` from |
| 111 | +`missing_features`), the JSON-RPC transport delivers non-string hash keys to you |
| 112 | +as their Ruby `inspect` string, and your engine is expected to render hashes and |
| 113 | +those keys per the rules above. |
0 commit comments