If all your keys and values are just digits (i.e., String#to_i likes them), then you can easily avoid eval and yaml. Just make your own parser. Here are some "learning tests" to get you started. If you avoid scrolling down too far to see my "answer", then just make the tests pass and you're done!
Of course, you can also expand the tests to cover even more exotic hash keys and values, but if you go too far you've just reinvented YAML or JSON with different syntax.
-Rob
require 'minitest/autorun'
class TestSomeCrappyMarkupLanguage < Minitest::Test
def setup
@str = "1=2,3=(4=5,6=7)"
end
def test_nil
refute SomeCrappyMarkupLanguage.parse(nil)
end
def test_empty_string
assert_equal({}, SomeCrappyMarkupLanguage.parse(""))
end
def test_simple_hash
assert_equal({1=>2}, SomeCrappyMarkupLanguage.parse("1=2"))
end
def test_two_elements
assert_equal({4=>5,6=>7}, SomeCrappyMarkupLanguage.parse("4=5,6=7"))
end
def test_nested
expected = { 1 => 2, 3 => { 4 => 5, 6 => 7 } }
assert_equal expected, SomeCrappyMarkupLanguage.parse(@str)
end
end
# spoilers below...
# spoilers below...
# spoilers below...
# spoilers below...
# spoilers below...
# spoilers below...
# spoilers below...
# spoilers below...
# spoilers below...
# spoilers below...
# spoilers below...
# spoilers below...
# spoilers below...
# spoilers below...
# spoilers below...
# spoilers below...
# spoilers below...
# spoilers below...
# spoilers below...
# spoilers below...
# spoilers below...
# spoilers below...
# spoilers below...
# spoilers below...
# spoilers below...
# spoilers below...
# spoilers below...
# spoilers below...
# spoilers below...
# spoilers below...
# spoilers below...
class SomeCrappyMarkupLanguage
def self.parse(str)
return nil unless str
result = {}
str.scan(/(\d+)=((?:\([^\)]*\))|\d+),?/).each do |key,value|
key = key.to_i
value = value =~ /\A\d+\z/ ? value.to_i : parse(value)
result[key] = value
end
result
end
end