[RUBY] About classes and instances

When studying programming, you need to learn the basic concepts. In Ruby, many terms such as "object" and "variable" come out and it is difficult, so I would like to output it in my own way to speed up understanding even little by little. I would be grateful if you could point out if there is something like "This is different!"

What are classes and instances?

In order to understand classes and instances in the first place, we need to understand the terms "object" and "method". A rough explanation is from the following.


It means "all data in Ruby". For example, characters such as "good morning" are called "character string objects", and numbers such as "139" are called "numeric objects".


The ** "processing" ** of an object is called a method. Every Ruby object has a "method". For example, like this -Character string object-> length method-> count the number of characters ・ Numerical object → to_s method → Convert to character string And so on, ** the methods that each object type has are different **. For example, ** the techniques that each monster can use are different depending on the monster **.


The introduction has become long, but the main subject is from here. A class is a "** collection of common properties and methods " and is often compared to a " car blueprint ". When you make a car, you need a blueprint, but there is something in common with all cars (such as "accelerating" with the steering wheel and brakes). That is " Let's put together the common parts in a blueprint **". In other words, it is a mechanism that allows you to create a class (blueprint) so that you can use it when making other cars. This is convenient.


An instance is an "object ** created by inheriting attributes and processing from a class **". Is it easy to understand an object (car) created based on a class (blueprint)? Since we have inherited the minimum necessary parts, we interpret that it is an advantage that "there is a handle" ** you do not have to make a blueprint from scratch **.

When I try to define a class

When I actually write the code, it looks like this.


class Apple(name of the class)
  #Method definition

Enclose it in class and end and you're done.

When you create an instance

You can create an instance like this.

#Specify the class name with the new method and define it in the variable
orange(Variable name) = apple(name of the class).new

Furthermore, the defined instance is output like this.

puts orange

And if you type ruby filename.rb in the terminal, it will be executed.


By writing an article like this, I thought it was very good to be able to sort out things that I didn't understand. I plan to write it on a regular basis, so it will be encouraging for me to see it if you like. Thank you for visiting!

Recommended Posts

About classes and instances
About classes and instances (evolution)
Consideration about classes and instances
About Ruby classes and instances
Classes and instances
[Ruby] 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
Classes and instances Java for beginners
About standard classes
Java programming (classes and instances, main methods)
Memorandum (Ruby: Basic Grammar: Classes and Instances)
Write code using Ruby classes and instances
I compared classes and instances with worldly things
java classes, instances, objects
HashMap and HashSet classes
String literals and instances
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
About for statement and if statement
About synchronized and Reentrant Lock
[Java] Generics classes and generics methods
About Ruby hashes and symbols
[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
[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
[Ruby] Creating code using the concept of classes and instances
About C # lambda expressions and Linq
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 ...
About Gradle's setup phase and execution phase
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 =