Writing code with classes and instances

I want to output the author, title, and body using classes and instances.

class Article

  def initialize(author, title, content)
    @author = author
    @title = title
    @content = content
  end

end

#The output result is
#Author:Suzuki
#title: Hello Ruby!
#Text:Hello, Ruby

Use the new method to output the expected result Create an instance and assign it to the variable article.

article = Article.new("Suzuki", "Hello Ruby!", "Hello, Ruby")

Actual argument at this time (( "Suzuki", "Hello Ruby!", "Hello, Ruby")) to specify the. The value specified as the actual argument is passed to the formal arguments author, title, and content.

def initialize(author, title, content)
    @author = author
    @title = title
    @content = content
  end

The value passed to the formal argument is assigned to the instance variable declared by the initialize method.

We will define each method that returns the value of the instance.

def author
  @author
end

def title
  @title
end

def content
  @content
end

It will be output if you write a description that calls the method defined last.

class Article

  def initialize(author, title, content)
    @author = author
    @title = title
    @content = content
  end

  def author
    @author
  end

  def title
    @title
  end

  def content
    @content
  end
end

article = Article.new("Suzuki", "Hello Ruby!", "Hello, Ruby")
puts "Author: #{article.author}"
puts "title: #{article.title}"
puts "Text: #{article.content}"

#Call using expression expansion

Recommended Posts

Writing code with classes and instances
Classes and instances
Write code using Ruby classes and instances
[Ruby] Classes and instances
About classes and instances
Ruby classes and instances
java (classes and instances)
Getting Started with Java_Chapter 8_About Instances and Classes
About classes and instances (evolution)
Consideration about classes and instances
About Ruby classes and instances
Creating Ruby classes and instances
Organize classes, instances, and instance variables
[Ruby] Creating code using the concept of classes and instances
Classes and instances Java for beginners
Link Java and C ++ code with SWIG
[Java] Reading and writing files with OpenCSV
Java programming (classes and instances, main methods)
Memorandum (Ruby: Basic Grammar: Classes and Instances)
Writing code Ruby
Experience .NET 5 with Docker and Visual Studio Code
Differences in writing Java, C # and Javascript classes
[Review] Reading and writing files with java (JDK6)
Lombok with VS Code
HashMap and HashSet classes
String literals and instances
About the difference between classes and instances in Ruby
Java classes and instances to understand in the figure
SystemSpec introduction and writing
List and happy classes
Monitor source code changes with guard-shell and make & execute automatically
[For beginners] Explanation of classes, instances, and statics in Java
Sample code to parse date and time with Java SimpleDateFormat
The difference between programming with Ruby classes and programming without it
Sample code for basic mocking and testing with Mockito 3 + JUnit 5
Spring validation and error code
Docker management with VS Code
URLSession with URLSession and Combine normally
Format Ruby with VS Code
Hello World with VS Code!
Reduce verbose code with Lombok
[Ruby] Singular methods and singular classes
Ruby methods and classes (basic)
Java abstract methods and classes
[Ruby] Singular methods and singular classes
Convert line feed code to html line feed tag with Thymeleaf and output