-
-
Save oleander/7b988a4cfbfd3e07b642 to your computer and use it in GitHub Desktop.
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
require "pp" | |
data = "123. This is a question A Option One B Option two C Danke D Nope" | |
options = {} | |
("A".."D").to_a.reverse.each do |letter| | |
data, letter, option = data.split(/ (#{letter}) (.+?)$/, 3) | |
options[letter] = option | |
end | |
_, number, question = data.match(/^(\d+)\. (.+)$/).to_a | |
result = { | |
number: number, | |
question: question, | |
options: options | |
} | |
pp result | |
# {:number=>"123", | |
# :question=>"This is a question", | |
# :options=>{"D"=>"Nope", "C"=>"Danke", "B"=>"Option two", "A"=>"Option One"}} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment