[Ruby] About instance generation

Problem: Define the class Fruits with the following specifications.

** Class variables ** ・ Sum

** Instance variable ** ・ Name ・ Price

** Class methods ** ・ Get_sum ↪️ "The total price is" class variable sum "yen" is displayed

** Instance method ** ・ Initialize ↪️ Pass the name and price as arguments and assign them to the instance variables name and price Add price to sum

After defining, let's create the following three instances from the class Fruits

instance

Instance name name price
apple Apple 120
orange Orange 200
strawberry Strawberry 60

** Template ** The template of the program to be made is as follows.

class Fruits
    #Add the class definition here
  end

  #Create 3 instances below

  #Once generated, get the class method_Let's call sum to display the total price

(1) First, define the class Fruits, and then define the class variable ** sum ** and the class method ** get_sum **.

(2) Next, define the instance variables name and price in the initialize method. The initialize method can receive the arguments of the new method. Receive the name and price from the new method and assign it to the instance variable. Then, add the price argument price received by the initialize method to the class variable "** @@ sum **".

(3) Pass the name and price as arguments when creating an instance with the new method.

④ Call ** get_sum ** to complete.

Answer

 class Fruits
    @@sum = 0

    def self.get_sum
      puts "The total price is#{@@sum}It's a yen"
    end

    def initialize(name, price)
      @name = name
      @price = price
      @@sum = @@sum + price
    end
  end

  apple = Fruits.new("Apple", 120)
  orange = Fruits.new("Orange", 200)
  strawberry = Fruits.new("Strawberry", 60)

  Fruits.get_sum

Recommended Posts

[Ruby] About instance generation
About Ruby instance methods
About Ruby symbols
About ruby ​​form
About Ruby Hashes
About Ruby arrays
About Ruby inheritance
About ruby block
About Ruby Hashes
About Ruby Symbols
About Ruby variables
About Enclosing Instance 2
About Ruby methods
About Ruby Kernel Module
About Ruby error messages
About Ruby exception handling
About Ruby Hashes (continued)
About eval in Ruby
[ruby] About here documents
About Ruby if statement
About calling instance methods
[Ruby] About instance generation
[Ruby] methods, instance methods, etc ...
About the [ruby] operator
Thinking about logic Ruby
[Self-use memo] (Ruby) class, class instance, instance
[Ruby on Rails] about has_secure_password
About regular expressions in Ruby
Ruby About various iterative processes
[Ruby] What is an instance?
About Ruby and object model
[Ruby] Class methods, instance methods, etc.
About Ruby classes and instances
About instance variables and attr_ *
Explanation about Ruby String object
About the behavior of ruby Hash # ==
About Ruby single quotes and double quotes
[Ruby] Classes, instance variables, instances, etc ...
[Ruby] Handle instance variables with instance methods
About =
About object-oriented inheritance and about yield Ruby
[Ruby] Review about nesting of each
Explanation about Array object of Ruby
Difference between Ruby instance variable and local variable
[Ruby on Rails] About bundler (for beginners)
[Ruby] 4-digit random number generation with sprintf
[Ruby on Rails] About Active Record callbacks
Find out about instance methods and self
[Ruby] Difference between methods with and without self in the class. About class methods and instance methods.