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 3 years have passed since last update.

attr_accesorの利用中、integerで詰まった話

Last updated at Posted at 2021-06-07

備忘録です。

student.rb

class Student
    
    attr_accessor :name, :height
    
    def initialize(name, height)
        @name = name
        @height = height
    end
    
    def info
        puts '名前:' + @name
        puts '身長:' + @height
    end
    
   
end

student1 = Student.new('山浦', 167)
student2 = Student.new('土田', 178)

student1.info
student2.info

これで実行するとなぜかエラーになる。

 `+': no implicit conversion of Integer into String (TypeError)

@height の後に to_sをつけることで解決。

0
0
2

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?