Class: Rbsiev::Repl

Inherits:
Object
  • Object
show all
Defined in:
lib/rbsiev/repl.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(prompt:, verbose: false) ⇒ Repl



13
14
15
16
17
# File 'lib/rbsiev/repl.rb', line 13

def initialize(prompt:, verbose: false)
  @verbose = verbose
  @prompt = prompt
  @components = init_components
end

Class Method Details

.start(prompt: "REPL> ", verbose: false) ⇒ Object



8
9
10
11
# File 'lib/rbsiev/repl.rb', line 8

def self.start(prompt: "REPL> ", verbose: false)
  msg = Repl.new(prompt: prompt, verbose: verbose).loop
  puts msg if msg
end

Instance Method Details

#loopObject



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/rbsiev/repl.rb', line 19

def loop
  second_prompt = "." * (@prompt.length - 1) + " "

  msg = Kernel.loop {
    begin
      source = read_source(second_prompt)
    rescue EOFError => _
      break "Bye!"
    else
      next if source.nil?
    end

    begin
      @components.exec(source)
    rescue Error => e
      puts e.message
      next
    end
  }
  msg
end