2017-11-08 12 views
1

私はRubyで昨日始めて問題があります。 私は、患者と患者の2つのクラスを持っています。 患者は(異なるアポイントメントの)複数の履歴を持つことができます。ここでオブジェクトをクラス内のフィールドとして配列します。単一のオブジェクトのパラメータにアクセスするには?

は私が今持っているものです:行と

class Patient 
    attr_accessor :name, 
        :surname, 
        :histories 

def initialize(*) 
    @histories = [] 
end 

def create_patient 
    @name = create_name_or_surname("name") 
    @surname = create_name_or_surname("surname") 
end 

def create_name_or_surname(name_or_surname) 
    #not relevant in this case 
end 

def add_history(history) 
    @histories.push(history) 
end 

def print_patient 
    puts "name: #{@name}" 
    puts "surname : #{@surname}" 

    ## I wish to do sth like: 
    ## puts "history date: #{@histories[1].date}" 
    ## to print content of Patient_history 

end 
end 

そして

class Patient_history 
    attr_accessor :date, 
       :description 

    def create_history 
    @date = create_date_or_desc("What is the date today?") 
    @description = create_date_or_desc("Write about an illness:") 
    end 
end 

: P患者 歴史と私が得る患者値を設定した後:

What is your name? 
john 
What is your surname? 
smith 
What is the date today? 
12/12/2016 
Write about an illness: 
sick 
#<Patient:0x007ffcb50ab518 @histories=[# 
<Patient_history:0x007ffcb50aad98 @date="12/12/2016", 
@description="sick">], @name="john", @surname="smith"> 

あなたは私に何をすべきかのヒントを教えてもらえますか?

+0

注: 'initialize'は' initialize(*) 'しないでください。 – tadman

+0

あなたの 'p patient'はうまく見えます...私は患者の記録を見ることができ、それには歴史の配列が含まれています。あなたは今何をしたいですか? – SteveTurczyn

+0

何をすべきか? – tadman

答えて

0

質問は少し曖昧ですが、患者の病歴のリストを印刷したいですか? #eachを使用して各履歴を繰り返し処理できます。

def print_patient 
    puts "name: #{@name}" 
    puts "surname : #{@surname}" 

    histories.each do |history| 
    puts "History Date: #{history.date}" 
    puts history.description 
    end 
end 
+0

はい!ありがとうございました! – bartaspol

関連する問題