[Ruby] Creating code using the concept of classes and instances

I am currently reviewing to train my logical thinking. This time, I will leave it because it was made by a different description method from the model answer.

problem

ruby.rb


class Article

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

end

Add to the above code so that you can get the following output result. However, use classes and instances.

ruby.rb


Author:Yamada
title:How to lose weight correctly
Text:Reduce one meal to 15g of fat and 35g of sugar

Model answer

ruby.rb


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("Yamada", "How to lose weight correctly", "Reduce one meal to 15g of fat and 35g of sugar")
puts "Author: #{article.author}"
puts "title: #{article.title}"
puts "Text: #{article.content}"

Pass the value to be assigned to the instance variable at the time of instance creation, Define a method to return an instance variable, It's the flow of calling at the end.

My answer

The flow is the same, but I also put the characters I want to output in the method.

ruby.rb


class Article

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

  def author
    puts "Author:#{@author}"
  end

  def title
    puts "title:#{@title}"
  end

  def content
    puts "Text:#{@content}"
  end

end

article = Article.new("Yamada", "How to lose weight correctly", "Reduce one meal to 15g of fat and 35g of sugar")
article.author
article.title
article.content

Can this still be done? I'm glad that the hypothesis is correct, I don't know which one is better. I would be grateful if anyone could tell me.

Recommended Posts

[Ruby] Creating code using the concept of classes and instances
Write code using Ruby classes and instances
Creating Ruby classes and instances
[Ruby] Classes and instances
Ruby classes and instances
About Ruby classes and instances
About the difference between classes and instances in Ruby
Writing code with classes and instances
Memorandum (Ruby: Basic Grammar: Classes and Instances)
Comparison of JavaScript objects and Ruby classes
Classes and instances
[Ruby] Code to display the day of the week
About classes and instances
java (classes and instances)
Java classes and instances to understand in the figure
[Technical memo] About the advantages and disadvantages of Ruby
[Ruby] Class nesting, inheritance, and the basics of self
Try using the query attribute of Ruby on Rails
[For beginners] Explanation of classes, instances, and statics in Java
Creating a calendar using Ruby
The nth and n + 1st characters of a Ruby string
[Ruby] Display today's day of the week using Date class
[Ruby] About the difference between 2 dots and 3 dots of range object.
About classes and instances (evolution)
Consideration about classes and instances
[Ruby] Singular methods and singular classes
Ruby methods and classes (basic)
The difference between programming with Ruby classes and programming without it
[Ruby] Singular methods and singular classes
[Java] Various summaries attached to the heads of classes and members
The design concept of Java's Date and Time API is interesting
About the behavior of ruby Hash # ==
[Ruby] See the essence of ArgumentError
[Ruby] Classes, instance variables, instances, etc ...
This and that of the JDK
Classes and instances Java for beginners
[Ruby] Display the contents of variables
How to operate IGV using socket communication, and the story of making a Ruby Gem using that method
[Ruby] I want to extract only the value of the hash and only the key
Parse and objectize JSON using the @JsonProperty annotation of the Java library Jackson
[Ruby basics] About the role of true and break in the while statement
Get a rough idea of the differences between protocols, classes and structs!
Calculate the percentage of "good", "normal", and "bad" in the questionnaire using SQL
Iterative processing of Ruby using each method (find the sum from 1 to 10)
[Ruby On Rails] How to search the contents of params using include?
Display Japanese calendar and days of the week using java8 standard class
Try using || instead of the ternary operator
[Ruby] Obtaining even values ​​using the even? Method
[Ruby on Rails] Code check using Rubocop-airbnb
Using Hystrix and Sentinel in code example
Test the integrity of the aggregation using ArchUnit ②
[Ruby] Simplify each using map and inject
Try using the Rails API (zip code)
part of the syntax of ruby ​​on rails
Folding and unfolding the contents of the Recyclerview
About the operation of next () and nextLine ()
Explanation of Ruby Time and Date objects
Summary of hashes and symbols in Ruby
Java programming (classes and instances, main methods)
Basic usage of enums and code examples
[Ruby] Cut off the contents of twitter-ads