Skip to content

Instantly share code, notes, and snippets.

@mojombo
Created December 14, 2010 04:54
Show Gist options
  • Save mojombo/740018 to your computer and use it in GitHub Desktop.
Save mojombo/740018 to your computer and use it in GitHub Desktop.
config options mini parser thing
# Convert an array of nested key parts into a nested Hash and convert the
# value to a proper Ruby object.
#
# keyparts - An Array of String parts of the nested Hash. For instance, the
# list ['a', 'b', 'c'] would lead to a nested hash that looks like
# {'a' => {'b' => {'c' => X}}}.
# val - The String value that will be converted into the proper Boolean,
# Integer, or String.
#
# Returns the nested Hash.
def config_options(keyparts, val)
if keyparts.empty?
case val
when 'true' then true
when 'false' then false
when /^\d+$/ then val.to_i
else val
end
else
part = keyparts.shift
hash = {}
hash[part] = config_options(keyparts, val)
hash
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment