LoginSignup
0
2

More than 3 years have passed since last update.

【備忘録】Ruby : 基本的なclass作成、定数の扱い方

Last updated at Posted at 2020-02-12
sample.rb

class SampleClass
    attr_accessor :name,:greet
    # attr_accessor:name,:greetはinitializeメソッド内の
    # @name,@greetに関連
    def initialize(name,greet)
        # initializeメソッド => classをnewした時に必ず呼び出される。
        # メソッドの引数で受け取った値をインスタンス変数
        # にセットしている。
        @name=name
        @greet=greet
    end
end

sample = SampleClass.new("Tanaka","挨拶")
# newメソッドでクラスの定数に引数を渡したので
# sampleオブジェクトではname="Tanaka",greet="挨拶"が入っている。


0
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
2