Skip to content

Instantly share code, notes, and snippets.

@Theminijohn
Last active August 29, 2015 14:02
Show Gist options
  • Save Theminijohn/564ce8ebf3577aee3014 to your computer and use it in GitHub Desktop.
Save Theminijohn/564ce8ebf3577aee3014 to your computer and use it in GitHub Desktop.
# 1st Example
raw_metas = file_content.scan( /\A\/\/\s==UserScript==(\w|\W)*\/\/\s==\/UserScript==$/ )
raw_metas.split(/\r\n|\n|\r/).each do |line_with_meta|
#each line is structured like @attribute<space>value
end
#so in this loop, we need another regex to get the attribute's name, and the value
# 2nd Example
raw_metas.split(/\r\n|\n|\r/).each do |line_with_meta|
attribute_name = line_with_data.scan(/@\w+/)
end
# 3nd Example
raw_metas.split(/\r\n|\n|\r/).each do |line_with_meta|
attribute_name = line_with_data.scan(/@\w+/)
value = line_with_data.sub("// #{attribute_name}", '').strip
end
# 4th Example
metas = {}
raw_metas.split(/\r\n|\n|\r/).each do |line_with_meta|
attribute_name = line_with_data.scan(/@\w+/)
value = line_with_data.sub("// #{attribute_name}", '').strip
metas[attribute_name.sub('@', '').to_sym] = value
end
# Last Example
metas = {}
raw_metas.split(/\r\n|\n|\r/).each do |line_with_meta|
attribute_name = line_with_data.scan(/@\w+/)
value = line_with_data.sub("// #{attribute_name}", '').strip
if metas[attribute_name.sub('@', '').to_sym].present?
metas[attribute_name.sub('@', '').to_sym] = [ metas[attribute_name.sub('@', '').to_sym], value].flatten
else
metas[attribute_name.sub('@', '').to_sym] = value
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment