Ruby 如何阅读格式化文本?
假设我有一个包含以下内容的字符串:Ruby 如何阅读格式化文本?,ruby,text-formatting,Ruby,Text Formatting,假设我有一个包含以下内容的字符串: TODO | Eat spaghetti. | High | food, happiness TODO | Get 8 hours of sleep. | Low | health CURRENT | Party animal. | Normal | socialization CURRENT | Grok Ruby. | High
TODO | Eat spaghetti. | High | food, happiness
TODO | Get 8 hours of sleep. | Low | health
CURRENT | Party animal. | Normal | socialization
CURRENT | Grok Ruby. | High | development, ruby
DONE | Have some tea. | Normal |
TODO | Destroy Facebook and Google. | High | save humanity, conspiracy
TODO | Hunt saber-toothed cats. | Low | wtf
DONE | Do the 5th Ruby challenge. | High | ruby course, FMI, development, ruby
TODO | Find missing socks. | Low |
CURRENT | Grow epic mustache. | High | sex appeal
阅读此类内容并将其存储在具有以下结构的对象中的最佳方式是什么
class example
attr_accessor status
attr_accessor description
attr_accessor priority
attr_accessor tags
end
我尝试使用以下正则表达式:
/[a-zA-Z0-9]*/.match(text above)
但我得到的只是
#<MatchData "TODO">
实现这一点的最佳方法是什么?您可能希望使用string.split而不是regex.match
请参阅您可能希望使用string.split而不是regex.match 请参见《待办事项:吃意大利面。快乐、食物、幸福》。拆分(“|”) 或 或
“待办事项|吃意大利面。|高|食物,幸福”。分开(“|”)
或
或
结果:
[
["TODO", "Eat spaghetti.", "High", "food, happiness"],
["TODO", "Get 8 hours of sleep.", "Low", "health"],
["CURRENT", "Party animal.", "Normal", "socialization"],
["CURRENT", "Grok Ruby.", "High", "development, ruby"],
["DONE", "Have some tea.", "Normal"],
[
"TODO",
"Destroy Facebook and Google.",
"High",
"save humanity, conspiracy"
],
["TODO", "Hunt saber-toothed cats.", "Low", "wtf"],
[
"DONE",
"Do the 5th Ruby challenge.",
"High",
"ruby course, FMI, development, ruby"
],
["TODO", "Find missing socks.", "Low"],
["CURRENT", "Grow epic mustache.", "High", "sex appeal"]
]
结果:
[
["TODO", "Eat spaghetti.", "High", "food, happiness"],
["TODO", "Get 8 hours of sleep.", "Low", "health"],
["CURRENT", "Party animal.", "Normal", "socialization"],
["CURRENT", "Grok Ruby.", "High", "development, ruby"],
["DONE", "Have some tea.", "Normal"],
[
"TODO",
"Destroy Facebook and Google.",
"High",
"save humanity, conspiracy"
],
["TODO", "Hunt saber-toothed cats.", "Low", "wtf"],
[
"DONE",
"Do the 5th Ruby challenge.",
"High",
"ruby course, FMI, development, ruby"
],
["TODO", "Find missing socks.", "Low"],
["CURRENT", "Grow epic mustache.", "High", "sex appeal"]
]
为了将数据放入类中,我们需要4个步骤: 首先在内存中获取数据:
file = 'path/to/my/file.txt'
raw_data = File.read(file)
解析数据时,每行取一行,按|分割,然后去掉空格和新行
parsed_data = raw_data.lines.map{|line| line.split('|').map(&:strip)}
p parsed_data.first #=> ["TODO", "Eat spaghetti.", "High", "food, happiness"]
定义类示例:
class Example
attr_accessor :status, :description :priority, :tags
def initialize(status, description, priority, tags)
@status, @description, @priority = status, description, priority
@tags = tags.split(', ') # Split tags and store in a list.
end
end
创建新对象:
list_of_examples = parsed_data.map{|line| Example.new(*line)}
p list_of_examples.first #=> #<Example:0x2212ae8 @priority="High", @description="Eat spaghetti.", @status="TODO", @tags=["food", "happiness"]>
list_of_examples=parsed_data.map{| line | Example.new(*line)}
p列出所有示例。首先#=>#
为了将数据放入类中,我们需要4个步骤:
首先在内存中获取数据:
file = 'path/to/my/file.txt'
raw_data = File.read(file)
解析数据时,每行取一行,按|分割,然后去掉空格和新行
parsed_data = raw_data.lines.map{|line| line.split('|').map(&:strip)}
p parsed_data.first #=> ["TODO", "Eat spaghetti.", "High", "food, happiness"]
定义类示例:
class Example
attr_accessor :status, :description :priority, :tags
def initialize(status, description, priority, tags)
@status, @description, @priority = status, description, priority
@tags = tags.split(', ') # Split tags and store in a list.
end
end
创建新对象:
list_of_examples = parsed_data.map{|line| Example.new(*line)}
p list_of_examples.first #=> #<Example:0x2212ae8 @priority="High", @description="Eat spaghetti.", @status="TODO", @tags=["food", "happiness"]>
list_of_examples=parsed_data.map{| line | Example.new(*line)}
p列出所有示例。首先#=>#
只需使用
拆分行并将其剥离。只需使用
拆分行并将其剥离即可。