Ruby 向一个类中添加许多变量的鲁比方式
我一直在做一个需要一长串国家来模拟的游戏(如果有的话,更像是一个网络玩具),我已经设法让它工作了,但我忍不住觉得我的解决方案既不是一种鲁莽的方式,也不是一种优雅的方式 代码看起来有点像这样:Ruby 向一个类中添加许多变量的鲁比方式,ruby,coding-style,Ruby,Coding Style,我一直在做一个需要一长串国家来模拟的游戏(如果有的话,更像是一个网络玩具),我已经设法让它工作了,但我忍不住觉得我的解决方案既不是一种鲁莽的方式,也不是一种优雅的方式 代码看起来有点像这样: class Countries include Singleton def get(tag) return eval "@#{tag}" end def initialize @array = [] @afghanista
class Countries
include Singleton
def get(tag)
return eval "@#{tag}"
end
def initialize
@array = []
@afghanistan = Country.new("Afghanistan", [:authoritarian, :reactionary, :sunni, :capitalist, :militarist, :southern_asia, :unstable])
@afghanistan.gdp = 20444
@afghanistan.population = 26023
@array << :afghanistan
@albania = Country.new("Albania", [:hybrid, :conservative, :sunni, :capitalist, :pacifist, :southern_europe])
@albania.gdp = 13276
@albania.population = 2893
@array << :albania
#and so on and so forth
end
attr_accessor :array
end
countries = Countries.instance
puts countries.get("usa").name
puts
for i in 0..(countries.array.size-1)
puts countries.get(countries.array[i]).name
end
但理想情况下,一个优雅的解决方案不需要.get(),而这看起来并不像是解决这个问题的Ruby方式。有没有更好的练习方法
我主要是从堆栈溢出、Ruby文档和测试中学到了一些知识,所以很可能我在这一过程中违反了很多最佳实践。Country类的初始值设定项使用字符串作为名称和要添加的标记数组,而其他属性则要在单独的行中添加。我会将countries的详细信息存储在文件(e.q.countries.yml或csv文件)或数据库中:
# in countries.yml
afganistan:
name: Afganistan
tags:
- authoritarian
- reactionary
- sunni
- capitalist
- militarist
- southern_asia
- unstable
gdp: 20444
population: 26023
albania:
name: Albania
tags:
...
...
然后您的课程简化为:
require 'yaml'
class Countries
include Singleton
def get(country)
@countries[country]
end
def initialize
@countries = {}
YAML.load_file('countries.yml').each do |country_key, options|
country = Country.new(options['name'], options['tags'])
country.gdp = options['gdp']
country.population = options['population']
@countries[country_key] = country
end
@countries.keys # just to keep the interface compatible with your example
end
end
有数百种方法可以干燥代码,但您的错误本质上不是使用(或建议的外部文件) 这就是我要做的,我做了一些假设,希望这能有所帮助
# I'll assume that Country is an actual class with a purpose, and not a simple
# structure.
Country = Struct.new(:name, :tags, :gdp, :population)
# list of all the countries
COUNTRIES_TABLE = [
["Afghanistan", [:authoritarian, :reactionary], 20444, 26023],
["Albania", [:hybrid, :conservative], 13276, 2893]
# etc..
]
COUNTRIES = COUNTRIES_TABLE.map { |e| Country.new(*e) }
# we could have directly defined the hash instead of a table, but this keeps
# everything more DRY
COUNTRIES_HASH = COUNTRIES.map {|e| [e.name, e]}.to_h
puts COUNTRIES_HASH["Albania"].name
COUNTRIES_HASH.map do |k,v|
puts v.name
end
你绝对想做的第一件事就是将国家存储在hash@DamianoStoffie你的意思是替换数组还是替换类?我推荐@spickermann的解决方案,但是关于类设计,这里有一些更大的问题。这可能值得一读,特别是单一责任原则和。非常感谢您的帮助,尽管我认为从表到散列有3个单独的对象似乎有点太多了。不过你知道得更清楚,谢谢你!我考虑过使用JSON文件,但这似乎比它的价值更麻烦。
# I'll assume that Country is an actual class with a purpose, and not a simple
# structure.
Country = Struct.new(:name, :tags, :gdp, :population)
# list of all the countries
COUNTRIES_TABLE = [
["Afghanistan", [:authoritarian, :reactionary], 20444, 26023],
["Albania", [:hybrid, :conservative], 13276, 2893]
# etc..
]
COUNTRIES = COUNTRIES_TABLE.map { |e| Country.new(*e) }
# we could have directly defined the hash instead of a table, but this keeps
# everything more DRY
COUNTRIES_HASH = COUNTRIES.map {|e| [e.name, e]}.to_h
puts COUNTRIES_HASH["Albania"].name
COUNTRIES_HASH.map do |k,v|
puts v.name
end