LoginSignup
0
0

More than 5 years have passed since last update.

【Ruby】ローカル変数の小さなサンプルメモ

Posted at
  • スコープ:クラス、モジュール、メソッド定義の本体
  • 小文字
  • _で始まる
  • 「オブジェクトの壁やメソッドの壁を超えては参照できず、それが定義された場所でしか通用しない」↓リンク曰く - 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)
0
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
0
0