英文:
Where to store my strings in a plain ruby project
问题
I noticed that I had some gaps in pure ruby and decided to build a side project in plain ruby.
我注意到我在纯 Ruby 方面有些不足,所以决定用纯 Ruby 构建一个辅助项目。
My project is basically a discord bot and i'm wondering what's the best way to store some data.
我的项目基本上是一个 Discord 机器人,我在想如何最好地存储一些数据。
My bot use a method to ask questions and await input from user, all questions data are stored in a object with this structure:
我的机器人使用一种方法来提问并等待用户输入,所有问题的数据都存储在一个具有以下结构的对象中:
QUESTIONS = {
title1: {
content: 'string',
validator: /regex/,
invalid_input_message: 'string'
},
title2: {
content: 'string',
validator: /regex/,
invalid_input_message: 'string'
},
# ...
}
Since i will implement a lot of question, i don't want to store this object in my class file. Inspired by i18n, my first intention was to use a yaml file, but i have regex too not only string. Moreover, since i would have to load my YAML into a ruby object, i was wondering if i could just store my object in a ruby file and import it to my Bot class.
因为我要实现很多问题,我不想将这个对象存储在我的类文件中。受到 i18n 的启发,我最初想使用一个 YAML 文件,但我不仅有字符串还有正则表达式。此外,由于我需要将 YAML 加载到一个 Ruby 对象中,我在想是否可以将我的对象存储在一个 Ruby 文件中,然后将其导入到我的 Bot 类中。
Would it be to store my object in a ruby file or should i use YAML ? And how to deal with this regex if i go for YAML ?
是否应将我的对象存储在一个 Ruby 文件中,还是应该使用 YAML?如果选择 YAML,如何处理这些正则表达式?
thanks for your time
谢谢您的时间
英文:
i'm coding with Rails since 2 years. I noticed that I had some gaps in pure ruby and decided to build a side project in plain ruby.
My project is basically a discord bot and i'm wondering what's the best way to store some data.
My bot use a method to ask questions and await input from user, all questions data are stored in a object with this structure:
QUESTIONS = {
title1: {
content: 'string',
validator: /regex/,
invalid_input_message: 'string'
},
title2: {
content: 'string',
validator: /regex/,
invalid_input_message: 'string'
},
# ...
}
Since i will implement a lot of question, i don't want to store this object in my class file. Inspired by i18n, my first intention was to use a yaml file, but i have regex too not only string. Moreover, since i would have to load my YAML into a ruby object, i was wondering if i could just store my object in a ruby file and import it to my Bot class.
Would it be to store my object in a ruby file or should i use YAML ? And how to deal with this regex if i go for YAML ?
thanks for your time
答案1
得分: 1
YAML 允许存储正则表达式,当你需要告诉解析器允许解析 Regexp
时。
require 'yaml'
yaml = <<~YAML
string: Hallo World
regexp: !ruby/regexp /^regexp/
YAML
hash = YAML.load(yaml, permitted_classes: [Regexp])
hash['string']
#=> "Hallo World"
hash['regexp']
#=> /^regexp/
hash['regexp'].class
#=> Regexp
英文:
YAML allows storing regexp too, when you need to tell the parser to allow parsing Regexp
.
require 'yaml'
yaml = <<~YAML
string: Hallo World
regexp: !ruby/regexp /^regexp/
YAML
hash = YAML.load(yaml, permitted_classes: [Regexp])
hash['string']
#=> "Hallo World"
hash['regexp']
#=> /^regexp/
hash['regexp'].class
#=> Regexp
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论