[RUBY] 11th class: Class

Reference site

Chart type ruby-VI (hello class)

What is a class

A template for creating objects (data that is a variable used in a program, methods that are behaviors are bundled into one unit).

Wikipedia: Class and wikipedia: Object-Oriented Programming

1: Classification

class Greeter
  def initialize
    @name = gets_name
    puts_hello
  end

  def puts_hello
    puts "Hello #{@name}."
  end

  def gets_name
    name = ARGV[0] || 'world'
    return name
  end
end

Greeter.new

The function in class becomes method (method: behavior of object)

The following four keys are the keys this time

-Greeter.new: Call method outside of class

-Class Greeter: Class declaration

-Initialize method: Initializes the data in the class

-@Name: Instance variable initialize method

2: Class inheritance

#Inherited class
class Greeter < String
  def hello
    "Hello #{self}."
  end
end

#Inheriting class
class String
  def hello
    "Hello #{self}."
  end
end

A class can pass data and methods that it has in another class (inheritance)

In the above sample program, Greeter is passed from String

Small story: Double quotes

Q: What is double quotation?

A: "← This guy

In emacs, when processing a character string, enclose it in double quotation marks like "character string".

In emacs, there is a function to determine where the character string is by color. Even if you make a mistake and do not enclose it in double quotation marks, if you look at the color, you will notice the mistake in one shot.


Recommended Posts

11th class: Class
10th class: Recursion
8th class: rake
5th class: Output
5th
6th
6th
7th class: if, else
9th
11th :: ruby_VI (hello class)
7th
11th
9th
6th class: Variables, method
7th
11th
10th
10th
Anonymous class (anonymous class)
Class method
ObjectMapper class
ArrayList class
Special Lecture on Multi-Scale Simulation: 11th (class)
JDBC class load
Java class methods
[Java] Class inheritance
java Scanner class
Decompile class files
Java HashMap class
About class inheritance.
java (abstract class)
Class in Ruby
[Java] Nested class
Java anonymous class
About Java class
String class methods
13th (thor, rubocop)
[Week 11] class, ruby_sixth
java.lang.IncompatibleClassChangeError: Implementing class
[java] abstract class
[Java] Object class
Java local class
Impl class Vo class
WildFly class loading