Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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
You can’t perform that action at this time.