2016-05-05 5 views
0

メソッドは別のクラスで定義されていると私はメソッドの呼び出しは、明示的な受信機を持っている場合、あなたがからそれを呼んコンテキストでは重要ではありません方法は、互いに別のクラスのメソッドを呼び出します。使い方?

class Route 
attr_reader :stations #getter method 
end 

class Train 
    attr_accessor :route #getter and setter method 

    def show_stations 
    route.stations # How it works? 
    end 
end 

route = Route.new 
train = Train.new 

train.route = route 
train.show_stations 

答えて

0

に関連しているか理解していません。重要なことは、レシーバにその方法があることです。

メソッドコールroute.stationsのコンテキストは関係ありません。重要なことは、受信者routeが方法stationsを持っているかどうかです。 routeRouteインスタンスであるため、attr_reader :stationsで定義されているように、メソッドstationsを持っています。

関連する問題