Variable type in ruby

Variable type in ruby

There are various types of variables and I didn't know how to use them properly, so I leave a note for myself.

table of contents

  1. [Local Variable]
  2. [Instance variable]
  3. [Class Variable]
  4. [Global Variables]

Local variables

local.rb


def local()
  a = 3
end
p a

# =>Traceback (most recent call last):
local.rb:5:in `<main>': undefined local variable or method `a' for main:Object (NameError)

a defined by the local method cannot be called from outside the method.

Instance variables

instance.rb


class Instance
  def set_val(val)
    @val = val
  end

  def put_val
    p @val
  end
end

test1 = Instance.new
test1.set_val("aaa")
test1.put_val

test2 = Instance.new
test2.put_val


# =>
"aaa"
nil

Put "aaa" in @val of instance test1 using set_val method. After that, in instance test2, put_val is called, but nil is returned. From this, it can be seen that the value is held for each instance.

instance.rb


class Instance
  def set_val(val)
    @val = val
  end

  def put_val
    p @val
  end
end

test1 = Instance.new
test1.set_val("aaa")
test1.put_val

test2 = Instance.new
test2.set_val("bbb")
test2.put_val
# =>
"aaa"
"bbb"

It is necessary to use the set_val method for instance test2 as well.

Class variables

class.rb


class Instance
  def set_val(val)
    @@val = val
  end

  def put_val
    p @@val
  end
end

test1 = Instance.new
test1.set_val("aaa")
test1.put_val

test2 = Instance.new
test2.put_val
# =>
"aaa"
"aaa"

Unlike the previous instance variable, the value is shared by all instances of the same class.

class.rb


class Instance
  def set_val(val)
    @@val = val
  end

  def put_val
    p @@val
  end
end

test1 = Instance.new
test1.set_val("aaa")
test1.put_val

test2 = Instance.new
test2.set_val("bbb")
test2.put_val

test1.put_val

# => 
"aaa"
"bbb"
"bbb"

Global variables

global.rb


class Global
  def set_val(val)
    $val = val
  end
end

test1 = Global.new
test1.set_val("aaa")

p $val

# => 
"aaa"

Recommended Posts

Variable type in ruby
Is there no type in Ruby?
ruby constant variable
Chart type Ruby
Heavy in Ruby! ??
About eval in Ruby
About var used in Java (Local Variable Type)
Type determination in Java
Fast popcount in Ruby
ABC177 --solving E in Ruby
Validate JWT token in Ruby
Implemented XPath 1.0 parser in Ruby
Read design patterns in Ruby
Write class inheritance in Ruby
Ruby # {} is not variable expansion
Try functional type in Java! ①
Integer unified into Integer in Ruby 2.4
Multiplication in a Ruby array
About regular expressions in Ruby
Birthday attack calculation in Ruby
Judgment of fractions in Ruby
Find Roman numerals in Ruby
Try using gRPC in Ruby
[Ruby] Find numbers in arrays
Format Timestamp type in Thymeleaf
NCk mod p in Ruby
Chinese Remainder Theorem in Ruby
Sorting hashes in a Ruby array
Easily try Ruby 3.0.0 type checking (rbs)
Basics of sending Gmail in Ruby
variable
How to iterate infinitely in Ruby
Ruby on Rails variable, constant summary
Achieve 3-digit delimited display in Ruby
I tried to write code like a type declaration in Ruby
Encoding when getting in Windows + Ruby
Run GraphQL Ruby resolver in parallel
Ruby on Rails Japanese-English support i18n
[Ruby] Extracting double hash in array
String output method memo in Ruby
Implement a gRPC client in Ruby
Write keys and values in Ruby
[Super Introduction] About Symbols in Ruby
Hanachan in Ruby (non-destructive array manipulation)
What is a reference type variable?
Manipulating data in GCS during Ruby
[Ruby] undefined method `dark?'occurs in rqr_code
openssl version information in ruby OPENSSL_VERSION
Ruby methods often used in Rails
[Caution summary] Set Ruby time (Time type/Date type/DateTime type) in the column
Make Ruby segfault in two lines
[Java] Display the bit string stored in the byte type variable on the console
I tried a calendar problem in Ruby
Implementing poker little by little in Ruby Part 2
How to get date data in Ruby
Regarding String type equivalence comparison in Java
Create variable length binary data in Java
Acquisition of article information in ruby ​​scraping
Implementing poker little by little in Ruby Part 1
[Easy-to-understand explanation! ] Reference type type conversion in Java
Directory information of DEFAULT_CERT_FILE in Mac ruby 2.0.0