MediaWiki to Ruby Converter
Transform MediaWiki tables into Ruby data structures including arrays, hashes, and Struct objects
MediaWiki Input
Ruby Output
About MediaWiki to Ruby Converter
Convert MediaWiki table markup to Ruby data structures including arrays, hashes, and Struct objects. Perfect for Rails seed data, testing fixtures, and Ruby scripts with automatic type detection and proper string escaping.
Key Features
- Array of Hashes: Most common format with key-value pairs for each row
- Array of Arrays: Simple nested array structure for basic data
- Struct Objects: Type-safe data structures with named attributes
- Symbol Keys: Option to use symbols (Ruby best practice) or strings
- Type Detection: Automatically detects numeric values
- String Escaping: Proper escaping of special characters
- MediaWiki Parser: Accurately parses MediaWiki table syntax
- File Upload: Upload .wiki files directly
- Instant Preview: Real-time conversion as you type
- Copy & Download: Easy export as .rb file
How to Use
- Input MediaWiki Table: Paste your MediaWiki table markup or upload a .wiki file
- Select Output Format: Choose between Hash, Array, or Struct format
- Configure Options: Toggle symbol keys and header inclusion
- Review Output: The Ruby code updates automatically
- Export: Copy to clipboard or download as .rb file
Output Formats
- Array of Hashes: Best for Rails models, JSON serialization, and database operations. Each row is a hash with column names as keys.
- Array of Arrays: Simple format for CSV-like data. First row can optionally be headers. Good for basic data processing.
- Struct Objects: Type-safe objects with attribute accessors. Perfect for domain models and value objects.
Example Conversion
MediaWiki Input:
{| class="wikitable" border="1"
! Name !! Age !! City !! Department
|-
| John Doe || 28 || New York || Engineering
|-
| Jane Smith || 34 || London || Marketing
|} Array of Hashes Output:
data = [
{
:name => "John Doe",
:age => 28,
:city => "New York",
:department => "Engineering"
},
{
:name => "Jane Smith",
:age => 34,
:city => "London",
:department => "Marketing"
}
] Struct Objects Output:
# Define Struct
Record = Struct.new(:name, :age, :city, :department)
# Create instances
data = [
Record.new("John Doe", 28, "New York", "Engineering"),
Record.new("Jane Smith", 34, "London", "Marketing")
]
# Access example:
# data[0].name Common Use Cases
- Rails Seed Data: Generate db/seeds.rb content for database seeding
- Testing Fixtures: Create test data for RSpec, Minitest, or Cucumber
- Data Migration: Prepare data for Rails migrations
- Wikipedia Data: Convert Wikipedia tables to Ruby data structures
- Configuration Files: Generate Ruby configuration data
- API Responses: Mock API response data for testing
- Data Processing: Create Ruby scripts for data transformation
- Factory Bot: Generate factory definitions for testing
MediaWiki Table Syntax
Supports standard MediaWiki table markup:
- {|: Table opening with optional attributes
- ! Header: Exclamation mark for header cells (separated by !!)
- |-: Row separator
- | Data: Pipe for data cells (separated by ||)
- |}: Table closing
Symbol vs String Keys
- Symbols (:key): Ruby best practice, more memory efficient, immutable, faster comparison
- Strings ("key"): Useful for JSON compatibility, dynamic key generation, external data
Type Detection
The converter automatically detects data types:
- Numbers: Integer and decimal values are output without quotes
- Strings: Text values are properly quoted and escaped
- Empty Values: Converted to empty strings ""
String Escaping
Special characters are properly escaped:
- Backslashes: \\ → \\\\
- Single quotes: ' → \\'
- Double quotes: " → \\"
- Newlines: \\n
- Tabs: \\t
- Carriage returns: \\r
Rails Integration
Seed Data Example:
# db/seeds.rb
data = [
{ name: "John Doe", age: 28, city: "New York" },
{ name: "Jane Smith", age: 34, city: "London" }
]
data.each do |record|
User.create!(record)
end RSpec Testing Example
# spec/fixtures/users.rb
USERS = [
{ name: "John Doe", age: 28, city: "New York" },
{ name: "Jane Smith", age: 34, city: "London" }
]
# In your spec
let(:user_data) { USERS.first } Struct Benefits
- Type Safety: Attribute accessors with defined structure
- Performance: Faster than hashes for repeated access
- Readability: Clear attribute names with dot notation
- Immutability: Can be frozen for immutable data
- Comparison: Built-in equality and comparison methods
Wikipedia Integration
Perfect for extracting Wikipedia data:
- Convert Wikipedia tables to Ruby data
- Use for educational projects
- Build reference databases
- Create data-driven applications
- Extract structured information
Best Practices
- Use symbols for hash keys (Ruby convention)
- Choose hashes for Rails models and ActiveRecord
- Choose structs for value objects and domain models
- Choose arrays for simple CSV-like data processing
- Include headers in arrays only when needed for processing
- Validate generated code in Ruby console before use
- Consider using OpenStruct for more dynamic structures
Factory Bot Integration
# spec/factories/users.rb
FactoryBot.define do
factory :user do
name { "John Doe" }
age { 28 }
city { "New York" }
department { "Engineering" }
end
end Tips for Best Results
- Use hash format for most Rails applications
- Enable symbol keys for better Ruby performance
- Use struct format for domain-driven design
- Test generated code in IRB or Rails console
- Consider data validation after conversion
- Use meaningful column names that become valid Ruby identifiers
- Combine with ActiveRecord for database operations
- Extract Wikipedia tables for data-driven projects
Privacy & Security
All conversions happen locally in your browser. Your MediaWiki data is never uploaded to any server, ensuring complete privacy and security.
