Skip to content

Instantly share code, notes, and snippets.

@stevebartholomew
Last active June 27, 2020 20:45
Show Gist options
  • Save stevebartholomew/1feab9d6fa5128f618c4 to your computer and use it in GitHub Desktop.
Save stevebartholomew/1feab9d6fa5128f618c4 to your computer and use it in GitHub Desktop.
Ruby refactoring exercise
class Array
def to_annotated_xml(root)
output = "<#{root}>"
each do |i|
if i.is_a?(Fixnum)
output << "<number>#{i}</number>"
elsif i.is_a?(String)
if i.match(/@/)
output << "<email>#{i}</email>"
else
output << "<string>#{i}</string>"
end
else
output << "<value>#{i}</value>"
end
end
output << "</#{root}>"
end
end
p [1, "Stephen", "stephenb@reallyenglish.com"].to_annotated_xml("person")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment