かずきのBlog@hatena

すきな言語は C# + XAML の組み合わせ。Azure Functions も好き。最近は Go 言語勉強中。日本マイクロソフトで働いていますが、ここに書いていることは個人的なメモなので会社の公式見解ではありません。

教え方

友達の一人にRubyを教えてる。
オブジェクト指向を教えるのはどうやろうかな・・・

name = "Tarou"
pet_name = "Pochi"

puts "Hello! My name is #{name}. My pet's name is #{pet_name}."

を題材にしてみようかなぁ・・・

書き直すとこんなん?

class Person
  def initialize name
    @name = name
  end
  attr_accessor :name, :pet
  
  def introduction
    puts "Hello! My name is #{@name}. My pet's name is #{pet.name}"
  end
end

class Pet
  def initialize name
    @name = name
  end
  attr_accessor :name
end

person = Person.new "Tarou"
pet = Pet.new "Pochi"
person.pet = pet;

person.introduction

ん〜いやに長いな。
短くてOOPの説明にいいものってないかなぁ。