0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

Classについて演習問題回答

Posted at

演習問題回答

演習問題

  • 魚(fish)クラスで「マグロは500円です」出力してください
paiza.io
class Fish
    
  def initialize(name, price)
    # インスタンス変数には@をつける
    @name = name
    @price = price
  end
  
  def description_of_item
    "#{@name}#{@price}です"
  end

end

fish_1 = Fish.new("マグロ", "500円")
puts fish_1.description_of_item

演習問題2

  • たい焼きクラスを作成して、attr_accessorを使って200円のカスタード味と300円のあんこ味のを作ってみましょう。
paiza.io

class Taiyaki
  attr_accessor :name, :price
    
  def initialize(name, price)
    # インスタンス変数には@をつける
    @name = name
    @price = price
  end
  
  def consumption_tax
    (@price * 1.10).round.to_s + "円"
  end

end
taiyaki_1 = Taiyaki.new("あんこ味", 200)
taiyaki_2 = Taiyaki.new("カスタード味", 300)

puts taiyaki_1.name
puts taiyaki_1.price.to_s + "円"
puts taiyaki_2.name
puts taiyaki_2.price.to_s + "円"
  • たい焼きに消費税10%を追加しましょう
paiza.io

class Taiyaki
  attr_accessor :name, :price
    
  def initialize(name, price)
    # インスタンス変数には@をつける
    @name = name
    @price = price
  end
  
  def consumption_tax
    (@price * 1.10).round.to_s + "円"
  end

end
taiyaki_1 = Taiyaki.new("あんこ味", 200)
taiyaki_2 = Taiyaki.new("カスタード味", 300)

puts taiyaki_1.name
puts taiyaki_1.price.to_s + "円"
puts taiyaki_2.name
puts taiyaki_2.price.to_s + "円"
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?