我想實現使用Ruby的貨幣基礎的應用,我發現:通知要求的問題和Ruby
require 'dollar'
require 'franc'
puts 'Why does this not work?'
class Money
attr_reader :amount
def self.dollar(number)
Dollar.new number
end
def ==(other)
self.amount == other.amount
end
def self.franc(number)
Franc.new(number)
end
end
我有Franc
類,看起來像這樣:
require 'money'
class Franc < Money
attr_reader :amount
def initialize(amount)
@amount = number
end
def times(mul)
amount = @amount * mul
Franc.new(amount)
end
def ==(other)
return false unless other.is_a? self.class
super
end
end
這是將Kent Beck的一些代碼直接翻譯成Ruby。當我運行bin/rspec
我看到:
/home/vamsi/Do/wycash/lib/franc.rb:3:in `<top (required)>': uninitialized constant Money (NameError)
from /home/vamsi/Do/wycash/lib/money.rb:2:in `require'
from /home/vamsi/Do/wycash/lib/money.rb:2:in `<top (required)>'
from /home/vamsi/Do/wycash/lib/dollar.rb:1:in `require'
from /home/vamsi/Do/wycash/lib/dollar.rb:1:in `<top (required)>'
from /home/vamsi/Do/wycash/spec/dollar_spec.rb:1:in `require'
from /home/vamsi/Do/wycash/spec/dollar_spec.rb:1:in `<top (required)>'
from /home/vamsi/Do/wycash/.bundle/ruby/2.3.0/gems/rspec-core-3.5.4/lib/rspec/core/configuration.rb:1435:in `load'
from /home/vamsi/Do/wycash/.bundle/ruby/2.3.0/gems/rspec-core-3.5.4/lib/rspec/core/configuration.rb:1435:in `block in load_spec_files'
from /home/vamsi/Do/wycash/.bundle/ruby/2.3.0/gems/rspec-core-3.5.4/lib/rspec/core/configuration.rb:1433:in `each'
from /home/vamsi/Do/wycash/.bundle/ruby/2.3.0/gems/rspec-core-3.5.4/lib/rspec/core/configuration.rb:1433:in `load_spec_files'
from /home/vamsi/Do/wycash/.bundle/ruby/2.3.0/gems/rspec-core-3.5.4/lib/rspec/core/runner.rb:100:in `setup'
from /home/vamsi/Do/wycash/.bundle/ruby/2.3.0/gems/rspec-core-3.5.4/lib/rspec/core/runner.rb:86:in `run'
from /home/vamsi/Do/wycash/.bundle/ruby/2.3.0/gems/rspec-core-3.5.4/lib/rspec/core/runner.rb:71:in `run'
from /home/vamsi/Do/wycash/.bundle/ruby/2.3.0/gems/rspec-core-3.5.4/lib/rspec/core/runner.rb:45:in `invoke'
from /home/vamsi/Do/wycash/.bundle/ruby/2.3.0/gems/rspec-core-3.5.4/exe/rspec:4:in `<top (required)>'
from bin/rspec:17:in `load'
from bin/rspec:17:in `<main>'
spec_helper應該包含'require_relative'lib/money.rb'' –
你可以發佈[mcve]嗎? – Stefan