LoginSignup
2
0

More than 5 years have passed since last update.

【Ruby】インスタンス変数、クラス変数

Posted at

インスタンス変数

test.rb

class Test
  def setName(str)
    @name = str
  end

  def getName()
    return @name
  end
end

test = Test.new()
test.setName("hogehoge")
puts test.getName()
hogehoge

クラス変数

test.rb
class Test
  @@count = 0
  def increment()
    @@count += 1
  end

  def show_count()
    puts @@count
  end
end

test1 = Test.new()
test2 = Test.new()

test1.increment()
test1.show_count()
test2.increment()
test2.show_count()
1
2
2
0
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
2
0