Consideration about classes and instances

About classes and instances

I didn't understand the scope of class fields, class methods, instance methods, and the scope of effect of instance variables, so I summarized them.

My understanding has deepened, but a new mystery has emerged.

I would appreciate it if you could tell me any suggestions or supplements.

◆ Description (explanation)
class Zoo
def self.kind(animal_name)
@animal_name = animal_name
puts @animal_name #output:Giraffe
puts animal_name #output:Giraffe
puts @name #output:Blank
puts name #output:Zoo
end

def self.kind_name
puts @animal_name #output:Giraffe
# puts animal_name #output:NameError
end

puts @animal_name #output:Blank
# puts animal_name #output:NameError

def initialize(new_animal)
@new = new_animal
puts "#{@new}Has increased." #output:新たな仲間Has increased.
end

def kind(name)
@name = name
puts @animal_name #output:Blank
# puts animal_name output:NameError
puts @name #output:Lion
puts name #output:Lion
end

puts @name #output:Blank
puts name #output:Zoo

def color(animal_color)
@color = animal_color
#puts name output:NameError
puts "#{@new}of#{@name}Is#{@color}is" #output:新たな仲間ofライオンIs黄色is
#puts name output:NameError
end
end

Zoo.kind("Giraffe") 
#Class method self.Kind execution
Zoo.kind_name
#Class method self.kind_Run name

animal = Zoo.new("New companion") 
#Create animal instance, execute initialize method
puts animal.kind("Lion") 
#Kind instance method execution of animal instance

puts animal.color("yellow") 
#Execute color instance method of animal instance
 
#↓ Out of scope
# puts @animal_name output:Blank
# puts animal_name output:NameError
# puts @name output:Blank
#puts name output:NameError

◆ Terminal output contents


Zoo
Giraffe
Giraffe

Zoo
Giraffe
New friends have been added.

Lion
Lion

The new fellow lion is yellow

◆ Conclusion

-The output contents will be the contents read in order from the top including the class part.

-Instance variables are between class methods or between instance methods. It is possible to output each other. However, since you cannot create an instance in the class method, @ Is just a pipe that connects class methods.

-Instance variables do not cause NameError even if they are placed outside the scope, It creates a blank in the output destination.

◆ New question

-Why in a class field other than an instance method (including a class method) Is the class name output when an undefined name is output?

Recommended Posts

Consideration about classes and instances
About classes and instances
About classes and instances (evolution)
About Ruby classes and instances
Classes and instances
[Ruby] Classes and instances
java (classes and instances)
Getting Started with Java_Chapter 8_About Instances and Classes
Creating Ruby classes and instances
About the difference between classes and instances in Ruby
Writing code with classes and instances
Organize classes, instances, and instance variables
Consideration about Rails and Clean Architecture
Classes and instances Java for beginners
Java programming (classes and instances, main methods)
About standard classes
Write code using Ruby classes and instances
I compared classes and instances with worldly things
java classes, instances, objects
HashMap and HashSet classes
About Bean and DI
[Java] About anonymous classes
About gets and gets.chomp
About redirect and forward
About encapsulation and inheritance
List and happy classes
About Serializable and serialVersionUID
Java classes and instances to understand in the figure
[For beginners] Explanation of classes, instances, and statics in Java
About for statement and if statement
About synchronized and Reentrant Lock
[Java] Generics classes and generics methods
About Ruby hashes and symbols
[Ruby] Creating code using the concept of classes and instances
[Java] About String and StringBuilder
About the same and equivalent
About pluck and ids methods
About Java Packages and imports
About Ruby and object model
Consideration about the times method
[Ruby] Singular methods and singular classes
About abstract classes in java
Ruby methods and classes (basic)
About instance variables and attr_ *
About self-introduction and common errors
Java abstract methods and classes
[Ruby] Singular methods and singular classes
About C # lambda expressions and Linq
About Java static and non-static methods
About fastqc of Biocontainers and Java
Learn more about gems and bundlers
About the equals () and hashcode () methods
[Java beginner] About abstraction and interface
About Ruby single quotes and double quotes
[Ruby] Classes, instance variables, instances, etc ...
How to call classes and methods
About Ruby product operator (&) and sum operator (|)
About go get and go install from Go1.16
About removeAll and retainAll of ArrayList
Java generics (defines classes and methods)
About if statement and branch processing