- スコープ:クラス、モジュール、メソッド定義の本体
- 小文字
- _で始まる
- 「オブジェクトの壁やメソッドの壁を超えては参照できず、それが定義された場所でしか通用しない」↓リンク曰く - https://qiita.com/kansiho/items/f5ab9b6eeb990e6af327
local_variable.rb
num0 = 0
module Kure1
num1 = 100
p num1
def test
p num1
num2 = 200
end
end
def test_method
num3 = 300
end
class TestClass
num4 = 400
p num4
# p num0 # Error(ndefined local variable or method)
def test
num5 = 500
p num5
# p num4 # Error(undefined local variable or method)
end
end
test_class = TestClass.new
test_class.test
# Kure1.num1 # Error(undefined method)
# test_method.num3 # Error(undefined method)
# test_class.num4 Error(undefined method)
# test_class.num5 Error(undefined method)