⚠️ Warning: This is a draft ⚠️

This means it might contain formatting issues, incorrect code, conceptual problems, or other severe issues.

If you want to help to improve and eventually enable this page, please fork RosettaGit's repository and open a merge request on GitHub.

{{implementation|HQ9+}}{{collection|RCHQ9+}} This [[Ruby]] program implements an [[HQ9+]] interpreter.

class HQ9plus
  Dispatch = Hash.new(:unknown).merge({
      'h' => :hello, 
      'q' => :quine, 
      '9' => :beer, 
      '+' => :accumulate
  })

  def initialize(opts={})
    @program = if    opts[:program]  then opts[:program]
               elsif opts[:filename] then File.read(opts[:filename])
               else  ''
               end
    @accumulator = 0
  end
  attr_reader :accumulator

  def run
    @program.downcase.each_char {|char| self.send Dispatch[char]}
  end

  def hello
    puts "Hello, world!"
  end

  def quine
    puts @program
  end

  def beer
    puts '99 bottles here ...'
  end

  def accumulate
    @accumulator += 1
  end

  def unknown
    # do nothing
  end
end
  
hq9 = HQ9plus.new(:program => '+qhp;+9Q')
hq9.run
puts hq9.accumulator

Output:

+qhp;+9Q
Hello, world!
99 bottles here ...
+qhp;+9Q
2