crystal-lang / crystal

The Crystal Programming Language
https://crystal-lang.org
Apache License 2.0
19.45k stars 1.62k forks source link

Error: instantiating 'Crystal::Playground::Agent#i(Int32)' #9555

Open renich opened 4 years ago

renich commented 4 years ago

Submitting bugs

Crystal playground told me to report this. Here's what it's giving me

class Mineral
  getter common_name : String
  setter common_name
  getter hardness : Float64
  getter crystal_struct : String | Nil

  def initialize(@common_name, @hardness, @crystal_struct = "")
  end

  def describe
    "This is #{common_name} with a Mohs hardness of #{hardness} 
    and a structure of #{crystal_struct}."
  end

  def self.to_s
    "#{common_name}, #{hardness}, #{crystal_struct}"
  end
end

mine = Mineral.new("talc", 1.0, "monoclinic")
mine.describe

other_mine = Mineral.new("gold", 1.0)
other_mine.describe

Exception

error in line 22
Error: instantiating 'Crystal::Playground::Agent#i(Int32)'

My OS: Fedora 32 My Crystal version:

Crystal 0.35.1 (2020-06-19)

LLVM: 10.0.0
Default target: x86_64-unknown-linux-gnu
bcardiff commented 4 years ago

Besides the exception in the playground, the Mineral#to_s definition should be an instance method with an io argument

  def to_s(io : IO)
    io << "#{common_name}, #{hardness}, #{crystal_struct}"
  end
renich commented 4 years ago

Yep, thanks. Got confused with the instructions of the book (in which you wrote a foreword ;D)