Organize classes, instances, and instance variables

A note about classes, instances, and instance variables.

--Class is a blueprint --The instance is an entity --Instance variables are attributes.

To materialize,

--Class is a blueprint of a car --The instance is a real car (for example, Roadster or Silvia) --Instance variables are manufacturer, color, tire type, etc.

What is an instance variable?

A variable defined within an instance method of a class.

Variables that can be used in different instance methods if they are in the same class.

Regular variables can only be used within the same instance method.

The advantage of using instance variables is that you can omit the code to define.

Concrete example

class Bridge
  def bridge_height1(height_number)
    @height_number = height_number
  end
  def bridge_height2
    @height_number *10
  end
end

big_bridge = Bridge.new
puts big_bridge.bridge_height1(3)
puts big_bridge.bridge_height2 #If you don't use instance variables, you'll get an error.

output data:
3
30

Recommended Posts

Organize classes, instances, and instance variables
Classes and instances
[Ruby] Classes, instance variables, instances, etc ...
[Ruby] Classes and instances
About classes and instances
Ruby classes and instances
java (classes and instances)
Difference between variables and instance variables
About classes and instances (evolution)
Consideration about classes and instances
About Ruby classes and instances
About instance variables and attr_ *
Creating Ruby classes and instances
Writing code with classes and instances
Classes and instances Java for beginners
[Java] Differences between instance variables and class variables
Java programming (classes and instances, main methods)
Memorandum (Ruby: Basic Grammar: Classes and Instances)
Write code using Ruby classes and instances
Getting Started with Java_Chapter 8_About Instances and Classes
I compared classes and instances with worldly things
Ruby: Differences between class methods and instance methods, class variables and instance variables
java classes, instances, objects
String literals and instances
About the difference between classes and instances in Ruby
Java classes and instances to understand in the figure
[Swift] Constants and variables
Ruby variables and methods
List and happy classes
[Java] Variables and types
[Java Silver] What are class variables instance variables and local variables?
[For beginners] Explanation of classes, instances, and statics in Java
How to separate words in names in classes, methods, and variables
[Ruby] Creating code using the concept of classes and instances
(Note) Java classes / variables / methods
[Java] Generics classes and generics methods
Java programming (variables and data)
Difference between class and instance
[Ruby] Singular methods and singular classes
[Programming Complete] §4 Variables and constants
Ruby variables and functions (methods)
Ruby methods and classes (basic)
Java abstract methods and classes
[Ruby] Singular methods and singular classes