SQL to Ruby Converter

Transform SQL database dumps into Ruby arrays, hashes, and Struct objects for Rails seed data, testing fixtures, and Ruby applications

SQL Input

Ruby Output

About SQL to Ruby Converter

Convert SQL database dumps (CREATE TABLE and INSERT statements) to Ruby arrays, hashes, and Struct objects with automatic type detection. Perfect for Rails seed data, testing fixtures, Factory Bot, and Ruby applications.

Key Features

  • Array of Hashes: Most common format for Ruby data structures
  • Array of Arrays: Simple 2D array format with optional header row
  • Struct Objects: Type-safe Ruby Struct instances with named attributes
  • Symbol or String Keys: Choose between symbols (:key) or strings ("key")
  • Automatic Type Detection: Detects numbers, booleans, strings, and nil
  • String Escaping: Properly escapes special characters
  • Smart Parsing: Extracts column names and data from SQL dumps
  • File Download: Save as .rb file

How to Use

  1. Input SQL Data: Paste your SQL CREATE TABLE and INSERT statements or upload a .sql file
  2. Select Output Format: Choose between Array of Hashes, Array of Arrays, or Struct Objects
  3. Configure Options: Toggle symbol keys and header row inclusion
  4. Copy or Download: Use the Copy or Download button to save your Ruby code

Output Format Options

  • Array of Hashes: Best for Rails, ActiveRecord, and most Ruby applications
  • Array of Arrays: Simplest format, good for CSV-like data processing
  • Struct Objects: Type-safe with named attributes, great for data models

Example Conversion

SQL Input:

CREATE TABLE products (
  id INT,
  name VARCHAR(100),
  price DECIMAL(10,2),
  inStock BOOLEAN
);

INSERT INTO products VALUES (1, 'Laptop', 999.99, true);
INSERT INTO products VALUES (2, 'Mouse', 24.99, false);

Array of Hashes Output:

# Array of hashes format
data = [
  {
    :id => 1,
    :name => "Laptop",
    :price => 999.99,
    :in_stock => true
  },
  {
    :id => 2,
    :name => "Mouse",
    :price => 24.99,
    :in_stock => false
  }
]

# Access example:
# data[0][:id]

Struct Output:

# Struct format
Record = Struct.new(:id, :name, :price, :in_stock)

# Create instances
data = [
  Record.new(1, "Laptop", 999.99, true),
  Record.new(2, "Mouse", 24.99, false)
]

# Access examples:
# data[0].id
# data.each { |record| puts record.name }

Common Use Cases

  • Rails Seed Data: Generate db/seeds.rb data from SQL dumps
  • Testing Fixtures: Create test data for RSpec, Minitest
  • Factory Bot: Generate factory definitions from database data
  • Data Migration: Convert SQL data to Ruby for processing
  • API Responses: Mock API responses with realistic data
  • Configuration Files: Create Ruby config files from database
  • Data Processing: Import SQL data for Ruby scripts

Type Detection

The converter automatically determines the appropriate Ruby type:

  • Numbers: Integers and floats (1, 2.5, -10)
  • Booleans: true/false values
  • Strings: Text values with proper escaping
  • nil: NULL values become nil

Symbol vs String Keys

Symbols (recommended):

  • More idiomatic Ruby style
  • Better performance (immutable, cached)
  • Standard for Rails and most Ruby gems
  • Example: :name

Strings:

  • Compatible with JSON parsing
  • Required for some legacy code
  • Example: "name"

Rails Integration

Perfect for Rails applications:

  • Use in db/seeds.rb for database seeding
  • Create fixtures for testing
  • Generate Factory Bot factories
  • Mock data for development

Supported SQL Syntax

  • CREATE TABLE: Extracts column names for hash keys or struct fields
  • INSERT INTO: Parses data values with type detection
  • Data Types: Handles all SQL data types (VARCHAR, INT, DECIMAL, BOOLEAN, etc.)
  • Quoted Strings: Handles single and double quotes with proper escaping
  • NULL Values: Converted to nil

Privacy & Security

All conversions happen locally in your browser. Your SQL data is never uploaded to any server, ensuring complete privacy and security.