Last active
June 27, 2020 20:45
-
-
Save stevebartholomew/1feab9d6fa5128f618c4 to your computer and use it in GitHub Desktop.
Ruby refactoring exercise
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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", "[email protected]"].to_annotated_xml("person") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment