About the difference between classes and instances in Ruby


It comes out while learning Ruby The concept of classes and instances.

I didn't understand what it meant when I first started school, so I will write with an example.

What is a class

A template for determining common attributes and processing rules that you want to have in a value (data). Since it has no substance, the data cannot be moved by the class alone.

Example) In terms of a car, it corresponds to a "blueprint". What to do with colors and shapes (common attributes) in blueprints, What to do with running, stopping, turning signals, etc. (common processing) Paper on which such things are written. It's just a blueprint, so it won't work without a car (instance).

#Class definition (starts with a capital letter)
class Car


What is an instance?

Data created based on a class. It has an entity and has common attributes and processes defined in the class.

Example) In terms of a car, it corresponds to a "car". Cars have colors and shapes (common attributes), You can run, stop, turn signals, etc. (common processing). It's a car made from blueprints, so it has substance and of course works.

#Instance generation
class Car


taxi = Car.new  #Create an instance of Car class with new method and assign it to variable taxi
puts taxi  #Output instance

Recommended Posts

About the difference between classes and instances in Ruby
About the difference between "(double quotation)" and "single quotation" in Ruby
About Ruby classes and instances
[Ruby] I thought about the difference between each_with_index and each.with_index
[Ruby] Difference between methods with and without self in the class. About class methods and instance methods.
[Ruby] About the difference between 2 dots and 3 dots of range object.
The difference between programming with Ruby classes and programming without it
About the difference between irb and pry
Difference between "|| =" and "instance_variable_defined?" In Ruby memoization
[Ruby] Classes and instances
About classes and instances
Ruby classes and instances
[Ruby] Difference between symbol variables and character string variables. About the difference between [: a] and ['a'].
[Rails / ActiveRecord] About the difference between create and create!
Understand the difference between abstract classes and interfaces!
About classes and instances (evolution)
Consideration about classes and instances
Creating Ruby classes and instances
Calculate the difference between numbers in a Ruby array
Java classes and instances to understand in the figure
[Ruby] Difference between get and post
[Ruby] Difference between is_a? And instance_of?
Understand the difference between each_with_index and each.with_index
[Ruby] Creating code using the concept of classes and instances
Think about the differences between functions and methods (in Java)
[Java] Note about the difference between equivalence judgment and equality judgment when comparing String classes
Classes and instances
Difference between final and Immutable in Java
Difference between Ruby instance variable and local variable
Difference between pop () and peek () in stack
Memorandum (Ruby: Basic Grammar: Classes and Instances)
Difference between getText () and getAttribute () in Selenium
Write code using Ruby classes and instances
[Ruby] Difference between print, puts and p
Difference between int and Integer in Java
Easy to understand the difference between Ruby instance method and class method.
[Rails] Difference in behavior between delegate and has_many-through in the case of one-to-one-to-many
A note on the differences between interfaces and abstract classes in Java
[Ruby basics] About the role of true and break in the while statement
Understand the difference between int and Integer and BigInteger in java and float and double
[Java] Understand the difference between List and Set
Getting Started with Java_Chapter 8_About Instances and Classes
[iOS] Understand the difference between frame and bounds
About the idea of anonymous classes in Java
Difference between next () and nextLine () in Java Scanner
[Understanding] Differences between hashes and arrays in Ruby
What is the difference between SimpleDateFormat and DateTimeFormatter? ??
[Ruby] Difference between puts and return, output and return value
About eval in Ruby
The difference between puts and print in Ruby is not just the presence or absence of line breaks
Difference between vh and%
Difference between i ++ and ++ i
About the [ruby] operator
java (classes and instances)
What is the difference between a class and a struct? ?? ??
[Ruby] Difference between receiver and object. Differences between Ruby objects and JS objects
Differences between Ruby syntax error statements in Ruby and binary
Regarding the difference between the three Timeouts in Java's HttpClient
Difference between new and create in Rais action controller
In fact, Ruby distinguishes between line breaks and whitespace.