YAML to Ruby
Turn YAML data into ready-to-use Ruby Struct type definitions with inferred field types.
Example
Input YAML:
name: Ada
age: 36
languages:
- Ruby
- Pascal
Generated Ruby:
Boolean = [TrueClass, FalseClass].freeze
Root = Struct.new(:name, :age, :languages, keyword_init: true) do
# name : String
# age : Integer
# languages : Array[String]
end
How it works
Parses your YAML with js-yaml, walks the resulting value to infer a type model, and emits Ruby Struct classes (named from keys, root type "Root") with type comments. Converts live as you type.
Good to know
YAML to Ruby takes a block of YAML and turns it into matching Ruby Struct class definitions, guessing the type of every field from the actual values you paste. It is aimed at Ruby developers who deal with config files, API fixtures, or serialized data in YAML and want a quick scaffold of typed objects instead of hand-writing each Struct.new call. Everything runs in your browser, so it is safe to paste internal or sensitive data.
Reach for it when you are modeling an unfamiliar YAML payload, prototyping a value object that mirrors a config schema, or migrating loosely-typed hashes toward explicit structs. Because it converts live as you type, it doubles as a fast way to sanity-check what types your YAML really contains, for example spotting that a value you assumed was an integer is being read as a string because it was quoted.
Read the output as a documentation sketch, not a finished contract. Each field carries a comment like # age : Integer, the root object is always named Root, nested mappings become their own PascalCase structs named after the key, and a Boolean = [TrueClass, FalseClass] alias is added whenever true/false values appear. Field names are normalized to snake_case and de-duplicated, and when a name had to change the original key is noted in a trailing comment so you can trace it back.
One caveat to watch: the tool infers types only from the sample you give it, so a field that happens to be empty, null, or single-valued in your snippet may be typed as NilClass, a bare Array, or a too-narrow type. Feed it a representative example that exercises every field, and treat mixed-type arrays (which fall back to plain Array) as a prompt to tighten the types yourself.
Frequently asked questions
What Ruby output style does it generate?
It emits keyword-initialized Struct definitions (Struct.new(:field, keyword_init: true)) with a type comment per field, plus a Boolean alias for true/false values. The root type is named Root.
How are nested objects and arrays handled?
Nested mappings become their own Struct classes named after the key (PascalCase). Arrays are typed as Array[ElementType] when all elements share a type, otherwise just Array; arrays of objects generate a singularized element struct.
Is my data uploaded anywhere?
No — this tool runs entirely in your browser. Your input never leaves your device and it works offline once loaded.
Is it free?
Yes, completely free with no sign-up and no limits.
People also ask
What is a Ruby Struct and why use it for YAML data?
A Ruby Struct is a lightweight class that bundles together named attributes, created with Struct.new. It is a convenient way to give parsed YAML data named, accessible fields instead of working with raw nested hashes.
What does keyword_init: true do in the generated structs?
keyword_init: true makes the struct accept keyword arguments, so you initialize it as Root.new(name: 'Ada', age: 36) rather than by positional order. This keeps construction readable and order-independent, which matches how YAML keys map to fields.
How does the tool decide between Integer and Float?
It checks whether the parsed number is a whole number; whole numbers become Integer and numbers with a decimal part become Float. Quoted numbers in YAML are read as strings and will be typed as String instead.
Does it handle nested objects and lists of objects?
Yes. Nested mappings become their own struct classes named after the key in PascalCase, and an array whose elements are objects produces a singularized element struct named from the key (for example projects yields a Project struct).
Will the generated Ruby code run as-is?
The struct definitions are valid Ruby and will define the classes, but the per-field types are comments only and are not enforced at runtime. You would add your own validation or use the Boolean alias and types as documentation.
What happens if my YAML is just a single value or invalid?
A scalar root produces a comment noting its type with Root assigned to that type, while invalid YAML shows a parse error message and clears the output. The tool relies on the js-yaml parser to read your input.
Is YAML to Ruby free and does my data stay private?
It is free with no sign-up, and it runs entirely client-side, so your input never leaves your browser and works offline once the page has loaded.
Related tools