enterメソッドで駅名が一緒に保存される理由がわかりません。。。
よろしくお願いいたします。
ruby
1class Gate 2 STATIONS = [:umeda, :juso, :mikuni] 3 FARES = [150, 190] 4 5 def initialize(name) 6 @name = name 7 end 8 9 def enter(ticket) 10 ticket.stamp(@name) 11 end 12 13 def exit(ticket) 14 fare = calc_fare(ticket) 15 fare <= ticket.fare 16 end 17 18 def calc_fare(ticket) 19 from = STATIONS.index(ticket.stamp_at) 20 to = STATIONS.index(@name) 21 distance = to - from 22 FARES[distance - 1] 23 end 24end 25
ruby
1class Ticket 2 attr_reader :fare, :stamp_at 3 4 def initialize(fare) 5 @fare = fare 6 end 7 8 def stamp(name) 9 @stamp_at = name 10 end 11end 12
ruby
1require 'minitest/autorun' 2require './lib/gate' 3require './lib/ticket' 4 5class GateTest < Minitest::Test 6 def test_gate 7 require 'byebug'; byebug 8 umeda = Gate.new(:umeda) 9 juso = Gate.new(:juso) 10 ticket = Ticket.new(150) 11 umeda.enter(ticket) 12 assert juso.exit(ticket) 13 end 14 15 def test_umeda_to_mikuni 16 umeda = Gate.new(:umeda) 17 mikuni = Gate.new(:mikuni) 18 19 ticket = Ticket.new(150) 20 umeda.enter(ticket) 21 refute mikuni.exit(ticket) 22 end 23end 24
プロを目指すなら、丸投げでぶん投げる質問はいかがなものかと思いますがw
回答2件
あなたの回答
tips
プレビュー