我尝试实现此示例以比较值:
error = system_errors[response_code]
raise error[:error_class].constantize, error[:error_message]
@system_errors = {100 => { error_class: 'SystemError', error_message = 'A general system error occured.'},
101 => { error_class: 'MaintenanceError', error_message = 'System is undergoing maintenance, request could not behandled.'},
110 => { error_class: 'AuthenticationError', error_message = 'Login failed. Check your API credentials.'},
120 => { error_class: 'ConfigurationError', error_message = 'Configuration error occured, e.g. terminal not configured properly. Check terminal settings.'},
200 => { error_class: 'CommunicationError', error_message = 'Communication with issuer failed, please contact support.'},
210 => { error_class: 'ConnectionError', error_message = 'Connection to issuer could not be established, please contact support.'},
220 => { error_class: 'AccountError', error_message = 'Issuer account data invalid, please contact support.'},
220 => { error_class: 'AccountError', error_message = 'Descption: Issuer account data invalid, please contact support.'},
230 => { error_class: 'TimeoutError', error_message = 'Issuer does not respond within given timeframe - please reconcile'}}
但是当我运行代码时,我收到了这个错误:
syntax error, unexpected '}', expecting => (SyntaxError)
...general system error occured.'},
... ^
/Users/response.rb:34: syntax error, unexpected '}', expecting =>
... request could not behandled.'},
... ^
/Users/response.rb:41: syntax error, unexpected '}', expecting =>
... timeframe - please reconcile'}}
... ^
/Users/response.rb:45: class definition in method body
/response.rb:80: syntax error, unexpected end-of-input, expecting keyword_end
from /Users/.rvm/rubies/ruby-2.2.2/lib/ruby/site_ruby/2.2.0/rubygems/core_ext/kernel_require.rb:55:in `require'
实现此代码的正确方法是什么? 基本上我想将哈希值中的值与哈希值进行比较。
答案 0 :(得分:1)
将所有=
替换为哈希内的:
。
@system_errors = {100 => { error_class: 'SystemError', error_message: 'A general system error occured.'},
101 => { error_class: 'MaintenanceError', error_message: 'System is undergoing maintenance, request could not behandled.'},
110 => { error_class: 'AuthenticationError', error_message: 'Login failed. Check your API credentials.'},
120 => { error_class: 'ConfigurationError', error_message: 'Configuration error occured, e.g. terminal not configured properly. Check terminal settings.'},
200 => { error_class: 'CommunicationError', error_message: 'Communication with issuer failed, please contact support.'},
210 => { error_class: 'ConnectionError', error_message: 'Connection to issuer could not be established, please contact support.'},
220 => { error_class: 'AccountError', error_message: 'Issuer account data invalid, please contact support.'},
220 => { error_class: 'AccountError', error_message: 'Descption: Issuer account data invalid, please contact support.'},
230 => { error_class: 'TimeoutError', error_message: 'Issuer does not respond within given timeframe - please reconcile'}}
答案 1 :(得分:1)
错误:error_message = '...'
必须为error_message: '...'
@system_errors = {100 => { error_class: 'SystemError', error_message: 'A general system error occured.'},
101 => { error_class: 'MaintenanceError', error_message: 'System is undergoing maintenance, request could not behandled.'},
110 => { error_class: 'AuthenticationError', error_message: 'Login failed. Check your API credentials.'},
120 => { error_class: 'ConfigurationError', error_message: 'Configuration error occured, e.g. terminal not configured properly. Check terminal settings.'},
200 => { error_class: 'CommunicationError', error_message: 'Communication with issuer failed, please contact support.'},
210 => { error_class: 'ConnectionError', error_message : 'Connection to issuer could not be established, please contact support.'},
220 => { error_class: 'AccountError', error_message: 'Issuer account data invalid, please contact support.'},
220 => { error_class: 'AccountError', error_message: 'Descption: Issuer account data invalid, please contact support.'},
230 => { error_class: 'TimeoutError', error_message: 'Issuer does not respond within given timeframe - please reconcile'}}