如何解决Object.name == array [element]
大家晚上好, 首先,我要为我是一名学生道歉,感谢您的帮助。到此为止。
这是我的代码,至少是与此问题相关的部分:
class BlackGlobeCli
attr_accessor :country_one,:country_two,:input_value
def initialize
@country_one
@country_two
@input_value
@greet_array = [
"Hello,traveler!","Welcome to Black Globe online exchange office! Below is a list of countries and organizations whose currencies we currently work with:"
]
@country_array = [
"United States","United Arab Emirates","Argentina","Australia","Canada","Switzerland","Chile","China","Colombia","Dominican Republic","Egypt","European Union","United Kingdom","Hungary","Israel","India","Mexico","Malaysia","Norway","Poland","Paraguay","Russia","Sweden","Turkey","Taiwan","Ukraine"
]
end
...
end
编辑:我正在使用的API在列表中保存国家/地区的货币信息。这是API的示例:
require 'net/http'
require 'json'
# Setting URL
url = "https://prime.exchangerate-api.com/v5/YOUR-API-KEY/latest/USD"
uri = URI(url)
response = Net::HTTP.get(uri)
response_obj = JSON.parse(response)
# Getting a rate
rate = response_obj['conversion_rates']['EUR']
这将返回:
{
"result":"success","documentation":"https://www.exchangerate-api.com/docs","terms_of_use":"https://www.exchangerate-api.com/terms","time_zone":"UTC","time_last_update":1581379262,"time_next_update":1581382982,"base":"USD","conversion_rates":{
"USD":1,"AED":3.6721,"ARS":60.7704,"AUD":1.4955,"BGN":1.7861,"BRL":4.3155,"BSD":1.0000,"CAD":1.3310,etc. etc. etc.
}
}
现在,使用我的BlackGlobeCli
类,我需要询问用户输入,使用该输入并创建第一个货币的对象,询问用户另一个输入,使用该输入来创建另一个国家对象和它的货币,并在处获得用户想要兑换并进行兑换的金额输入。
因此,我陷入了第一步(创建第一种货币的对象)。
我希望我现在能更好地解释它。
解决方法
我不确定您想要什么,如果您想在一类中实现货币兑换,例如下面的内容可能会有所帮助。如果我误解了您的问题,请发表评论。
class CurrencyExchanger
class CountryNotSupported < StandardError; end
def initialize(from,to,money)
[from,to].each { |e| raise CountryNotSupported if supported_countries.exclude? e }
@from = from
@to = to
@money = money
end
def supported_countries
YAML.load_file( '/your_path_to_country_yaml' )
# set countries in yml file,not initialize values in class explicitly
end
def exchange_rate
# implement exchange rate depends on @from and @to
end
def result
@money / exchange_rate
end
end
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。