Class: Peruby::Op::HashLiteral

Inherits:
Base
  • Object
show all
Defined in:
lib/peruby/op/list.rb

Overview

Anonymous hash reference.

Instance Method Summary collapse

Methods inherited from Base

#argument_cells, #local_slot, #lvalue, #run_subroutine, #to_callable, #warning_directive?

Constructor Details

#initialize(items) ⇒ HashLiteral

Returns a new instance of HashLiteral.



89
90
91
# File 'lib/peruby/op/list.rb', line 89

def initialize(items)
  @items = items
end

Instance Method Details

#run(env, context) ⇒ Object



93
94
95
96
97
98
99
# File 'lib/peruby/op/list.rb', line 93

def run(env, context)
  values = @items.flat_map { |item| item.run(env, :list) }
  hash = PerlHash.new
  values.each_slice(2) { |key, value| hash.slot(Conv.to_str(key&.get)).set(value&.get) }
  ref = Ref.new(hash)
  context == :list ? [Scalar.new(ref)] : ref
end