{"action":"created","issue":{"url":"https://api.github.com/repos/takeroyasuhara/ruby-training/issues/3","labels_url":"https://api.github.com/repos/takeroyasuhara/ruby-training/issues/3/labels{/name}","comments_url":"https://api.github.com/repos/takeroyasuhara/ruby-training/issues/3/comments","events_url":"https://api.github.com/repos/takeroyasuhara/ruby-training/issues/3/events","html_url":"https://github.com/takeroyasuhara/ruby-training/issues/3","id":94604287,"number":3,"title":"7/9~ 学習まとめ（#3）","user":{"login":"takeroyasuhara","id":13011597,"avatar_url":"https://avatars.githubusercontent.com/u/13011597?v=3","gravatar_id":"","url":"https://api.github.com/users/takeroyasuhara","html_url":"https://github.com/takeroyasuhara","followers_url":"https://api.github.com/users/takeroyasuhara/followers","following_url":"https://api.github.com/users/takeroyasuhara/following{/other_user}","gists_url":"https://api.github.com/users/takeroyasuhara/gists{/gist_id}","starred_url":"https://api.github.com/users/takeroyasuhara/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/takeroyasuhara/subscriptions","organizations_url":"https://api.github.com/users/takeroyasuhara/orgs","repos_url":"https://api.github.com/users/takeroyasuhara/repos","events_url":"https://api.github.com/users/takeroyasuhara/events{/privacy}","received_events_url":"https://api.github.com/users/takeroyasuhara/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"milestone":null,"comments":2,"created_at":"2015-07-12T22:14:27Z","updated_at":"2015-07-14T02:00:17Z","closed_at":null,"body":"練習問題7.6.1の自力回答\r\n```Ruby\r\nclass Person\r\n  attr_reader :bloodtype # アクセッサーを省略し、読み取り専用のインスタンス変数bloodtypeを定義\r\n  def initialize(bloodtype) # インスタンスの初期化のためのメソッドを定義\r\n    @bloodtype = bloodtype\r\n  end\r\n  # compatibilityメソッドの定義が必要\r\n  # compatibilityメソッドはクラスメソッドである\r\n  # compatibilityメソッドの中では、引数に対してbloodtypeメソッドを呼び出し、文字列（血液型）を参照する\r\n  # また、呼び出した文字列同士の組み合わせをキーとし、相性度の値を返していく\r\n\r\n  class << Person # 特異クラス定義\r\n    def compatibility(p1,p2) # クラスメソッドの定義\r\n      @@compatibility = {\"A-A\"=>75,\"A-B\"=>50,\"A-O\"=>95,\"A-AB\"=>40,\"B-A\"=>50,\"B-B\"=>75,\"B-O\"=>75,\"B-AB\"=>90,\"O-A\"=>95,\"O-B\"=>75,\"O-O\"=>65,\"O-AB\"=>50,\"AB-A\"=>40,\"AB-B\"=>90,\"AB-O\"=>50,\"AB-AB\"=>70}\r\n      @@compatibility[\"#{p1.bloodtype}-#{p2.bloodtype}\"] #呼び出した文字列の組み合わせをキーとして値を返す\r\n    end\r\n  end\r\nend\r\na = Person.new(\"A\")\r\nb = Person.new(\"B\")\r\no = Person.new(\"O\")\r\nab = Person.new(\"AB\")\r\n\r\nputs Person.compatibility(b,ab)\r\n```\r\n\r\n教科書の回答にて、クラスメソッドの定義の前にクラス変数の定義とunless文が使用されているのはなぜか理解できていない。以下回答\r\n```Ruby\r\n  @@compatibility = nil # クラス変数の定義\r\n  class << Person # 特異クラス定義\r\n    def compatibility(p1,p2) # クラスメソッドの定義\r\n      unless @@compatibility # unless文　@@compatibilityが存在していれば、以下のキャッシュを代入する\r\n        @@compatibility = {\"A-A\"=>75,\"A-B\"=>50,\"A-O\"=>95,\"A-AB\"=>40,\"B-A\"=>50,\"B-B\"=>75,\"B-O\"=>75,\"B-AB\"=>90,\"O-A\"=>95,\"O-B\"=>75,\"O-O\"=>65,\"O-AB\"=>50,\"AB-A\"=>40,\"AB-B\"=>90,\"AB-O\"=>50,\"AB-AB\"=>70}\r\n      end\r\n      @@compatibility[\"#{p1.bloodtype}-#{p2.bloodtype}\"] #呼び出した文字列の組み合わせをキーとして値を返す\r\n    end\r\n  end\r\n```\r\n\r\n"},"comment":{"url":"https://api.github.com/repos/takeroyasuhara/ruby-training/issues/comments/121110319","html_url":"https://github.com/takeroyasuhara/ruby-training/issues/3#issuecomment-121110319","issue_url":"https://api.github.com/repos/takeroyasuhara/ruby-training/issues/3","id":121110319,"user":{"login":"takeroyasuhara","id":13011597,"avatar_url":"https://avatars.githubusercontent.com/u/13011597?v=3","gravatar_id":"","url":"https://api.github.com/users/takeroyasuhara","html_url":"https://github.com/takeroyasuhara","followers_url":"https://api.github.com/users/takeroyasuhara/followers","following_url":"https://api.github.com/users/takeroyasuhara/following{/other_user}","gists_url":"https://api.github.com/users/takeroyasuhara/gists{/gist_id}","starred_url":"https://api.github.com/users/takeroyasuhara/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/takeroyasuhara/subscriptions","organizations_url":"https://api.github.com/users/takeroyasuhara/orgs","repos_url":"https://api.github.com/users/takeroyasuhara/repos","events_url":"https://api.github.com/users/takeroyasuhara/events{/privacy}","received_events_url":"https://api.github.com/users/takeroyasuhara/received_events","type":"User","site_admin":false},"created_at":"2015-07-14T02:00:17Z","updated_at":"2015-07-14T02:00:17Z","body":"練習問題8.2\r\n```Ruby\r\nrequire \"date\"\r\n\r\ndef chg_month(list, &block)\r\n  result = []\r\n  unless block\r\n    list.each do |i|\r\n      n = Date::MONTHNAMES[i]\r\n      result << n  if n\r\n    end\r\n  else\r\n    list.each do |i|\r\n      next unless i >=1 && i<=12\r\n        n = block.call(i)\r\n        result << n  if n\r\n      end\r\n  end\r\n  puts result\r\nend\r\n\r\nchg_month([0,1,2,3,13])\r\nchg_month([1,2,13]){|m| \"#{m}月\"}\r\n# next文を使用すると、現在のループを途中で切り上げて次のループに進むことができる\r\n```\r\n\r\n if n の意味を理解できていない。"}}