Execute HQ9+/Ruby: Difference between revisions

m
Fixed syntax highlighting.
(add Ruby)
 
m (Fixed syntax highlighting.)
 
(6 intermediate revisions by 3 users not shown)
Line 1:
{{implementation|HQ9+}}{{collection|RCHQ9+}}
This [[Ruby]] program implements an [[HQ9+]] interpreter.
<langsyntaxhighlight lang="ruby">class HQ9plus
methodsDispatch = Hash.new(:unknown).merge({
methods[ 'h'] => :hello,
methods[ 'q'] => :quine,
methods[ '9'] => :beer,
methods[ '+'] => :accumulate
})
 
def initialize(opts={})
@program = if opts[:program] then opts[:program]
elsif opts[:filename] then File.read(opts[:filename])
else ''
end
@accumulator = 0
end
Line 11 ⟶ 19:
 
def run
@program.downcase.each_char do {|char| self.send Dispatch[char]}
methods = Hash.new(:unknown)
methods['h'] = :hello
methods['q'] = :quine
methods['9'] = :beer
methods['+'] = :accumulate
 
@program.downcase.each_char do |char|
send methods[char]
end
end
 
Line 45:
hq9 = HQ9plus.new(:program => '+qhp;+9Q')
hq9.run
puts hq9.accumulator</langsyntaxhighlight>
 
Output:
9,476

edits