6th

variable

This time we will deal with variables and methods

Theme

Create a program that assigns the received argument ARGV [0] to a variable called name and issues it.

> ruby name_variable.rb Ruby
Ruby

Commentary

Variables in ruby ​​do not need to be type-declared like in C language, so you can enter them as follows.

name='Ruby'

Variable type

There are various variables in Ruby as follows.

・ Local variables ・ Constants ・ Global variables ・ Block variables ・ Instance variables ・ Class variables ・ Class instance variables

Local variables

These are the variables listed in the explanation. It is most often used.

Declaration

You can use alphanumeric characters and \ _ (underscore) in the variable name. You cannot use numbers at the beginning, you need to declare and initialize at the same time.

name="Ruby"    #ok
name_1="Ruby"  #ok

2_name="Ruby"  #NG  ->SyntaxError

name           #NG ->NameError

scope

It can be used only within the defined location such as method and class. References cannot be made beyond classes and methods, and an error will occur if a reference is made from a location other than the defined location.

constant

Declaration

The rules are the same as local variables, but constants are declared in all uppercase. "Camel case", which capitalizes only the first letter, is used for class names.

NAME="ruby"
Name="ruby" #Not used as a constant as it is confusing with the class name

scope

It can be referred to in the defined class, in the module, or in the class that inherits the class.

RUBY=2.78

puts RUBY
2.78

Since it can be defined in the class, even the same constant name can have different values ​​for each class.

Global variables

Declaration

You must add "$" at the beginning of the variable name.

$name="ruby"

scope

A variable that is the opposite of a local variable and can be referenced across methods and classes. You can refer to or change it from anywhere in the program regardless of the defined location.

$name="ruby"

def greet
 p $name
end

greet
"ruby"

Block variable

Declaration

It can be declared by enclosing it in "|" in "{} (brace) or in do ~ end" in the block.


#When grouping in multiple lines
3.times do |i,x| 
end

#When putting together in one line
[1,2,3].inject {|sum, n| sum+n}

scope

A temporary variable used within a block when the block is executed. When performing processing such as repetition, processing is performed while assigning a value to the "block variable". Can only be referenced from within the block.

num=99
3.times do |i, x=num|
 num=i*10 
 p x 
end
10

Instance variables

Declaration

Add "@" at the beginning of the variable.

@name="ruby"

scope

Variables that can only be used within instance methods. Each instance can have a different value and can be referenced across methods.

class User
 def initialize(name)
  @name=name
 end

 def put_name
  p @name
 end
end

user1=User.new("ruby")
user2=User.new("python")
user1.put_name
user2.put_name

"ruby"
"python"

Class variables

Declaration

Add "@@" at the beginning of the variable.

@@name="ruby"

scope

A variable that can handle values ​​shared within a defined class. Even if it is used in other instances, it is treated as a common value if it is in the same class, so only one value can be handled in the class. It can also be referenced from a class that inherits the class.

class Ruby
 @@number=1
 def foo
  puts @@number
 end
end

num=Ruby.new
num.foo
1
class Jewelry < Ruby
 def hoge
  puts @@number
 end
end

num2=Jewelry.new
num2.hoge
1

In this way, it can be seen that the Jewelry class, which inherits the Ruby class, can also access the class variables.

Class instance variable

Declaration

Like an instance variable, it starts with "@", but it is distinguished from an instance variable depending on where it is defined.

@class_instance_var="ruby"

scope

Like class variables, they can be defined within a class and referenced only within the defined class.

Like an instance variable, it starts with @, but if it is defined in a class, it is distinguished by the class instance variable, and if it is defined in an instance, it is distinguished by the place where it is defined as an instance variable. The difference from class variables is that they cannot be referenced from classes that inherit the class.

class MyClass
 @class_instance_var="ruby"
 def sef.class_instance_var
  puts @class_instance_var
 end

 def class_instance_var
  puts @class_instance_var 
 end
end

instance=MyClass.new
instance.class_instance_var

Since it is recognized as an instance variable in the instance method, it can be seen that the class instance variable cannot be accessed.

 class MyClass
  @class_instance_var="ruby"
  def self.class_instance_var
   puts @class_instance_var
  end
  def class_instance_var
   puts @class_instance_var
  end
end

MyClass.class_instance_var
ruby

In the case of class methods, you can access class instance variables.

class SubClass < MyClass
 def self.class_instance_var_again
  puts @class_instance_var
 end
end

SubClass.class_instance_var_again

Class instance variables cannot be accessed from inherited classes.

References

https://www.sejuku.net/blog/12879


Recommended Posts