已关闭此问题为not reproducible or was caused by typos。它目前不接受回答。
此问题是由打印错误或无法再重现的问题引起的。虽然类似的问题可能是on-topic在这里,这一个是解决的方式不太可能帮助未来的读者。
上个月关门了。
Improve this question
class ProcessTrip < MetroCardClassMethods
attr_accessor :travel_history, :station, :destination
def initialize
end
def update_balace_aumount(balance_details,trip_to,trip_complete)
effective_tarif = trip_complete ? passenger_tarif_for(_passenger_type) / 2 : passenger_tarif_for(_passenger_type)
trip_to['discount']= effective_tarif if trip_complete
trip_to['discount']= 0 if !trip_complete
if balance_details["balance"].to_i < effective_tarif
effective_trip_cost = effective_tarif - balance_details["balance"].to_i
trip_to["trip_cost"] += effective_trip_cost*0.02
balance_details["balance"] = 0;
else
balance_details["balance"] = balance_details["balance"].to_i - effective_tarif
end
end
end
def total_amount_collected ()
@@vertex.each do |metro_id, travel_history|
result = find_amount_collected_for_metrocard(self.class.card_list[metro_id], travel_history)
result.each do |key,value|
self.class.collection[key]["cost"]+=value["trip_cost"];
self.class.collection[key]["discount"]+=value["discount"];
self.class.collection[key]["passengers"]+=value["passenger"];
end
end
return @@collection
end
def find_amount_collected_for_metrocard(balance_details, trip_info)
trip_from={}
trip_info.each do |station, destination|
@station = station
if _is_return_journey(trip_from)
process_trip_segment(trip_from,_destination,_station)
trip_from[_station]["trip_cost"] = passenger_tarif_for(_passenger_type) * 0.5
update_balace_aumount(balance_details, trip_from[_station], true);
else
process_trip_segment(trip_from,_station,_station)
trip_from[_station]["trip_cost"] = (trip_from[_station]["trip_cost"] || 0) + passenger_tarif_for(_passenger_type)
update_balace_aumount(balance_details, trip_from[_station], false);
end
end
return trip_from
end
我试图从类ProcessTrip的示例调用total_amount_collected,但我得到一个错误,说它是一个私有方法,即使该方法是一个常规方法,我没有把它放在私有关键字下
trip.total_amount_collected
NoMethodError: private method `total_amount_collected' called for #<ProcessTrip:0x0000000110e33da0>
from (pry):3:in `get_total_collection'
我从下面的方法调用类
def get_total_collection
trip = ProcessTrip.new
trip.total_amount_collected
sort_passengers(total_collection);
print_summary(total_collection) if @print_summary
end
此外,我有超类,是由ProcessTrip类扩展
class MetroCardClassMethods
@@card_list = []
@@vertex = {};
@@collection ={"CENTRAL"=>{"cost"=>0,"discount"=>0,"passengers"=>[]},"AIRPORT"=>{"cost"=>0,"discount"=>0,"passengers"=>[]}}
@@station_from = {"CENTRAL"=>"AIRPORT","AIRPORT"=>"CENTRAL"}
@@station = []
def initialize(item)
@travel_history = {}
@metro_id = item["metro_id"]
@station = item["station"]
@passenger_type = item["passenger_type"]
@destination = @@station_from[@station]
end
class << self
def card_list=(card_list)
@@card_list=card_list
end
def card_list
@@card_list
end
def collection
@@collection
end
def station_from
@@station_from
end
def vertex
@@vertex
end
end
end
谁能解释一下为什么total_amount_collected被视为私有方法,而update_balace_aumount被视为公共方法
1条答案
按热度按时间vxbzzdmp1#
您的
def
/end
匹配已关闭。total_amount_collected
之前的最后一个end
关闭了ProcessTrip
类。然后,您完全在类外部定义total_amount_collected
方法。下面是一个简短的例子:如果您将其粘贴到IRB控制台,然后调用:
您将得到一个
private method
异常,因为some_other_method
被定义为Object
类上的一个新的私有方法,而不是MyClass
上的一个新的公共示例方法。