shinyMao shinyMao - 1 month ago 19
Ruby Question

Ruby Class error output

enter image description here

can anyone tell me why the last line of my code output nothing? THX

Answer

You need to use initialize not init method,

class Mystuff
  def initialize
    @apple="I am instance"
  end

  def talk
    puts 'I am taking'
  end

  attr_reader :apple
end

thing = Mystuff.new
thing.talk
puts thing.apple

output

I am taking
I am instance